简体   繁体   中英

MySql query double WHERE clause

I currently have a query that checks whether a certain word is found in the array values.

$imploded = implode("','",$company_array); 
$query = "SELECT * 
          FROM $wpdb->usermeta 
          WHERE meta_key='company' 
          AND meta_value IN ('".$imploded."')";

The $company_array contains different values.

I want to add another WHERE clause but don't know how.
It should check on the users role:

SELECT*
FROM $wpdb->usermeta
WHERE meta_key = 'wp_capabilities'
AND meta_value = 'subscriber'

Maybe some sort of join queries? I have no clue.

==== Sample data and expected output ====
This is my current query

$query = "SELECT * 
          FROM $wpdb->usermeta 
          WHERE meta_key='company' 
          AND meta_value IN ('".$imploded."')";

I want to add the following query

SELECT*
FROM $wpdb->usermeta
WHERE meta_key = 'wp_capabilities'
AND meta_value = 'subscriber'

The first query loads a list of users that work for a certain company. The problem is................

Crap. I just realised something while writing this.
The problem was that the administrator is also listed in the list of users. I wanted him out of the results.
The users are listed based upon their value in the company field. But if that is empty the admin isn't listed anywhere.

Your WHERE clause needs to be

WHERE (meta_key='company' AND meta_value IN ('".$imploded."'))
   OR (meta_key = 'wp_capabilities' AND meta_value = 'subscriber')

try like this

$query = "SELECT * 
          FROM $wpdb->usermeta 
          WHERE (meta_key='company' 
          AND meta_value IN ('".$imploded."')) or (meta_key = 'wp_capabilities' and meta_value = 'subscriber')";

I think you required below. Note the () with OR for your both where clauses.

$query = "SELECT * 
          FROM $wpdb->usermeta 
          WHERE ( meta_key='company' AND meta_value IN ('".$imploded."')") 
          OR (meta_key = 'wp_capabilities' AND meta_value = 'subscriber');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM