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 04-02-2009, 08:24 AM   PM User | #1
xiaodao
Regular Coder

 
Join Date: Sep 2004
Posts: 713
Thanks: 6
Thanked 2 Times in 2 Posts
xiaodao is an unknown quantity at this point
What is wrong with sql

Hi

what is wrong with this sql

Code:
SELECT series.* FROM series LEFT JOIN (select rating.* from rating WHERE rating.USER_USERID=51) ON series.SERIESID=rating.SERIES_SERIESID WHERE series.SERIESID=1
the result should return records of Series with NULL rating if the rating table does not have the matching series_seriesid
__________________
flying dagger
xiaodao is offline   Reply With Quote
Old 04-02-2009, 08:56 AM   PM User | #2
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,293
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
Change these
Code:
series.*
to this
Code:
*
Do it for the other one you have too.
__________________
||||If you are getting paid to do a job, don't ask for help on it!||||
_Aerospace_Eng_ is offline   Reply With Quote
Old 04-02-2009, 10:13 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,999 Times in 3,968 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
No reason to use an inner SELECT here.

Just need to make sure that you code the ON condition correctly!

Code:
SELECT explicit, list, of, fields 
FROM series LEFT JOIN rating 
ON ( rating.USER_USERID=51 AND series.SERIESID=rating.SERIES_SERIESID )
WHERE series.SERIESID=1
And I *DISAGREE* with Aerospace. You should avoid doing SELECT * like the plague! Always only SELECT the actual fields you need. There's a LOT of overhead involved in using SELECT * in all too many circumstances.
Old Pedant is offline   Reply With Quote
Old 04-03-2009, 03:09 PM   PM User | #4
bdl
Regular Coder

 
Join Date: Apr 2007
Location: Camarillo, CA US
Posts: 590
Thanks: 4
Thanked 83 Times in 82 Posts
bdl is an unknown quantity at this point
Well, what _Aerospace_Eng_ was getting at, I believe, is the fact that this statement is at least redundant if not problematic in and of itself:
Code:
SELECT series.* FROM series
You should simply change the series.* to just *. Same thing with ratings.*. Another problem comes into play when you have two tables with repeated field names using SELECT ALL ('*').

That's not the fix to the statement, but you get the idea.


Yes, the SELECT ALL statement should be avoided, absolutely.
bdl is offline   Reply With Quote
Old 04-03-2009, 10:30 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,999 Times in 3,968 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
Code:
Well, what _Aerospace_Eng_ was getting at, I believe, is the fact that this statement is at least redundant if not problematic in and of itself:
Code:

SELECT series.* FROM series

You should simply change the series.* to just *
WHY?

There is NO DIFFERENCE AT ALL between
Code:
SELECT series.* FROM series
and
Code:
SELECT * FROM series
None. Not one iota.

There *IS* a difference if you were using mutiple tables:
Code:
SELECT series.* FROM series LEFT JOIN rating ...
Indeed, that would get ONLY the fields from the series table. Wheras
Code:
SELECT * FROM series LEFT JOIN rating ...
would be the same as doing
Code:
SELECT series.*, rating.* FROM series LEFT JOIN rating ...
Granted that using * is bad, but there is truly ZERO difference between
Code:
SELECT series.* FROM series LEFT JOIN (
       select rating.* from rating WHERE rating.USER_USERID=51) 
       ON series.SERIESID=rating.SERIES_SERIESID 
WHERE series.SERIESID=1
and the same code using just * in place of series.* and rating.*

The query is utterly bogus from the get-go because he LEFT JOINs to a sub-SELECT but then his ON clause refers to the ENTIRE rating table!

If he really wanted to use a sub-SELECT, he should have coded:
Code:
SELECT series.* FROM series LEFT JOIN (
       select rating.* from rating WHERE rating.USER_USERID=51) AS XXX
       ON series.SERIESID=XXX.SERIES_SERIESID 
WHERE series.SERIESID=1
But of course it's much better to just do the LEFT JOIN as I showed it.
Old Pedant is offline   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 08:28 PM.


Advertisement
Log in to turn off these ads.