开发者

Help with a joined query (MySQL)

开发者 https://www.devze.com 2023-01-09 22:36 出处:网络
Hello can anybody see why this query fails? SELECTA.idAd, A.ads_in_Cat, A.title, A.currency, A.price,

Hello can anybody see why this query fails?

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

But this one works:

SELECT *FROM ads 
LEFT JOIN featured ON featured.ad = ads.idAd
INNER JOIN dept ON dept.id_dept = ads.in_dpt
INNER JOIN sub_c开发者_如何学编程at_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

In the first one, I don't want all columns from the table "ads", the query returns only columns till ...FROM ads A.


If you specify to only select fields from A that's what you get: Only fields from A.
If you want fields from other tables too you have to specify them as well.

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
0

精彩评论

暂无评论...
验证码 换一张
取 消