简体   繁体   中英

PHP or MYSQL : how to group results by ROW?

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM