Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-29-2013, 01:44 PM   PM User | #1
Ric
New to the CF scene

 
Join Date: Jan 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Ric is an unknown quantity at this point
Need help with query

Hello everyone!

I need help comming up with a query.

I have 3 tables: users, courses and results. The results table has 2 foreign keys (user_id and course_id).

The result I want to get is as follows:

All the data of the user (id, username, first_name, last_name) and then for each course I want the latest date of the result table if there is one and only when the status is either 'passed' or 'attended'. If there is no result, or the status is not 'passed' or 'attended' the result should say 'no result'.

Preferably it will look something like this:

Columns:[user id][username][first name][last name][course 1][course 2]
Example row:[1][jdoe][John][Doe][01-29-2012][no result]

I've been at it for many hours now, and I can't figure it out. If anyone has any ideas, I'd really appreciate it!

Thanks,

Ric
Ric is offline   Reply With Quote
Old 01-29-2013, 04:54 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,231
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
The problem: Unless you want to return the SAME NUMBER of courses for each user, you can't do this in SQL.

That is you could *NOT* get back:
[1][jdoe][John][Doe][01-29-2012]
[2][adam][Adam][And][12-31-2012][2-29-2012][3-31-2012]
because you can not have a different number of columns per row.

You *could* do this with a combination of SQL and PHP/JSP/ASP code.

Or you would get back a *list* of dates (say comma delimited) as a single column:
[1][jdoe][John][Doe][01-29-2012]
[2][adam][Adam][And][12-31-2012,2-29-2012,3-31-2012]

Though I don't understand how you will interpret that if you don't know what date goes with what course.

Now, in your example, you showed exactly *two* courses, 1 with "no result". If you want exactly two courses for each person, *that* we can do, although it's not trivial at all.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:24 AM.


Advertisement
Log in to turn off these ads.