Home > Net >  how to get 2 different results from the same column using MySQL Query from Wordpress
how to get 2 different results from the same column using MySQL Query from Wordpress

Time:02-15

I want to get 2 results at the same time, from a single column.

For example:

    $MySQLQuery = $wpdb->get_results("
       SELECT meta_value FROM wp_postmeta 
       WHERE 
       meta_key='client_name' AND meta_value = 'Jhon' AND
       meta_key='order_completed' AND meta_value = 'yes'

        ");

I'm not an experienced programmer, I'm a beginner, and in my spare time I'm fiddling with WordPress on a local server

I have a basic understanding of PHP and MySQL (non-experienced, apprentice), Can someone help me?

Edit: i want to get 2 distinct results from the same column.

The Columns are called "meta_key" and "meta_value"

In the column "meta_key" I want to get the 2 results:

client_name
order_completed

In column "meta_value", it is the result of columns "meta_key"

john
yes

CodePudding user response:

I managed to solve my problem using

GROUP BY

SELECT *
FROM wp_postmeta
WHERE meta_value IN ('john', 'yes')
GROUP BY post_id
HAVING COUNT(*) = 2 

In this way, MySQL returns all the values ​​of the "meta_value" with the condition

CodePudding user response:

Try to use UNION to concatenate 2 queries.

Or try:

select * from 
(select * from TABLE WHERE condition = "value"),
(select * from TABLE WHERE condition = "value"),
  • Related