简体   繁体   中英

How to LEFT JOIN three tables with order by?

I have tried many times but every time its getting sql syntax error so i splited it into two queries but i want it to be one.Please help me.Follwing is the code:

$pictures = $db->get_results(sprintf("SELECT imageID, image_name, image_date, username, userID 
            FROM images LEFT JOIN users ON images.image_user = users.userID 
            WHERE show_in_gallery = 'Y' AND image_user = '%d' 
            ORDER BY imageID DESC LIMIT 0, 24", $data['userID']));
$stats = $db->get_results(sprintf("SELECT STAT.imageID, STAT.image_views,STAT.unique_views,STAT.earnings 
        FROM image_stats as STAT LEFT JOIN images as IMG ON STAT.imageID = IMG.imageID 
        WHERE  image_user = '%d' 
        ORDER BY imageID DESC LIMIT 0, 24", $data['userID']));

And here is the error:

Warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'is ON is.imageID = i.imageID WHERE show_in_gallery= 'Y' AND image_user = '8' ORD' at line 1

Probably what you want? Not entirely sure why your attempt failed.

SELECT IMG.imageID, IMG.image_name, IMG.image_date,
    USR.username, USR.userID,
    STAT.image_views, STAT.unique_views, STAT.earnings 
FROM image_stats as STAT LEFT JOIN images as IMG 
    ON STAT.imageID = IMG.imageID
LEFT JOIN users as USR 
    ON IMG.image_user = USR.userID
WHERE  image_user = '%d' AND USR.show_in_gallery = 'Y'
ORDER BY imageID DESC LIMIT 0, 24

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM