0

我正在寻找昨晚在使用外键连接两个表时遇到的真实场景问题。实际上我想代表外键从第二个表中获取所有值。

这是我的两张表,假设:

table1 (id_user_history(PK),id_user(FK), order_no, p_quantity)
table2 (id_shoping_cart(PK), id_user(FK),order_id, prod_quantity)

现在我想通过将这些表与 table1(id_user(Fk)) 和 table2(id_user(FK)) 连接来从 table2 中获取所有值

4

2 回答 2

0

看起来一个简单的连接符合要求:

select  *
from    table1 t1
left join
        table2 t2
on      t1.id_user = t2.id_user
于 2013-01-24T12:56:53.940 回答
0
SELECT  *
FROM    table2 t2
LEFT JOIN
        table1 t1
on      t1.id_user = t2.id_user

表 2 中的所有记录,只有与表 1 匹配的记录。

SQL主要是设置逻辑。这是一个有助于可视化的链接。 http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html

于 2013-01-24T13:13:28.923 回答