Sajid Anwar Sajid Anwar - 3 months ago 6
MySQL Question

How to select record from same column but different value

I want to select 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'
please help me

enter image description here

Answer

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';
Comments