mysql inner join on two tables - mysql

Columns in table wp_users:
id
user_login
Columns in table wp_usermeta:
user_id
meta_key [if equals 'primaryblog']
metav_value
The id in wp_users and the user-id in wp_usermeta are same. I am expecting the result as
id, user_login, meta_key, meta_value
I tried:
select a.user_id,a.meta_key,a.meta_value
from wp_usermeta as a
where meta_key = 'primaryblog'
inner join b.id, b.user_login
from wp_users as b on a.user_id=b.id
How to get the intended result?

The JOIN comes before the WHERE clause:
SELECT
a.id, a.user_login, b.meta_key, b.meta_value
FROM wp_users a
JOIN wp_usermeta b ON a.id = b.user_id
WHERE meta_key = 'primaryblog';

Related

wp_users, wp_usermeta table join query not work for me

wp_users, wp_usermeta table join query not work for me
SELECT distinct(a.ID) FROM wp_users as a
INNER JOIN wp_usermeta as b ON a.id = b.user_id
WHERE a.id < 1000 AND b.meta_key != 'user_name' AND (b.meta_key = 'first_name' AND b.meta_value = '')
I want to find data where user_id(a.id) is less than 1000 and there is no user_name(wp_usermeta table) field in meta_key and first_name(wp_usermeta table) in meta_key is blank.
However, this query does not check for user_name data.
What is wrong with my query?
I tried
SELECT distinct(a.ID)
FROM wp_users as a
INNER JOIN wp_usermeta as b ON a.id = b.user_id
INNER JOIN wp_usermeta as c on a.id = c.user_id
WHERE a.id < 1000 AND c.meta_key != 'user_name' AND (b.meta_key = 'first_name' AND b.meta_value = '')
not worked. and i tried LIKE statement and NOT IN () .. etc
I think I've used everything I can..
I'd appreciate your help.
Grouping by a.ID is a good scenario over distinct. The SUM(expression) added up the expression value (either 0 false, or 1 true). By using HAVING as a post grouping criteria allows this to be your filter.
SELECT a.ID, SUM(meta_key = 'user_name') as un, SUM(meta_key = 'first_name' AND meta_value = '') as fn
FROM wp_users as a
INNER JOIN wp_usermeta as b ON a.id = b.user_id
WHERE a.id < 1000
GROUP BY a.ID
HAVING un = 0 and fn = 1

Need help in optimizing the query

