sql 在列上加入多个条件

sql join with multiple conditions on columns

我认为我无法通过工会实现这一目标。 例如,我想要所有键列为“品牌”且值列为“Burago”的汽车。 最重要的是,key-column price 的值在 100 到 220 之间。

我应该做子查询什么的吗?如果我也想查询值为“sport”的键“type”怎么办? 当我使用一个 where 子句时,查询仅 returns 一些内容,而使用以下查询时 returns 什么也没有。

select `posts`.`title` from `posts`
  inner join `meta` on `posts`.`id` = `meta`.`metable_id`
  where `meta`.`key` = "price" and `meta`.`value` between 100 and 220
   and `meta`.`key` = "brand" and `meta`.`value` = "Burago"
 group by `posts`.`id`

元 table:

ID  | post_id |   key     |       value
----------------------------------------------------
69  |   8     |   brand   | some-brand
----------------------------------------------------
70  |   8     |   type    | sport
----------------------------------------------------
70  |   8     |   price   | 100
----------------------------------------------------
71  |   8     |   brand   | some-other-brand
----------------------------------------------------
70  |   8     |   type    | coupe
----------------------------------------------------
72  |   8     |   price   | 150
----------------------------------------------------
73  |   8     |   brand   | some-brand
----------------------------------------------------
70  |   8     |   type    | cabrio
----------------------------------------------------
74  |   8     |   price   | 100
----------------------------------------------------
75  |   8     |   brand   | some-brand
----------------------------------------------------
70  |   8     |   type    | sport
----------------------------------------------------
76  |   8     |   price   | 250
select `posts`.`title` from `posts`
  inner join `meta` on `posts`.`id` = `meta`.`metable_id`
  where `meta`.`key` = "price" and `meta`.`value` between 100 and 220
   and `meta`.`key` = "brand" and `meta`.`value` = "Burago"
 group by `posts`.`id`
having COUNT(DISTINCT `meta`.`key`) = 2