Php 从左连接mysql获取两列中的两行

Php 从左连接mysql获取两列中的两行,php,mysql,Php,Mysql,我有两个表,下面给出了它们的结构和数据 table1 id, name 1, abc 2, xyz table2 id, table1_id, type, other_name 1, 1, 1, hello 2, 1, 2, world 3, 2, 1, wonder 4, 2, 2, this world 需要具有以下列的行的结果: table1.id, table1,name, table2.other_name where table2.type=1, table2.other_na

我有两个表,下面给出了它们的结构和数据

table1
id, name
1, abc
2, xyz


table2
id, table1_id, type, other_name
1, 1, 1, hello
2, 1, 2, world
3, 2, 1, wonder
4, 2, 2, this world
需要具有以下列的行的结果:

table1.id, table1,name, table2.other_name where table2.type=1, table2.other_name where table2.type=2
我试过:

SELECT
table1.id, table1,name,
IF(table2.type=1, table2.other_name,NULL) AS current_name,
IF(table2.type=2, table2.other_name,NULL) AS previous_name
FROM table1
LEFT JOIN table2 ON table2.table1_id = table1.id
GROUP BY table1.id;
但它就像:

1, abc, hello, NULL
2, xyz, wonder, NULL
鉴于我希望得到如下结果:

1, abc, hello, world
2, xyz, wonder, this world

请帮帮我

您应该使用聚合函数,该函数使用“分组依据”处理多行,也可以将table1.name移动到“分组依据”下


你想做什么?您使用的是“group by”子句,但表1中没有聚合函数。名称、当前名称、以前的名称能否请您解释一下我应该使用哪一个来获得所需的结果。
SELECT table1.id, 
       table1.name,
       max(IF(table2.type=1, table2.other_name, NULL)) AS current_name,
       max(IF(table2.type=2, table2.other_name, NULL)) AS previous_name
FROM table1
LEFT JOIN table2 ON table2.table1_id = table1.id
GROUP BY table1.id, table1.name;