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.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Displaying the query result in one row

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-02-04, 05:14
SamKlinsmann SamKlinsmann is offline
Registered User
 
Join Date: Dec 2003
Posts: 14
Question 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
Reply With Quote
  #2 (permalink)  
Old 12-02-04, 07:51
andrewst andrewst is offline
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.
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #3 (permalink)  
Old 12-02-04, 10:21
r937 r937 is offline
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
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 12-02-04, 10:54
andrewst andrewst is offline
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.
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #5 (permalink)  
Old 12-02-04, 10:58
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by andrewst
... or Oracle 9i like this.
WTF OMG LOL

that's horrible!!

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 12-02-04, 11:22
andrewst andrewst is offline
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).
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #7 (permalink)  
Old 12-02-04, 14:35
SamKlinsmann SamKlinsmann is offline
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
Reply With Quote
  #8 (permalink)  
Old 12-02-04, 15:02
r937 r937 is offline
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
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 12-03-04, 01:32
SamKlinsmann SamKlinsmann is offline
Registered User
 
Join Date: Dec 2003
Posts: 14
Thumbs up

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
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On