0

Table_Aentity_id

Table_B有一个相关的entity_idattribute_id = 1

Table_B有一个相关的entity_idattribute_id = 2

  SELECT Table_B.value
    FROM  Table_A
    LEFT JOIN Table_B
    ON Table_A.entity_id = Table_B.entity_id
    WHERE Table_A.date =  '2012-7-30 00:00:00'
    AND Table_B.attribute_id = 1

我想将attribute_id = 1 和attribute_id = 2 放入一个查询中。

4

3 回答 3

1

您可以再次加入 Table_B 并使用 AS 给它一个新名称(别名)。

SELECT Table_B.value, Table_B2.value
FROM  Table_A
LEFT JOIN Table_B
ON Table_A.entity_id = Table_B.entity_id
LEFT JOIN Table_B AS Table_B2
ON Table_A.entity_id = Table_B2.entity_id
WHERE Table_A.date =  '2012-7-30 00:00:00'
AND Table_B.attribute_id = 1
AND Table_B2.attribute_id = 2

您还可以移动属性标准以使其更清楚您的目标:

SELECT Table_B.value, Table_B2.value
FROM  Table_A
LEFT JOIN Table_B
ON Table_A.entity_id = Table_B.entity_id AND Table_B.attribute_id = 1
LEFT JOIN Table_B AS Table_B2
ON Table_A.entity_id = Table_B2.entity_id AND Table_B2.attribute_id = 2
WHERE Table_A.date =  '2012-7-30 00:00:00'
于 2012-07-30T22:16:08.697 回答
1

好吧,您可以通过两种简单的方法简单地做到这一点

select 
    t1.entity_id,
    t2.attribute
from t1
left join t2 on t2.entity_id = t1.entity_id;

或者

select 
    t1.entity_id,
    group_concat(t2.attribute) as Attr
from t1
left join t2 on t2.entity_id = t1.entity_id
group by t1.entity_id
于 2012-07-31T03:23:34.870 回答
0

如果您想同时获得attribute_id = 1 和2,为什么不使用OR 作为条件呢?

SELECT Table_B.value
FROM  Table_A
LEFT JOIN Table_B
ON Table_A.entity_id = Table_B.entity_id
WHERE Table_A.date =  '2012-7-30 00:00:00'
AND (Table_B.attribute_id = 1 OR Table_B.attribute_id = 2)
于 2012-07-31T04:20:42.113 回答