尝试将子查询添加到连接查询

时间:2012-06-08 14:14:09

标签: mysql subquery

我有这样的查询

SELECT
    tbl_products.*,
    GROUP_CONCAT(tags.name)
FROM
    tbl_page_collections_products,
    (SELECT page_collection_name as name 
       FROM tbl_page_collections 
       LEFT JOIN tbl_pages ON tbl_page_collections.page_id = tbl_pages.page_id 
       WHERE tbl_pages.page_name LIKE '%friends%') tags
    LEFT JOIN tbl_page_collections 
      ON tbl_page_collections.page_collection_id = tbl_page_collections_products.colID 
    LEFT JOIN tbl_pages 
      ON tbl_page_collections.page_id = tbl_pages.page_id 
    LEFT JOIN tbl_products 
      ON tbl_products.product_id = tbl_page_collections_products.product
   WHERE
    tbl_pages.page_name LIKE '%friends%'

我得到的错误是Unknown column 'tbl_page_collections_products.colID in on clause但是当子查询不存在且该表中存在该列时,我没有收到该错误。

是否存在冲突?

1 个答案:

答案 0 :(得分:0)

tbl_page_collections_products不在子查询子句中。也许这就是你想要的:

SELECT
tbl_products.*,
GROUP_CONCAT(tags.name)
FROM
tbl_products,
(SELECT page_collection_name as name 
   FROM tbl_page_collections
   ,tbl_page_collections_products
   LEFT JOIN tbl_pages ON tbl_page_collections.page_id = tbl_pages.page_id 
   WHERE tbl_pages.page_name LIKE '%friends%') tags
LEFT JOIN tbl_page_collections 
  ON tbl_page_collections.page_collection_id = tbl_page_collections_products.colID 
LEFT JOIN tbl_pages 
  ON tbl_page_collections.page_id = tbl_pages.page_id 
LEFT JOIN tbl_products 
  ON tbl_products.product_id = tbl_page_collections_products.product
WHERE
 tbl_pages.page_name LIKE '%friends%'