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 > Database Server Software > MySQL > Subquery in Select clause

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-19-10, 14:55
bla4free bla4free is offline
Registered User
 
Join Date: Jan 2005
Posts: 165
Subquery in Select clause

What I'd like to do are get results like this:

Code:
AGENCY_NUM | DEFENDANTS
-----------+---------------
2010-66096 | Jones, Smith, Wolfe
Here is my data:

Code:
CASE TABLE
id | agency_num
---+-----------
1  | 2010-44570
2  | 2010-40569
3  | 2010-66096

CASE_RELATIONSHIPS TABLE
case_id | people_id
--------+----------
3       | 15
3       | 16
3       | 23

PEOPLE TABLE
id | last_name
---+----------
15 | Smith
16 | Jones
23 | Wolfe
This is the query I have, but I get an error saying my subquery returns more than one row.

Code:
SELECT agency_num, (SELECT people.last_name FROM people) AS defendants

FROM case LEFT JOIN  case_relationships ON case.id = case_relationships.case_id

ORDER BY agency_num
Is this type of query possible? Maybe a subquery is not the right method to do this. Thanks!
Reply With Quote
  #2 (permalink)  
Old 07-19-10, 15:18
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
try recursive query or pivot, you should find what you seek.
Dave
Reply With Quote
  #3 (permalink)  
Old 07-19-10, 16:01
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Code:
SELECT agency_num
     , GROUP_CONCAT(people.last_name) AS defendants
  FROM case 
LEFT OUTER
  JOIN case_relationships 
    ON case_relationships.case_id = case.id 
LEFT OUTER
  JOIN people
    ON people.id = case_relationships.people_id
GROUP
    BY agency_num
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 07-19-10, 16:32
bla4free bla4free is offline
Registered User
 
Join Date: Jan 2005
Posts: 165
Quote:
Originally Posted by r937 View Post
Code:
SELECT agency_num
     , GROUP_CONCAT(people.last_name) AS defendants
  FROM case 
LEFT OUTER
  JOIN case_relationships 
    ON case_relationships.case_id = case.id 
LEFT OUTER
  JOIN people
    ON people.id = case_relationships.people_id
GROUP
    BY agency_num
That works perfectly! I think all I really needed was to know about the GROUP_CONCAT function. I've never heard of that before but it looks like I can use it in several places now. Thanks a lot!
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