SQL查询组合列值

时间:2012-11-04 10:46:14

标签: mysql sql

我真的不确定如何标题这个问题...想象一下,我们有一张如下表:

object | attribute | value
------------------------------
   7     country     Germany
   7     position    12
   7     points      12
   8     country     Germany
   8     position    10
   8     points      3

现在我想选择所有具有country germany且位置为12或5的对象标识符

我不知道如何编写此查询,是否可能?

2 个答案:

答案 0 :(得分:4)

加入您需要其他查询选项的次数:

SELECT t1.object
FROM table t1
    INNER JOIN table t2 ON t1.object = t2.object
WHERE t1.attribute = 'country' AND t1.value = 'Germany'
    AND t2.attribute = 'position' AND t2.value IN (12,5)

答案 1 :(得分:3)

你必须自己加入桌子:

SELECT *
FROM foo l
JOIN foo r ON l.object=r.object AND r.attribute='position'
WHERE l.attribute='country' AND l.value='Germany' AND r.value IN (5,12)