3

i'm working on mysql those are my tables

D
----------
id , name
----------
1  , d1
2  , d2
3  , d3

t
----------
id , name
----------
1  , t1
2  , t2
3  , t3

c
----------
id , d_id , t_id
----------
1  , 1    , 1
1  , 1    , 2
1  , 1    , 3
1  , 2    , 1
1  , 2    , 2
1  , 2    , 3
1  , 3    , 1
1  , 3    , 2
1  , 3    , 3

i want to produce a query to get result like this

-------------------------------------------
d.name , t.name , t.name , t.name
-------------------------------------------
d1     , t1     , t2     , t3
d2     , t1     , t2     , t3
d3     , t1     , t2     , t3

is that possible ?

Thanks

EDIT

if it's not possible, is there any idea on how to get it as an array or object using PHP ?

trrrrrrm
  • 11,362
  • 25
  • 85
  • 130

1 Answers1

1

No, dynamically generating columns in MySQL is not a good idea. You'd be better of just selecting the d.name - t.name pairs, and then creating a hash per d.name.

For example, Perl has a fetchall_hashref that could suit you. Other languages often have the same functionality built-in, but you didn't specify a programming language other than pure SQL.

Community
  • 1
  • 1
Konerak
  • 39,272
  • 12
  • 98
  • 118
  • that looks exactly as i want, do you have any idea how to do this in PHP ? – trrrrrrm Jun 24 '11 at 12:13
  • I'm not a PHP expert, but most people here on StackOverflow like PDO. [It seems to appear PDO supports this](http://www.php.net/manual/en/pdostatement.fetchall.php), but that might warrant another question (or an edit here). Offcourse there are always [workarounds](http://snipplr.com/view/1358/mysql-fetch-all/). – Konerak Jun 24 '11 at 12:17