PHP or MYSQL : how to group results by ROW?
i'm working on mys开发者_运维技巧ql 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 ?
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.
精彩评论