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 08-12-2011, 01:21 PM   PM User | #1
LJackson
Senior Coder

 
Join Date: Jun 2008
Location: Cornwall
Posts: 1,973
Thanks: 289
Thanked 12 Times in 12 Posts
LJackson is on a distinguished road
Exclamation need help to modify my query, to set field names as 'something'

i have this query
PHP Code:
SELECT * , count( * ) AS howmany
        FROM tbl_popularity 
AS pop
        LEFT JOIN 
(
        
tbl_dvds AS dvd
        
ON pop.prodID dvd.filmID
        WHERE dvd
.filmBinding '$cSect' && pop.prodID <>&& pop.dateStamp DATE_SUBcurdate( ) , INTERVAL 30
        DAY 
)
        
GROUP BY `prodID`
        
ORDER BY howmany DESC
        LIMIT 0 

and on the join i need to select certain fields like i have in this query
PHP Code:
SELECT filmID as idfilmName as namefilmThumbIMG as imgfilmThumbHeight as heightfilmThumbWidth as widthfilmRRP as rrpfilmBinding as binding 
        FROM tbl_dvds 
i tried this
PHP Code:
SELECT * , count( * ) AS howmany
        FROM tbl_popularity 
AS pop
        LEFT JOIN 
(
        
SELECT filmID as idfilmName as namefilmThumbIMG as imgfilmThumbHeight as heightfilmThumbWidth as widthfilmRRP as rrpfilmBinding as binding FROM tbl_dvds AS dvd
        
ON pop.prodID dvd.filmID
        WHERE dvd
.filmBinding 'DVD' && pop.prodID <>&& pop.dateStamp DATE_SUBcurdate( ) , INTERVAL 30
        DAY 
)
        
GROUP BY `prodID`
        
ORDER BY howmany DESC
        LIMIT 0 

but it says Every derived table must have its own alias?

can i do that using the left join or do i need to use union? and will union enable me to product the same outputs as my first query?

thanks
Luke
__________________
Kernow Connect: Online Shopping, Price Comparison, Maximum Savings On Top UK Stores
Follow Us On: Twitter | Facebook
LJackson is offline   Reply With Quote
Old 08-12-2011, 08:37 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 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
It's just saying you need "... AS anyname " after each of your inner SELECTs.

You have your AS in the wrong place: As written, you are aliasing tbl_dvds to dvd. You need to alias the entire SELECT.

*PROBABLY* this:
Code:
SELECT * , count( * ) AS howmany
FROM tbl_popularity AS pop
LEFT JOIN (
        SELECT filmID as id, filmName as name, filmThumbIMG as img, filmThumbHeight as height, 
               filmThumbWidth as width, filmRRP as rrp, filmBinding as binding 
        FROM tbl_dvds ) AS dvd
ON pop.prodID = dvd.filmID AND dvd.filmBinding = 'DVD' 
WHERE pop.prodID <> 0 
AND pop.dateStamp > DATE_SUB( curdate( ) , INTERVAL 30  DAY )
GROUP BY `prodID`
ORDER BY howmany DESC
LIMIT 0 , 6
Notice that I moved your dvd.filmBinding = 'DVD' to the ON clause. If you put it in the WHERE clause, you just converted your LEFT JOIN into an INNER JOIN.

Didn't I show you that before? If not, look here:
http://www.codingforums.com/showthre...192#post818192

Also, I don't trust/like your GROUP BY. Normally, you should GROUP BY *every single field* in your SELECT except the aggregate fields (aggregates are the functions such as COUNT, AVG, SUM, etc. that work on a group of records). If you don't do that, you can't guarantee that your aggregate values actually go with the non-aggregate fields. Almost alone among DBs, MySQL allows you to omit fields from the GROUP BY, but then it gives oddball results.

n.b.: Rather than even having that one condition in the JOIN, why not put it in the inner SELECT, thus:
Code:
SELECT * , count( * ) AS howmany
FROM tbl_popularity AS pop
LEFT JOIN (
        SELECT filmID as id, filmName as name, filmThumbIMG as img, filmThumbHeight as height, 
               filmThumbWidth as width, filmRRP as rrp, filmBinding as binding 
        FROM tbl_dvds 
        WHERE filmBinding = 'DVD' ) AS dvd
ON pop.prodID = dvd.filmID  
WHERE pop.prodID <> 0 
AND pop.dateStamp > DATE_SUB( curdate( ) , INTERVAL 30  DAY )
GROUP BY `prodID`
ORDER BY howmany DESC
LIMIT 0 , 6
That's more likely to be more efficient.
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 03:51 PM.


Advertisement
Log in to turn off these ads.