MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 3rd, 2013, 11:21 PM
kazuking kazuking is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Location: NY
Posts: 6 kazuking User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 28 sec
Reputation Power: 0
How to combine multiple results into one

Hello,
I searched this forum but I am not sure the right words to describe the problem and so I have not been successful in seeing if this has been answered before. That being said I have a question about a selection that would return multiple rows. I want to combine the result into one result.

Let me give you an example:
Let's say I have 3 tables.
Code:

      Student                    Class                   RelStudentClass 
+----------+-------+     +----------+-----------+   +-----------+---------+ 
|studentID | Name  |     |  classID | className |   | studentID | classID | 
+----------+-------+     +----------+-----------+   +-----------+---------+ 
|    01    | Mike  |     |     01   | Math 1    |   |    01     |    02   | 
|    02    | John  |     |     02   | English 1 |   |    01     |    03   | 
|    03    | Susan |     |     03   | Science 1 |   |    02     |    01   | 
|    04    | Mary  |     |     04   | Math 2    |   |    02     |    02   | 
|     .    |   .   |     |     05   | English 2 |   |    02     |    03   | 
|     .    |   .   |     |     .    |     .     |   |    03     |    04   | 
|     .    |   .   |     |     .    |     .     |   |     .     |     .   | 
etc.


and then I do a normal query like so....
Code:
SELECT st.Name, cl.className 
FROM Student st 
INNER JOIN RelStudentClass rsc ON st.studentID = rsc.studentID 
LEFT JOIN Class cl on cl.classID = rsc.classID


It would return something like this.
Code:

Mike     English 1
Mike     Science 1
John     Math 1
John     English 2
John     English 3


NOW here is the question.
Is there a way to change the MySQL query to produce something like this?
Code:
NAME          Math 1    English 1    Science 1    ....
Mike                       X             X
John            X          X             X
etc.


I'm guessing it could be done with if() statement? Like
if (cl.className='Math 1', 'y','n')

But if I do this for each class, then I would get multiple rows again. Is there a way? or do I have to go into PHP and create an array and handle it that way?
I would like to do it all in MySQL if possible.
Thank you.

Reply With Quote
  #2  
Old February 3rd, 2013, 11:54 PM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Lost in code
Click here for more information.
 
Join Date: Dec 2004
Posts: 7,939 E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 9 h 13 m 20 sec
Reputation Power: 7053
That is something you should do in your application code. As far as I know, there is no efficient or scalable way of writing a query that would do something like that.
__________________
PHP FAQ
How to program a basic, secure login system using PHP

Quote:
Originally Posted by Spad
Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

Reply With Quote
  #3  
Old February 4th, 2013, 01:44 AM
kazuking kazuking is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Location: NY
Posts: 6 kazuking User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 28 sec
Reputation Power: 0
Thanks, I was hoping there was a way......

I guess I will make something like

Make an double array[total student, total class]
while (student name is same)
add the class name to to total class



Quote:
Originally Posted by E-Oreo
That is something you should do in your application code. As far as I know, there is no efficient or scalable way of writing a query that would do something like that.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > How to combine multiple results into one

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap