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 > General > Database Concepts & Design > help me modify sql query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-09-10, 06:36
_db _db is offline
Registered User
 
Join Date: Mar 2010
Posts: 7
help me modify sql query

I'm back and need more help

When I run this query
Code:
SELECT persons.personID, firstname, lastname, lecture, there 
FROM persons, lectures, person_lectures
WHERE  person_lectures.personID = persons.personID 
		AND person_lectures.lectureID = lectures.lectureID
		AND persons.personID = 1"
I get this:

Code:
+----------+-----------+----------+---------+-------+
| personID | firstname | lastname | lecture | there |
+----------+-----------+----------+---------+-------+
|    1	   |    Jack   |   Ryan   |    L1   |  YES  |
+----------+-----------+----------+---------+-------+
|    1	   |    Jack   |   Ryan   |    L2   |  YES  |
+----------+-----------+----------+---------+-------+
|    1	   |    Jack   |   Ryan   |    L3   |   NO  |
+----------+-----------+----------+---------+-------+
but what I really want is this
Code:
+----------+-----------+----------+---------+---------+---------+
| personID | firstname | lastname |    L1   |    L2   |    L3   |
+----------+-----------+----------+---------+---------+---------+
|    1     |   Jack    |   Ryan   |   YES   |   YES   |    NO   |
+----------+-----------+----------+---------+---------+---------+
do you have any idea how to do this?

Persons:

Code:
CREATE TABLE  persons
( personID TINYINT NOT NULL AUTO_INCREMENT PRIMARY KEY 
, firstname VARCHAR( 30 ) NOT NULL 
, lastname VARCHAR( 30 ) NOT NULL
) ENGINE = INNODB;
Lectures:

Code:
CREATE TABLE  lectures 
( lectureID TINYINT NOT NULL AUTO_INCREMENT PRIMARY KEY 
, lecture VARCHAR( 3 ) NOT NULL
, there VARCHAR( 3 ) NOT NULL
) ENGINE = INNODB;
PersonsLectures:

Code:
CREATE TABLE person_lectures 
( personID TINYINT NOT NULL
, lectureID TINYINT NOT NULL 
, PRIMARY KEY ( personID, lectureID )
, INDEX person_lectures ( lectureID, personID )
, FOREIGN KEY ( personID ) REFERENCES persons ( personID )
, FOREIGN KEY ( lectureID ) REFERENCES lectures ( lectureID )
) ENGINE = INNODB;
Reply With Quote
  #2 (permalink)  
Old 06-09-10, 07:08
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Code:
SELECT persons.personID
     , persons.firstname
     , persons.lastname
     , MAX(CASE WHEN lecture='L1' THEN there ELSE NULL END) AS L1
     , MAX(CASE WHEN lecture='L2' THEN there ELSE NULL END) AS L2
     , MAX(CASE WHEN lecture='L3' THEN there ELSE NULL END) AS L3
  FROM persons
INNER
  JOIN person_lectures
    ON person_lectures.personID = persons.personID
INNER
  JOIN lectures
    ON lectures.lectureID = person_lectures.lectureID 
 WHERE persons.personID = 1
GROUP
    BY persons.personID
notice that the GROUP BY clause has only 1 column, but the SELECT clause has 2 additional non-aggregate columns -- you are allowed to do this only in mysql, where it not only works but can be a bit more efficient than including the extra columns in the GROUP BY

i knew you were using mysql from the AUTO_INCREMENT
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 06-09-10, 11:53
_db _db is offline
Registered User
 
Join Date: Mar 2010
Posts: 7
You're right about MYSQL

thanks a lot for your help

Last edited by pootle flump; 06-14-10 at 05:36.
Reply With Quote
  #4 (permalink)  
Old 06-14-10, 05:16
_db _db is offline
Registered User
 
Join Date: Mar 2010
Posts: 7
I ran into a bit of a snag

I want to include all of the persons, whether they were on a lecture or not, so when I tried to modify your line

Code:
 WHERE persons.personID = 1
and changed it into

Code:
 WHERE persons.personID BETWEEN 1 AND 100
I only got data for the persons whose presence on a lecture I explicitly set to "Yes" or "No". All the other persons who are in the table persons were not displayed, and I would want them too.

Can you help me mend this?

* I chose 100 because there will never be 100 persons in my table, but also because I didnt get any results with this line
Code:
 WHERE persons.personID IN persons
Reply With Quote
  #5 (permalink)  
Old 06-14-10, 05:37
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Please do not slip links to nonsense in to your posts in technical threads.
Reply With Quote
  #6 (permalink)  
Old 06-14-10, 05:50
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
If you want all people, remove the WHERE clause entirely.
To have everyone display in the query irrespective of their attendance in lectures, change your join to LEFT OUTER JOINS.
Reply With Quote
  #7 (permalink)  
Old 06-14-10, 09:56
_db _db is offline
Registered User
 
Join Date: Mar 2010
Posts: 7
Sorry about the link to youtube, I was just overjoyed when I saw the solution.

Thanks for the advice
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