简体   繁体   English

加入 2 个 MySQL 查询

[英]Joining 2 MySQL Queries

I finally got all of my queries written (thanks to all who have helped me).我终于把所有的问题都写好了(感谢所有帮助过我的人)。

The First One:第一个:

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                 

And the Second One:第二个:

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;

I tried a couple of times to get this to work (my last attempt)我尝试了几次以使其正常工作(我的最后一次尝试)

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                 

But I keep getting an error that says: Not unique table/alias: 'card_lookup_values'但我不断收到一条错误消息: Not unique table/alias: 'card_lookup_values'

Does anyone know what I am doing wrong?有谁知道我做错了什么?

You're joining twice to the card_lookup_values table.您将两次加入card_lookup_values表。 You must assign each reference to this table a unique alias to distinguish them.您必须为对该表的每个引用分配一个唯一别名以区分它们。 I've used clv1 and clv2 in the snippet below to illustrate.我在下面的代码片段中使用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
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM