从具有单个结果的两个表中读取特定值 - mysql

时间:2013-05-23 06:56:51

标签: mysql join union

我有两个表,表示清单和目录_Options

编目

listing_id
catalog_option_1
catalog_option_2
catalog_option_3

Catalog_Options

id
value

此处catalog_option_1, catalog_option_2, catalog_option_3包含映射到Catalog_Options表的id的id。我想读取所有3的值并将其显示在一行中,用于listing_id,例如list_1

3 个答案:

答案 0 :(得分:1)

试试这个

 SELECT * FROM `catalog_options` 
 where id in
 (SELECT catalog_option_1 FROM `listings` 
 union
 SELECT catalog_option_2 FROM `listings`
 union
 SELECT catalog_option_3 FROM `listings`)

答案 1 :(得分:0)

试试这个:

SELECT l.listing_id,c1.value as ca1,c2.value as ca2,c3.value as ca3 FROM listing l
   JOIN catalog_option c1 on c1.id = l.catalog_option_1
   JOIN catalog_option c2 on c2.id = l.catalog_option_2
   JOIN catalog_option c3 on c3.id = l.catalog_option_3;

答案 2 :(得分:0)

如果我理解正确,你需要试试这个:

SELECT
    GROUP_CONCAT(l.listing_id),
    GROUP_CONCAT(c.value)
FROM
    Listings l
LEFT JOIN Catalog_Options c
    ON c.id = RIGHT(l.listing_id,1)