table1
-------------
id pagetitle
1 x
2 y
3 z
-------------
table2
------------
id contentid value
1 1 img.jpg
2 1 someval
3 1 someval2
4 2 x
5 3 y
RESULTS SHOULD BE ONE BIG TABLE
-----------------------------------------------------------------------------
id pagetitle id(idfrom table2...) contentid value1 value2 value3
1 x 1 1 img.jpg someval someval2
2 y 4 2 x
3 z 5 3 y
-----------------------------------------------------------------------------
有兴趣获取每行的所有3个或更多值,没有重复
我在我的查询(示例)中查了这部分代码,但是我得到了table2中找到的每个值的重复行:
SELECT * FROM `table1` AS t1
INNER JOIN `table2` AS t2
ON t1.id = t2.contentid
感谢
答案 0 :(得分:1)
这是一个非常困难的答案,但如果您可以使用逗号分隔所有值的单个列,那么以下SQL语句应该为您做到:
SELECT t1.id, t1.pagetitle, t2.contentid, GROUP_CONCAT(value SEPARATOR ', ') as val
FROM table1 AS t1
INNER JOIN table2 AS t2 ON t1.id = t2.contentid
GROUP BY t1.id, t1.pagetitle, t2.contentid
这将导致类似下面的事情:
id, pagetitle, contentid, val
1 , x , 1 , someval2, img, someval
2 , y , 2 , x
3 , z , 3 , y
答案 1 :(得分:-1)
SELECT * FROM table1
AS t1
外联接table2
AS t2
ON t1.id = t2.contentid