我觉得这个白痴问这个......
Table 1: users id serial person integer username char(32) Table 2:persons id serial name char(16)
我可以通过在用户中提供用户名来运行在人员中返回名称字段的查询吗?
users 1 | 1 | larry123 persons 1 | larry 2 | curly
SQL?
select name from persons where users.person=persons.id and users.username='larry123';
Run Code Online (Sandbox Code Playgroud)
期望的回报
larry
到目前为止,我已经完成了两次传递,并且认为使用连接的嵌套选择可能是我需要的
1 | larry
这听起来像你问如何做到在SQL中加入:
SELECT
name
FROM
users JOIN persons ON (users.person = persons.id)
WHERE
users.username = 'larry123';
Run Code Online (Sandbox Code Playgroud)
这几乎是你写的查询.你所缺少的只是join子句.你也可以像这样加入:
SELECT name
FROM users, persons
WHERE
users.person = persons.id
AND users.username = 'larry123';
Run Code Online (Sandbox Code Playgroud)
我建议找一个写得很好的SQL介绍.
| 归档时间: |
|
| 查看次数: |
927 次 |
| 最近记录: |