2010-07-31 154 views
0

你好,任何人都可以看到爲什麼這個查詢失敗?幫助加入查詢(MySQL)

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廣告A.

+0

它怎麼會失敗呢?我想這只是一些例子,因爲這些連接似乎已經過時了。 – Bobby 2010-07-31 22:12:09

+1

你是什麼意思失敗?它給出了一個錯誤?或者你期待那些沒有被返回的列?如果是這樣的列和哪些表? – 2010-07-31 22:12:24

+0

@馬丁史密斯,在第一個,從廣告A,它不選擇加入列 – jartaud 2010-07-31 22:17:35

回答

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 
+0

謝謝!有用。 – jartaud 2010-07-31 22:24:39