SQL : Query For Multiple Column Value Filter

Hi Guys,

If you want to apply multiple column value filter on the same table, you can use following SQL Query:

Lets consider the table structure and data as below:

Now, if you run the below query:

select DISTINCT t.user_id, fst.meta_value as First_Name , lst.meta_value as Last_Name
from test as t
inner join test as fst on fst.user_id = t.user_id
inner join test as lst on lst.user_id = t.user_id
inner join test as est on est.user_id = t.user_id
where fst.meta_key =’first_name’ and lst.meta_key =’last_name’ and est.meta_key =’empid’
order by t.user_id

You will get the below result:

That’s it! You are done.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s