检查同一列中的多个值

时间:2021-10-03 10:03:33

This is how my database table structure looks -

这就是我的数据库表结构的样子 -

ID fieldname fieldvalue
1   country   USA
2   language  English
3   country   India
4   language  Hindi

So I want to select the rows where the fieldname is say 'country' and fieldvalue is USA AND fieldname is language and fieldvalue is English.

所以我想选择fieldname为'country'的行,fieldvalue为USA,fieldname为language,fieldvalue为English。

Basically filtering by multiple values bases on key / value pairs.

基本上按键/值对的多个值进行过滤。

I've tried using the AND keyword but its returns nothing

我尝试过使用AND关键字,但它没有返回任何内容

Any clue ?

任何线索?

2 个解决方案

#1


Try to use OR operator as below

尝试使用OR运算符,如下所示

select *
from tab
where (fieldname = 'country' and  fieldvalue = 'USA')
or (fieldname = 'language' and  fieldvalue = 'English')

#2


A fieldname can't be both 'country' AND 'language' at the same time. But if you use OR, they are both true.

字段名称不能同时是“国家/地区”和“语言”。但如果你使用OR,它们都是正确的。

SELECT *
FROM table
WHERE ( fieldname = 'country' AND fieldvalue = 'USA' )
   OR ( fieldname = 'language' AND fieldvalue = 'English');

#1


Try to use OR operator as below

尝试使用OR运算符,如下所示

select *
from tab
where (fieldname = 'country' and  fieldvalue = 'USA')
or (fieldname = 'language' and  fieldvalue = 'English')

#2


A fieldname can't be both 'country' AND 'language' at the same time. But if you use OR, they are both true.

字段名称不能同时是“国家/地区”和“语言”。但如果你使用OR,它们都是正确的。

SELECT *
FROM table
WHERE ( fieldname = 'country' AND fieldvalue = 'USA' )
   OR ( fieldname = 'language' AND fieldvalue = 'English');