Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > MySQL
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
MySQL General discussion about the MySQL database.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the MySQL section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 27th, 2007, 04:00 AM
Authorized User
 
Join Date: Oct 2006
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Got problem when combine 2 tables using left join

Hi,

I'm having problem with left join ...

Let's say i've these following tables

academic
ID | year | level | m_l
----------------------------------------------
147 | 2006 | dip | 20
148 | 2006 | dip | 12


course
ID | year | level | course_desc | m_l
----------------------------------------------
147 | 2006 | dip | k | 7
147 | 2006 | dip | b | 11


Hint
----
(1) see course table, select sum(m_l) from course where ID=147 and year=2006 and level='dip' = 18
(2) after query from (1), if user select ID=147, year=2006, level=dip, then m_l receiving 2 or less only (0,1,2).


What i've done shown as follow:-

SELECT a.ID, a.Year, a.Level, a.m_l - b.m_l [value_left_in_course]
FROM
academic a
LEFT JOIN
(SELECT SUM(m_l) [m_l] FROM course
where ID=147 and year=2006 and level='dip'
GROUP BY ID) b
ON a.ID = b.ID
where a.ID=147 and a.year=2006 and a.level='dip'

the result is
ID | year | level | value_left_in_course
----------------------------------------------
147 | 2006 | dip | 2

MY PROBLEM IS WHEN NO ROWS in course when ID=148, then i'm query as above the result is
ID | year | level | value_left_in_course
----------------------------------------------
147 | 2006 | dip | (null)


How to writing SQL to changing (null), then the result shown as follow?
ID | year | level | value_left_in_course
----------------------------------------------
147 | 2006 | dip | 12





Reply With Quote
  #2 (permalink)  
Old March 27th, 2007, 04:02 PM
Friend of Wrox
 
Join Date: Jul 2003
Location: South San Francisco, CA, USA.
Posts: 128
Thanks: 0
Thanked 1 Time in 1 Post
Default

wkm1925,
1st you need know on SELECT queries with WHERE clause you are filtering the row result of your query.
And for SELECT queries to filter an AGGREGATE FUNCTION you must filter it with HAVING clause.
SELECT a.ID As id, a.Year As yr, a.Level AS lev, SUM(a.m_l - b.m_l)
FROM academic a
LEFT OUTER JOIN course b
HAVING a.ID = 147
AND a.year = 2006
AND a.level ='dip'
GROUP BY id

HOPE IT WORKS.





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 Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
left outer join 2 or 3 tables in ms access keyvanjan Classic ASP Basics 2 February 2nd, 2006 06:42 PM
left outer join 2 or 3 tables in ms access keyvanjan SQL Server ASP 0 January 30th, 2006 03:33 AM
Why doesn't this LEFT JOIN work? harg7769 Access 2 April 13th, 2005 04:49 AM
left join msrnivas Classic ASP Databases 2 October 15th, 2004 07:37 AM
Oracle 8i inner join and left join problem puteri_84 Oracle 2 August 19th, 2004 07:14 AM



All times are GMT -4. The time now is 12:05 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.