带有多个 where 子句的 MySQL 查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11920318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:28:59  来源:igfitidea点击:

MySQL query with multiple where clauses

mysql

提问by Shameer Rahman

I have a table wp_postmetawith columns called meta_keyand meta_value. I have 5 records in meta_key(location,area,price,bedrooms,bathrooms)

我有一个表wp_postmeta ,其中包含名为meta_keymeta_value 的列。我在meta_key 中有 5 条记录(位置、面积、价格、卧室、浴室)

screenshot of table here

表格截图在这里

For example, I want to find a hotel in Texas with 2 bathrooms:

例如,我想在德克萨斯州找到一家有 2 个浴室的酒店:

select post_id from wp_postmeta where meta_key = 'location' and meta_value = 'texas' and where meta_key = 'bathrooms' and meta_value= '2';

I know the above SQL command is not valid. Can anyone please help me to achieve the above result?

我知道上面的 SQL 命令无效。任何人都可以帮助我实现上述结果吗?

采纳答案by while1

You can try mysql subquery:

你可以试试mysql子查询:

select post_id 
from wp_postmeta 
where meta_key = 'location' and meta_value = 'texas' 
                  and post_id IN (select post_id 
                  from wp_postmeta 
                  where meta_key = 'bathrooms' and meta_value= '2')