Joining two tables (through a link), one which may yield multiple rows, together into one result.

Posted by Eikern on Stack Overflow See other posts from Stack Overflow or by Eikern
Published on 2010-03-14T22:36:37Z Indexed on 2010/03/14 22:45 UTC
Read the original article Hit count: 220

Filed under:
|
|
|

Lets say I've got a table listing car brands or models:

Cars:

Id | Brand
-----------
1  | BMW
2  | Audi
3  | Volvo

And I've also got another table which links features.
Link:

Id | carid | featureid
-----------------------
1  | 1     | 1
2  | 1     | 2
3  | 2     | 2
4  | 3     | 1
5  | 3     | 2
6  | 3     | 3

And I've got the table listing the features.
Features:

Id | Feature
-----------
1  | A/C
2  | 4WD
3  | Heated seats

I want to list these results on my front page like this:

BMW

  • A/C
  • 4WD

Audi

  • 4WD

Volvo

  • A/C
  • 4WD
  • Heated seats

What's the best/most efficient way of doing this (using PHP and MySQL)?

© Stack Overflow or respective owner

Related posts about mysql

Related posts about join