| |
|
If this is your first visit, be sure to check out the FAQ by clicking the link above.
You may have to register before you can post: click the register link above to proceed.
To start viewing messages, select the forum that you want to visit from the selection below.
|
 |

12-02-04, 05:14
|
|
Registered User
|
|
Join Date: Dec 2003
Posts: 14
|
|
Displaying the query result in one row
|
|
Hi all,
I have two M:M tables, A and B, which are connected by a 3rd link table C.
Each row of A might be connected to 0 to 10 row(s) of B.
A (ID, AName)
C (A.ID, B.ID)
B (ID, Bname)
And when I query AName = ‘Smith’, normally, it returns:
Smith | Hat
Smith | Coat
Smith | Shoes
My question:
Is there a way to DISPLAY this result in ONE row without changing the tables’ structure?
Like -> Smith | Hat | Coat | Shoes
Any comment and help would be appreciated.
Thanks
Sam
|
|

12-02-04, 07:51
|
|
Moderator.
|
|
Join Date: Sep 2002
Location: UK
Posts: 5,171
|
|
Yes, but not using pure SQL. You will have to write application code to query the data order by ANAME and then keep concatenating the BNAME values together until the ANAME changes.
|
|

12-02-04, 10:21
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
|
|
|
|
Quote:
|
Originally Posted by SamKlinsmann
Is there a way to DISPLAY this result in ONE row without changing the tables’ structure?
Like -> Smith | Hat | Coat | Shoes
|
there sure is, but only in mysql version 4.1+, and sybase ase
|
|

12-02-04, 10:54
|
|
Moderator.
|
|
Join Date: Sep 2002
Location: UK
Posts: 5,171
|
|
Quote:
|
Originally Posted by r937
there sure is, but only in mysql version 4.1+, and sybase ase
|
... or Oracle 9i like this.
|
|

12-02-04, 10:58
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
|
|
Quote:
|
Originally Posted by andrewst
|
WTF OMG LOL
that's horrible!!

|
|

12-02-04, 11:22
|
|
Moderator.
|
|
Join Date: Sep 2002
Location: UK
Posts: 5,171
|
|
True, but at least Tom Kyte has done all the work for us. All we have to do is install his STRAGG function once, make it public, and from then on just use it, i.e.:
select deptno, stragg(ename)
from emp
group by deptno;
(But it would be neat if it came built-in to the DBMS I agree).
|
|

12-02-04, 14:35
|
|
Registered User
|
|
Join Date: Dec 2003
Posts: 14
|
|
Thanks a lot Tony and Rudy.
I thought there might be a SQL trick (!) to do that and I would put myself in disadvantage if I didn't ask you about that.
I will try this again when I start my application development. Any reference, link, or hint on this subject would be helpful for me. I am not even sure what such a process is called, "multi-column to one-row"?
Rudy, I was wondering if you could address the section of MySQL manual for doing that job in MySQL 4.1+ as you mentioned. (I am going to use MySQL 4.1.x and PHP)
Thanks again
Sam
|
|

12-02-04, 15:02
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
|
|
it's called "denormalization"
in mysql it's the GROUP_CONCAT function (see 12.9.1 GROUP BY (Aggregate) Functions)
using your tables A,B,C above, you would write:
Code:
select AName
, group_concat(BName separator ' | ') as BNames
from A
inner join C ...
group by AName
|
|

12-03-04, 01:32
|
|
Registered User
|
|
Join Date: Dec 2003
Posts: 14
|
|
Yes! That was what I was looking for.
Thank you very much Rudy. You solved my problem again. Your solution works very well. Especially, with that separator '|' you recommended, the result is very readable.
You are not only very knowledgeable but also an awesome teacher. I must enroll in your "online course SQL for Database-Driven Web Sites". I am sure I will learn a lot from you.
Regards,
Sam
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|