2011-06-01 182 views
2

我終於得到了我寫的所有問題(感謝所有幫助過我的人)。加入2個MySQL查詢

第一個:

SELECT 
    cards.card_id, 
    concat(cards.title, ' By Amy') AS TitleConcat, 
    cards.meta_description, 
    cards.description, 
    '' as Bob, 
    '' as Weight, 
    '' as UPC, 
    cards.seo_keywords, 
    concat('http://www.amyadele.com/attachments//cards/',cards.card_id,'/',cards.card_image) AS ImageURL, 
    card_import.artist, 
    concat('ARTIST - ', card_import.artist) AS Brand, 
    min(card_lookup_values.card_price) AS LowPrice, 
replace(lower(concat('http://www.amyadele.com/', pcat.seoname,'/',cat.seoname, '/', cards.seoname, '.htm')),' ','+') AS link, 
      concat(pcat.name,'>',cat.name) as Merchant 


FROM 
    cards 
    join card_cheapest on cards.card_id = card_cheapest.card_id 
    left join card_import on card_import.card_id = cards.card_id 
    join card_lookup_values on card_lookup_values.card_id = cards.card_id 
    INNER JOIN card_categories cc ON cards.card_id = cc.card_id AND cards.card_live = 'y' AND cards.active = 'y' AND cc.active = 'Y' 
      INNER JOIN categories cat ON cat.category_id = cc.category_id AND cat.active = 'Y' 
      INNER JOIN categories pcat ON cat.parent_category_id = pcat.category_id 


WHERE card_lookup_values.card_price > 0 
GROUP BY 
    cards.card_id 
ORDER BY 
    cards.card_id     

而且第二個:

SELECT cards.card_id, round(min(card_lookup_values.card_price), 2) AS 'price', min(cast(lookup_details.value as signed)) as 'quantity' 
FROM cards 
INNER JOIN card_lookup_values ON cards.card_id = card_lookup_values.card_id 
INNER JOIN lookup_details ON card_lookup_values.lookup_detail_id = lookup_details.lookup_detail_id 
WHERE card_lookup_values.lookup_id = 7 
-- AND c.card_id = 'al007' 
GROUP BY cards.card_id; 

我試過幾次來得到這個工作(我最後一次嘗試)

SELECT 
    cards.card_id, 
    concat(cards.title, ' By Amy') AS TitleConcat, 
    cards.meta_description, 
    cards.description, 
    '' as Bob, 
    '' as Weight, 
    '' as UPC, 
    cards.seo_keywords, 
    concat('http://www.amyadele.com/attachments//cards/',cards.card_id,'/',cards.card_image) AS ImageURL, 
    card_import.artist, 
    concat('ARTIST - ', card_import.artist) AS Brand, 
    min(card_lookup_values.card_price) AS LowPrice, 
replace(lower(concat('http://www.amyadele.com/', pcat.seoname,'/',cat.seoname, '/', cards.seoname, '.htm')),' ','+') AS link, 
      concat(pcat.name,'>',cat.name) as Merchant, 
round(min(card_lookup_values.card_price), 2) AS 'price', 
min(cast(lookup_details.value as signed)) as 'quantity' 

FROM 
    cards 
    join card_cheapest on cards.card_id = card_cheapest.card_id 
    left join card_import on card_import.card_id = cards.card_id 
    join card_lookup_values on card_lookup_values.card_id = cards.card_id 
    INNER JOIN card_categories cc ON cards.card_id = cc.card_id AND cards.card_live = 'y' AND cards.active = 'y' AND cc.active = 'Y' 
      INNER JOIN categories cat ON cat.category_id = cc.category_id AND cat.active = 'Y' 
      INNER JOIN categories pcat ON cat.parent_category_id = pcat.category_id 
INNER JOIN card_lookup_values ON cards.card_id = card_lookup_values.card_id 
INNER JOIN lookup_details ON card_lookup_values.lookup_detail_id = lookup_details.lookup_detail_id 

WHERE card_lookup_values.card_price > 0 and where card_lookup_values.lookup_id = 7 
GROUP BY 
    cards.card_id 
ORDER BY 
    cards.card_id     

但我不斷收到一個錯誤,指出:不是唯一的表/別名:「card_lookup_values」

有誰知道我做錯了嗎?

回答

1

您加入了兩次card_lookup_values表。您必須爲每個引用分配一個唯一的別名來區分它們。我在下面的代碼片段中使用了clv1clv2來說明。

... 
join card_lookup_values clv1 on clv1.card_id = cards.card_id 
INNER JOIN card_categories cc ON cards.card_id = cc.card_id AND cards.card_live = 'y' AND cards.active = 'y' AND cc.active = 'Y' 
     INNER JOIN categories cat ON cat.category_id = cc.category_id AND cat.active = 'Y' 
     INNER JOIN categories pcat ON cat.parent_category_id = pcat.category_id 
INNER JOIN card_lookup_values clv2 ON cards.card_id = clv2.card_id 
... 
+0

哦。我認爲我也需要將我的card_lookup_values實例更改爲clv1?我發現在進行更改後,我收到錯誤Unknown table card_lookup_values – 2011-06-01 13:26:39

+0

@Louis:是的。在你引用這張表的列的任何地方,你都需要使用適當的別名。 – 2011-06-01 13:29:04