Sajid anwar
Sajid anwar

Reputation: 1194

How to select record from same column but different value

I want to select a record from below table I have tried with this query

SELECT * FROM `wp_postmeta` WHERE meta_key = 'rating_average' and meta_key='rating_counter' 

Which is not working.

if I select with this then working

SELECT * FROM `wp_postmeta` WHERE meta_key = 'rating_average'

I want both meta_key = 'rating_average' and meta_key='rating_counter'

enter image description here

Upvotes: 1

Views: 67

Answers (3)

Try IN Query:

`SELECT * 
FROM `wp_postmeta` 
WHERE meta_key IN ('rating_average','rating_counter')

Upvotes: 1

zakhefron
zakhefron

Reputation: 1443

You can achieve your desired output using below methods

1) Using OR

SELECT * 
FROM `wp_postmeta` 
WHERE meta_key = 'rating_average' OR meta_key='rating_counter';

2) Using IN

SELECT *
FROM `wp_postmeta`
WHERE meta_key IN ('rating_average', 'rating_counter');

3) Using UNION

SELECT * 
FROM `wp_postmeta` 
WHERE meta_key = 'rating_average' 
UNION
SELECT * 
FROM `wp_postmeta` 
WHERE meta_key = 'rating_counter';

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1269563

You want OR, but a more typical way of doing this uses IN:

SELECT *
FROM `wp_postmeta`
WHERE meta_key IN ('rating_average', 'rating_counter');

Upvotes: 2

Related Questions