0

Okay so I have two tables, with many columns, but for illustration purposes I have a and b:

a:           b:   
+---+-----+  +---+------+-----+
|id |name |  |id | a_id |size |
+---+-----+  +---+------+-----+

What I want is to SELECT * FROM a and then for each a' in a I want to SELECT id FROM b WHERE a_id = a'.id. But can I create a single curery such that the results becomes:

ab:
+---+-----+--------------------------------+ 
|id |name | b_idx_1, b_idx_2, ... b_idx_n  |
+---+-----+--------------------------------+ 

I tried: SELECT id, name, (SELECT id FROM b WHERE b.a_id = a.id) FROM a WHERE a.name LIKE 'Random Name' But then I get an error, with the sub-query returning more than one row.

Lars Nielsen
  • 2,005
  • 2
  • 25
  • 48

2 Answers2

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'
Jay Shankar Gupta
  • 5,918
  • 1
  • 10
  • 27
  • There is missing an `ON` in the query and could you give an explanation of how it works? Then I will accept this answer :) – Lars Nielsen Apr 06 '18 at 06:27
0

try this join query

select a.id,a.name,b.* from a join b on a.id=b.a_id
Bhargav Chudasama
  • 6,928
  • 5
  • 21
  • 39