左连接的正确语法
问题描述:
有人能告诉我为什么这个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
答
与你陈述的问题是你有FROM
子句前额外的逗号,
SELECT t_ads.*, t_adpics.picfile, FROM
^remove this comma
+1
这真的很无聊,我没注意到 – user481913
答
你必须把情况和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
+0
+1感谢发现 – user481913
有你'SELECT'语句后面的逗号,没有什么帮助。 –