views:

25

answers:

1

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_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

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

+1  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
VolkerK
@VolkerK Thanks! it works.
jartaud