1

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.

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

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

Any clue ?

2
  • 1
    Such generic design is a database design one would avoid if not really, really necessary. Maybe you should change your design, so writing queries would be much easier. Commented May 13, 2015 at 12:45
  • @ThorstenKettner I agree Commented May 13, 2015 at 12:49

2 Answers 2

4

Try to use OR operator as below

select *
from tab
where (fieldname = 'country' and  fieldvalue = 'USA')
or (fieldname = 'language' and  fieldvalue = 'English')
Sign up to request clarification or add additional context in comments.

8 Comments

Yes, but the issue is it would return either country is USA or language is English, right ?
@nope, this returns the items if either field name is there or fieldvalue.
@user2475624 What do you expect in result?
@user2475624 So it's totally different problem than you asked. I need to see a query than I can help you.
@user2475624: No, you shouldn't continue in chat. You've asked a question and got an answer, and you should accept it. As Parado just said, the question you are about to ask now is completely different from the question at hand, so you should better ask a new question in a new request.
|
2

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.