table 1
--------
pid | name
1 someone
2 another
table2
--------
bid | valu
1 drum
2 guitar
3 flower
4 cake
table3
------
id | pid | bid | pref
1 1 3 yes
2 1 1 maybe
3 1 2 no
4 2 4 definately
5 2 2
6 2 3 no
因此,您可以看到我有3个简单的表,其中第三个表用于创建表1和表2之间的映射以及一些其他数据。现在我需要编写一个查询,根据valu
,
pref
和pid
因此,对pid = 1
,预期的输出是这样的
flower yes, drum maybe, guitar no
....所以如何编写此查询?
我试过(几乎是盲目的猜测):
SELECT opa.name, GROUP_CONCAT(CONCAT(opb.valu,' ',opc.pref) SEPARATOR ',') AS myChoice
From
table_1 opa
INNER JOIN table_3 opc ON opc.pid = opa.pid
INNER JOIN table_2 opb ON opb.bid = opc.bid
感谢任何帮助。
答案 0 :(得分:2)
您的查询是对的,您忘记了GROUP BY
SELECT opa.name, GROUP_CONCAT(CONCAT(opb.valu,' ',opc.pref) SEPARATOR ',') AS myChoice
From
table1 opa
INNER JOIN table3 opc ON opc.pid = opa.pid
INNER JOIN table2 opb ON opb.bid = opc.bid
group by opc.pid