帮助加入查询(MySQL)

时间:2010-07-31 22:08:03

标签: php mysql pdo left-join inner-join

你好,任何人都可以看到为什么这个查询失败了?

SELECT  A.idAd, A.ads_in_Cat, A.title, A.currency, A.price, 
A.in_dpt, A.description, A.featured FROM ads A
LEFT JOIN featured F ON F.ad = A.idAd
INNER JOIN dept D ON D.id_dept = A.in_dpt
INNER JOIN sub_cat_ad S ON S.id_sub_cat = A.ads_in_Cat
INNER JOIN cat_ad C ON C.idCat_ad = S.from_cat_ad 
ORDER BY A.featured DESC LIMIT :limit, :offset

但是这个有效:

SELECT *FROM ads 
LEFT JOIN featured ON featured.ad = ads.idAd
INNER JOIN dept ON dept.id_dept = ads.in_dpt
INNER JOIN sub_cat_ad ON id_sub_cat = ads.ads_in_Cat
INNER JOIN cat_ad ON idCat_ad = sub_cat_ad.from_cat_ad
ORDER BY featured DESC LIMIT :limit, :offset

在第一个中,我不希望表“广告”中的所有列,查询只返回列 ... FROM ads A。 < / p>

1 个答案:

答案 0 :(得分:1)

如果您指定只选择A中的字段,那么您只能从A中选择字段 如果你也想要其他表中的字段,你也必须指定它们。

SELECT
  A.idAd, A.ads_in_Cat, A.title, A.currency, A.price, A.in_dpt, A.description, A.featured,
  F.*,
  D.*,
  S.*,
  C.*
FROM ads A
LEFT JOIN featured F ON F.ad = A.idAd
INNER JOIN dept D ON D.id_dept = A.in_dpt
INNER JOIN sub_cat_ad S ON S.id_sub_cat = A.ads_in_Cat
INNER JOIN cat_ad C ON C.idCat_ad = S.from_cat_ad 
ORDER BY A.featured DESC LIMIT :limit, :offset