1

有人能告诉我为什么这个 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

4

3 回答 3

4

你声明的问题是你在FROM子句之前有额外的逗号,

SELECT t_ads.*, t_adpics.picfile, FROM
                                ^  remove this comma
于 2013-04-18T12:49:46.933 回答
1

你需要删除,

改变

SELECT t_ads.*, t_adpics.picfile, FROM

SELECT t_ads.*, t_adpics.picfile FROM
于 2013-04-18T12:51:02.787 回答
1

您必须将条件 ANDt_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
于 2013-04-18T12:53:35.113 回答