如何LEFT JOIN三个表依次排序?

时间:2016-08-29 12:02:06

标签: php mysql left-join

我已经尝试了很多次,但是每次获取sql语法错误,所以我把它分成两个查询,但我希望它是一个。请帮助我.Follwing是代码:

$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']));

这是错误:

  

警告:您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   '是ON is.imageID = i.imageID WHERE show_in_gallery ='Y'AND   第1行的image_user ='8'ORD'

1 个答案:

答案 0 :(得分:0)

可能你想要什么?不完全确定你的尝试失败的原因。

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