[相同的帖子,但有点复杂的问题]
它应该是简单的,但我错过了一些东西。我有两个表
toneid id email
1 634 45
2 643 23
3 648 55
table_two (tid is autoincrement with primary key)
tid
633
634
643
648
我期望的结果应该是
tid email
633 null
634 45
643 23
648 55
我的查询是
select table_two.tid,
table_one.id,
table_one.email
from table_two
left join table_one on table_one.id = table_two.tid
但是它返回
tid email
634 45
643 23
648 55
你差不多搞定了,只要把左边的连接反过来:
select table_two.tid,
table_one.id,
table_one.email
from table_two
left join table_one on table_one.id = table_two.tid
总是必须这样读:从有更多值的表中获取所有内容,并将其与可能有注册表关联的表左联接。这里是table_two left与table_one连接
您的表顺序不对。使用LEFT JOIN
,但将第一个表作为保存所有记录的表:
select table_two.tid, table_one.id, table_one.email
from table_two left join
table_one
on table_one.id = table_two.tid;
我发现使用left join
s连接这些表的结构是最直观的方法。就我个人而言,我很难理解混合左连接、右连接和内连接的查询。通常情况下,您拥有的是一个"驱动"表,将其放在首位似乎是编写查询的最清晰方法。