左连接的正确语法

时间:2013-04-18 12:48:07

标签: mysql sql

有人可以告诉我为什么这个SQL查询不会执行吗?

SELECT t_ads.*, t_adpics.picfile, 
FROM t_ads 
LEFT JOIN t_adpics ON t_ads.adid = t_adpics.adid AND t_adpics.isevent = '0' 
LEFT JOIN t_featured ON t_ads.adid = t_featured.adid AND t_featured.adtype = 'A' 
WHERE (a.adtitle LIKE '%findandpost%' OR a.addesc LIKE '%findandpost%') 
AND a.enabled = '1' 
AND a.verified = '1' 
AND a.expireson >= NOW() 
AND a.paid <> '0' 
AND (t_featured.adid IS NULL OR t_featured.featuredtill < NOW()) 
GROUP BY t_ads.adtitle 
ORDER BY RAND() LIMIT 0, 50

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 'FROM t_ads LEFT JOIN t_adpics ON t_ads.adid = t_a' at line 4

3 个答案:

答案 0 :(得分:4)

你声明的问题是你在FROM子句

之前有额外的逗号
SELECT t_ads.*, t_adpics.picfile, FROM
                                ^  remove this comma

答案 1 :(得分:1)

您需要删除,

更改

SELECT t_ads.*, t_adpics.picfile, FROM

SELECT t_ads.*, t_adpics.picfile FROM

答案 2 :(得分:1)

您必须将条件AND t_featured.adtype = 'A'放在WHERE子句中,并在,关键字

之前删除FROM
SELECT t_ads.*, t_adpics.picfile
FROM t_ads LEFT JOIN t_adpics ON t_ads.adid = t_adpics.adid 
LEFT JOIN t_featured ON t_ads.adid = t_featured.adid 
WHERE (a.adtitle LIKE '%findandpost%' OR a.addesc LIKE '%findandpost%')
        AND a.enabled = '1' AND a.verified = '1' 
        AND a.expireson >= NOW() AND a.paid <> '0' 
        AND (t_featured.adid IS NULL OR t_featured.featuredtill < NOW())
        AND t_featured.adtype = 'A'
        AND t_adpics.isevent = '0'
GROUP BY t_ads.adtitle ORDER BY RAND() LIMIT 0, 50