好的,所以我有两个表,有很多列,但为了便于说明,我有a
和b
:
a: b:
+---+-----+ +---+------+-----+
|id |name | |id | a_id |size |
+---+-----+ +---+------+-----+
我想要的是SELECT * FROM a
,然后是a'
中的每个a
,我想要SELECT id FROM b WHERE a_id = a'.id
。但是,我可以创建一个单一的治疗,结果变成:
ab:
+---+-----+--------------------------------+
|id |name | b_idx_1, b_idx_2, ... b_idx_n |
+---+-----+--------------------------------+
我试过了:
SELECT id, name, (SELECT id FROM b WHERE b.a_id = a.id) FROM a WHERE a.name LIKE 'Random Name'
然后我得到一个错误,子查询返回多行。
答案 0 :(得分:1)
SELECT a.id, a.name, GROUP_CONCAT(b.id)
FROM a
INNER JOIN b
ON b.a_id = a.id
WHERE a.name LIKE 'Random Name'
答案 1 :(得分:0)
试试这个join query
select a.id,a.name,b.* from a join b on a.id=b.a_id