I'm trying to create a view in MySQL, using phpMyAdimn, which will display data from three tables, in the format below. It needs to be sorted by last name, first name. I've never pulled data from more than one table before and this is difficult for me to grasp. I need to get this to work as soon as possible so I can create reports in Google Data Studio for our client.
Thanks...
View needs to present data in this order, and is named UserCourseProgress.
Last Name, First Name, Email, Group (Atlanta, Charleston, Greenvile, Nashville), Course Progress, Completion Date
Tables and columns in database.
TABLES COLUMNS
wp_users user_email
wp_usermeta first_name, last_name, wp_2_capabilities
wp_2_wpcw_user_progress unit_completed_status, unit_completed_date
Code I've tried.
CREATE VIEW UserCourseProgress AS
SELECT
wp_users.user_email, wp_usermeta.first_name, wp_usermeta.last_name, wp_usermeta.wp_2_capabilities, wp_2_wpcw_user_progress.unit_completed_status, wp_2_wpcw_user_progress.unit_completed_date
FROM
wp_users,
wp_usermeta,
wp_2_wpcw_user_progress
INNER JOIN wp_usermeta ON (wp_users.user_email=wp_usermeta.first_name) AND (wp_users.user_email=wp_usermeta.last_name) AND (wp_users.user_email=wp_usermeta.wp_2_capabilities)
INNER JOIN wp_2_wpcw_user_progress (wp_users.user_email=wp_2_wpcw_user_progress.unit_completed_status) AND (wp_users.user_email=wp_2_wpcw_user_progress.unit_completed_date)
I'm answering my own question here. Let me state that I'm a novice with MySQL queries, but some of you may have guessed that.
I studied many examples before I was able to get my query to work, and it took several hours, over several days. The code for my solution is as follows and it works. I still haven't figured out how to concatenate the data in wp_2_capabilities to only show the role, and not the serialized data, but that is for another day.
Thanks for the input, to the ones who did.
MY SOLUTION
CREATE VIEW CourseProgress AS
SELECT
wp_users.ID,
wp_users.user_email,
firstmeta.meta_value AS first_name,
lastmeta.meta_value AS last_name,
rolemeta.meta_value AS wp_2_capabilities,
unitprogress.unit_completed_status
FROM
wp_users
LEFT JOIN wp_usermeta AS firstmeta
ON
wp_users.ID = firstmeta.user_id AND firstmeta.meta_key = 'first_name'
LEFT JOIN wp_usermeta AS lastmeta
ON
wp_users.ID = lastmeta.user_id AND lastmeta.meta_key = 'last_name'
LEFT JOIN wp_usermeta AS rolemeta
ON
wp_users.ID = rolemeta.user_id AND rolemeta.meta_key = 'wp_2_capabilities'
LEFT JOIN wp_2_wpcw_user_progress AS unitprogress
ON
wp_users.ID = unitprogress.user_id AND unitprogress.unit_completed_status = 'complete'
WHERE
rolemeta.meta_value REGEXP 'gs_atlanta|gs_charleston|gs_greenvilee|gs_nashville'
ORDER BY
wp_users.ID;
Related
We have a woocommerce site that has had a backdoor registration for years resulting in thousands of spam accounts.
I've been able to remove all of the ".ru" and other spam email accounts but there are many gmail addresses being used.
The one thing that seems to be common for many of these is that the first and last name are identical ex: "Donaldpat Donaldpat".
How would I write an SQL query that would delete the users from the wp_users table that have an identical value for first and last name in the related wp_usermeta table values?
would it be something like this:
DELETE FROM wp_users
WHERE EXISTS (
SELECT * FROM wp_usermeta
WHERE wp_usermeta.user_id = wp_users.ID
AND wp_usermeta.first_name = wp_usermeta.last_name
)
You can try this, what I'm doing here is separating the users.ID that I don't need so I just have the tables that I want to remove. Instead of using EXISTS I decided to use IN because that way you get the list ID directly, and remove them.
DELETE FROM wp_users
WHERE wp_users.ID IN (
SELECT DISTINCT user_id
FROM wp_usermeta
WHERE wp_usermeta.first_name = wp_usermeta.last_name
)
I hope this was helpful.
How can we show the 'inverse' of the Inner Join. For example, I have a list of actual transactions of customers that went thru the payment processor, in this case 'Paypal' but they never clicked the 'Back to Merchant' tab so that we can process their userid and password.
This script shows ALL the people that are in the customer list and their associated place in the users database:
SELECT
`Transactions List`.`Customer Email`,
users.Email,
`Transactions List`.`Transaction ID`,
users.`Name`,
users.Phone
FROM
`Transactions List`
INNER JOIN users ON `Transactions List`.`Customer Email` = users.Email
What I'm seeking to do is show the INVERSE of that. i.e. all the people who LOST their way. They DO appear in the TRANSACTIONS LIST table but do NOT appear in the USERS table.
Anyone have an idea how to convert this MYSQL Query into the Inverse so we can quickly identify which customers did not get user accounts?
There's an existing post "Inner join inverse Php MySQL" here that wasn't answered that asks a similar question. Perhaps the person asking the question was not clear enough: Inner join inverse Php mysql
also
What is the difference between “INNER JOIN” and “OUTER JOIN”?
What is the difference between "INNER JOIN" and "OUTER JOIN"?
but neither of these methods actually do what I want the script to do.
What I'm seeking to do is show [...] all the people who [...] appear in the TRANSACTIONS LIST table but do NOT appear in the USERS table.
You could use not exists:
select t.*
from transactions_list t
where not exists (
select 1 from users u where t.customer_email = u.email
)
Another way to phrase this is to use an anti-left join (this is more in the spirit of your question, that relates to joins):
select t.*
from transactions_list t
left join users u on t.customer_email = u.email
where u.email is null
This means: try to join each transaction with a user, and filter on those that did not match.
select t.*
from `Transactions List` t
left join users u on t.`Customer Email` = u.email
where u.email is null
Given the above syntax and the name of the table in the database as specified above this is the correct answer. Thank you to GMB for answering the question. For other readers, keep in mind that if your database tables include spaces in their names or field names then you must use the scare quotes to identify your table or field names. This is commonly used when importing tables into MySQL from 3rd party tools.
I'm completely new to mongoDB, and wondering if it's even possible to query something like this in the mongo language:
SELECT
stg.emp_user_id as emp_user_id,
stg.mgr_user_id as mgr_user_id,
stg.emp_email as emp_email,
stg.employeetype as employeetype,
s.shift_start_time as shift_start_time,
s.shift_end_time as shift_end_time,
s.days_worked as days_worked,
s.num_employees as num_employees,
FROM(
#nested table to link the employee to the manager, based on employee email
SELECT
u.userid as emp_user_id,
p.userid as mgr_user_id,
u.email as emp_email,
u.employeetype as employeetype,
FROM Users u
LEFT JOIN Peoples p on u.email = p.email) stg
#bring in shifts table linked to mgr, now that we have mgr-emp relationship
LEFT JOIN shifts s ON stg.mgr_user_id = s.user_id;
I've been looking around and have been able to find simple conversion references (querymongo.com, stackoverflow examples, etc), but haven't found anything for joins and nested tables. Any help/direction would be appreciated!
I have a WordPress database that has approximatly 28000 spam user`registrations in there which I some how need to isolate and delete without removing any actual customers.
I have noticed that all of the spam user registrations have an empty meta_value for a specific meta_key
The meta_key in question is billing_first_name
Therefore, I am trying to write a query I can execute in PHPmyAdmin that will return a list of all user IDs that have an empty meta_value for the meta_key billing_first_name so that I can then delete all of these users from the _usermeta table AND the _users table.
I found a user online who had a similar issue: https://wordpress.org/support/topic/deleting-spam-user-accounts-from-site-with-woocommerce
He wrote the following query that I have attempted to run on my database:
SELECT * FROM wp_usermeta JOIN wp_users ON (wp_usermeta.user_id = wp_users.ID) WHERE user_id NOT IN (SELECT um1.user_id FROM wp_usermeta um1 WHERE um1.meta_key = 'shipping_first_name')
However, its only returning 137 results, and with 28000+ plus registration, but only around 1000 actual orders, I have a heck of a lot more than 137 spam accounts in the database.
I have tried adapting the query to expand upon it and get it to do what I want, but I am not really a "database" person and am not having much luck.
My attempt was:
SELECT * FROM wv5_usermeta JOIN wv5_users ON (wv5_usermeta.user_id = wv5_users.ID) WHERE user_id NOT IN (SELECT um1.user_id FROM wv5_usermeta um1 WHERE um1.meta_key = 'billing_first_name' AND um1.meta_value IS NOT NULL)
Can someone help me formulate a working query that I can use?
Note that my attempt was based on the query I found online, and I have no idea if thats even the right approach for this.
My end goal is to identify ALL users that have never made any orders. Users that have made no orders do not have a billing address or shipping address set. So therefore, I am trying to build a query that selects users with a null billing address or null shipping address and then delete them.
Many Thanks for any asssitance provided.
EDIT:
Just to clarify, the site users WooCommerce and as such there is no specific orders table in the database. meta_keys such as "billing_first_name", "shipping_first_name" and such only get populated with a value once a user has made an order for the first time. Therefore, the assumption is that a spam user would have never made an order and thus these meta_keys will be null.
Just a guess according to this post woocommerce stores orders in a posts table with post_type = 'shop_order'.
So you can try this query :
SELECT u.* FROM wv5_users u
LEFT JOIN wv5_posts p
ON u.ID = p.post_author
AND p.post_type = 'shop_order'
GROUP BY u.ID
HAVING COUNT(p.id)=0
It should return list of users never had any posts with post_type = 'shop_order'. Probably you should check what exact post_type your woocommerce uses.
Be careful if you plan to delete all those users. Some of them could be your administrators.
EDIT Sorry, I have no wordpress and woocommerce installed. Since my query does not help. Let try to go your way but a bit properly:
SELECT u.* FROM wv5_users u
LEFT JOIN wv5_usermeta m
ON u.ID = m.user_id
AND m.meta_key = 'shipping_first_name'
GROUP BY u.ID
HAVING COUNT(m.id)=0
or according to this
HAVING COUNT(m.umeta_id)=0
I have a database table with the following information :
owner.primaryitowner,
owner.secondaryitowner,
owner.primarybusinessowner,
owner.secondarybusinessowner
The issue, is the owners are only stored as emails. There is another table I normally inner join users on users.username = owner.primaryitowner to get users.displayname so the data reads correctly. The issue is I need to do this for all 4 columns, and when I can only figure out how to connect 1 column in a query. Thanks for the help
P.S. I cannot change the database I am only a report writer.
Assuming you want all the display name for all owners, try something like this:
select u.displayname
from users u
inner join owners o on
o.primaryitowner = u.username
or o.secondaryitowner = u.username
or o.primarybusinessowner = u.username
or o.secondarybusinessowner = u.username