我有两张桌子:
1:
id | name
1 | test
2 | test1
2
id | related_id | additional
1 | 1 | 1
2 | 1 | 2
1表中的id与2中的related_id相关
如何在没有复制记录的情况下加入1个表,因此第二个表中的结果只有1行(related_id,其他可以是任意)
id | name | related_id | additional
1 | test | 1 | 1
2 | test1| NULL | NULL
更新 如果我在INNER / LEFT JOIN之后尝试分组,结果是
id | name | related_id | additional
1 | test | 1 | 1
答案 0 :(得分:1)
您可以使用主键
对结果进行分组select *
from table1 left join table2
on table1.id = table2.related_id
group by table1.id
答案 1 :(得分:0)
您可以使用联接
SELECT
*
FROM table1 as t1
LEFT JOIN (
SELECT
MAX(id),
related_id
FROM table2
) as t2
ON t1.id = t2.related_id