Christian
Christian

Reputation: 338

WordPress Database Query

I'm trying to run a SQL query from a WordPress database. Basically, I need to get the email address of all users from the wp_users table and then check to see if those users have a value that is not null in the row 'agent_name' in the wp_usermeta table. So something like this pseudo code SELECT user_email FROM 'wp_users' and SELECT agent_name FROM 'wp_usermeta'. The goal is to have the query printed out in a csv (that part I can figure out). You can see an example of the kind of output I'm looking for in this screenshot:

enter image description here

I hope that makes sense.

Upvotes: 1

Views: 74

Answers (1)

Cyclonecode
Cyclonecode

Reputation: 30121

You should use an INNER JOIN to achive this:

SELECT u.user_email, m.meta_value FROM wp_users u 
INNER JOIN wp_usermeta m ON m.user_id = u.ID && 
           m.meta_key = 'agent_name' && 
           m.meta_value IS NOT NULL

Upvotes: 1

Related Questions