SELECT
u.ID,
u.display_name as name,
u.user_email as email,
u.user_registered as registered,
(
select
meta_value
from
wp_usermeta
where
user_id = u.ID
and meta_key = 'mobileno'
limit
1
) as mobileno,
(
select
meta_value
from
wp_usermeta
where
user_id = u.ID
and meta_key = 'referral_id'
limit
1
) as referral_id,
(
SELECT
COUNT(meta_value) AS total_ref
FROM
wp_usermeta
WHERE
meta_key = 'ambassador_ref_id'
AND meta_value = referral_id
) as total_ref,
wc.task_no,
wc.status,
wc.uploaded_date,
wc.reject_reason
FROM
wp_users u,
wp_ca_tasks wc
WHERE
u.ID = wc.user_id
GROUP BY
wc.user_id,
wc.task_no;
In the above code, if we remove the block
(
SELECT
COUNT(meta_value) AS total_ref
FROM
wp_usermeta
WHERE
meta_key = 'ambassador_ref_id'
AND meta_value = referral_id
) as total_ref
the code executes a bit faster. But if we add that block, it basically gets stuck in Loading...
Currently using MySQL 5.7.
How can I optimize the above block of code to make the execution faster?
Ah, the notorious WordPress meta-table slowdown.
Change the comma-joins (FROM a,b WHERE a.ID = b.user_id) to proper JOINs.
Eliminate your dependent subqueries and replace them with JOINed subqueries.
A quicker query might look like this.
SELECT
u.ID,
u.display_name as name,
u.user_email as email,
u.user_registered as registered,
/* from the joined tables
mobilno.meta_value as mobileno,
referral_id.meta_value as referral_id,
counts.total_ref
wc.task_no,
wc.status,
wc.uploaded_date,
wc.reject_reason
FROM
wp_users u
JOIN wp_ca_tasks wc ON u.ID = wc.user_id
LEFT JOIN wp_usermeta mobilno ON mobilno.user_id = u.ID
AND meta_key = 'mobilno'
LEFT JOIN wp_usermeta referral_id ON referral_id.user_id = u.ID
AND meta_key = 'referral_id'
LEFT JOIN (
SELECT COUNT(*) total_ref,
meta_value referral_id
FROM wp_postmeta
WHERE meta_key = 'ambassador_ref_id'
GROUP BY meta_value)
) counts ON counts.referral_id = referral_id.meta_value
GROUP BY wc.user_id, wc.task_no;
The trick is to avoid repeating the queries buried in the SELECT statement over and over. LEFT JOINing them helps.
And, your WordPress tables need better indexes. Look at this. https://wordpress.org/plugins/index-wp-mysql-for-speed/
In addition to what O.Jones says, wc needs
INDEX(user_id, task_no)
However, the GROUP BY probably violates "only_full_group_by". That is, for a given user_id and task_no, you will get random values for other columns fetched from wp_ca_tasks.

MYSQL query in Wordpress without a specific meta_key in wp_usermeta

I am trying to write a MYSQL in Wordpress query to search for users without a specific meta_key in wp_usermeta for the moment I write the opposite: users with a specified meta_key.
SELECT wp_users.ID, wp_usermeta.meta_key, wp_usermeta.meta_value
FROM `wp_users` LEFT JOIN wp_usermeta
ON wp_users.ID = wp_usermeta.user_id AND (wp_usermeta.meta_key = 'nombre_curso' OR wp_usermeta.meta_key ='caduca_enlace1')
How can I rewrite this to obtain only wp_users.ID WITHOUT specified meta_key rows?
Thanks
A first solution
SELECT wp_users.ID, wp_users.user_email FROM wp_users
WHERE wp_users.ID NOT IN (
SELECT wp_usermeta.user_id
FROM wp_usermeta
WHERE wp_usermeta.meta_key = 'caduca_enlace1' )
Thanks #Howard E.
SELECT wp_users.ID, wp_users.user_email
FROM wp_users
WHERE NOT EXISTS ( SELECT 1
FROM wp_usermeta
WHERE meta_key = 'caduca_enlace1'
AND user_id = wp_users.ID )

SQL for WP to delete users with multiple meta keys and comments

I need help putting together an SQL that can to remove users if they don't have (metakey1 or metakey2) and (does not have comments)
I have this SQL which does it for single meta_key
SELECT *
FROM wp_users LEFT JOIN wp_usermeta
ON wp_users.ID = wp_usermeta.user_id
AND wp_usermeta.meta_key = 'metakey1'
WHERE wp_usermeta.user_id IS NULL
How can i extend the above SQL to do that?
You can use in in the on clause:
SELECT u.*
FROM wp_users u LEFT JOIN
wp_usermeta um
ON u.ID = um.user_id AND
um.meta_key IN ('metakey1', 'metakey2', 'comments')
WHERE um.user_id IS NULL;
You get no matches only if all the metakeys are missing, which I think is what you are asking for.
EDIT:
You seem to want:
SELECT u.*
FROM wp_users u
WHERE NOT EXISTS (SELECT 1
FROM wp_usermeta um
WHERE u.ID = um.user_id AND
um.meta_key IN ('metakey1', 'metakey2')
) AND
NOT EXISTS (SELECT 1
FROM wp_comments c
WHERE u.ID = c.user_id
);
I prefer NOT EXISTS if you are going to have multiple comparisons to different tables.
Try doing a delete with an exists clause asserting the requirement of either of two keys:
DELETE
FROM wp_users wp1
WHERE NOT EXISTS (SELECT 1 FROM wp_usermeta wp2
WHERE wp1.ID = wp2.user_id AND
wp2.meta_key IN ('metakey1', 'metakey2'));
Note that the following where clause is no longer needed in the version of the query I wrote above:
WHERE wp_usermeta.user_id IS NULL
It is no longer needed because NOT EXISTS now handles the job which the exclusion join was handling previously.

mysql query error with four tables

i have 4 tables. wp_users, ppmro_members, cu_orders, wp_posts.
now i want to all the users which are members for that i know i can you inner join using this query
SELECT wp_users.display_name, wp_users.user_email, cu_orders.post_id,wp_posts.post_title FROM ((wp_users INNER JOIN wp_pmpro_memberships_users ON wp_users.id=wp_pmpro_memberships_users.user_id) INNER JOIN cu_orders ON cu_orders.user_id =wp_users.ID INNER JOIN wp_posts ON wp_posts.ID = cu_orders.post_id)
and then i want to know all these users have purchased courses or not for that i need post id from cu_orders then by the post_id i can know the post_title from wp_posts. user_id and id is common in wp_users and ppmro_members. cu_orders has user_id, email, post_id. cu_orders has also the emails who are not users or members and for them user_id is 0. how to do that query in mysql together?
Not an answer, but FWIW, I find this a little easier to read:
SELECT u.display_name
, u.user_email
, o.post_id
, p.post_title
FROM wp_users u
JOIN wp_pmpro_memberships_users x
ON x.user_id = u.id
JOIN cu_orders o
ON o.user_id = u.ID
JOIN wp_posts p
ON p.ID = o.post_id