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
Related
I have a wordpress site that is used to store student grades on various lessons (from the quizzes on the site). I am trying to create a query that will pull out all of the lessons for all students in a certain group (using buddypress groups) and the students grades in each lesson.
I have created this query:
SELECT u.display_name, p.post_title, cm.meta_value
FROM wp_users u
JOIN wp_bp_groups_members gm
ON u.ID = gm.user_id
JOIN wp_comments c
ON u.ID = c.user_id
JOIN wp_commentmeta cm
ON c.comment_ID = cm.comment_id
JOIN (SELECT * FROM wp_posts WHERE post_type LIKE 'lesson') p
ON c.comment_post_id = p.ID
WHERE gm.group_id = 4 AND cm.meta_key LIKE 'grade'
This currently returns all the grades for all students in a group in the lessons they have attempted the test. However it does not return any lessons they have not attempted the test in, which I need still.
Just to be clear: lessons are posts, grades are meta_values in a record with a meta_key of 'grades'. These are stored as comments, and comment_meta.
I hope this is all clear and you can help. Thanks.
After Ollie Jones help I made this:
SELECT u.display_name, p.post_title, IFNULL(cm.meta_value,'--nothing--') grade
FROM wp_users u
JOIN wp_comments c
LEFT JOIN wp_commentmeta cm
ON c.comment_ID = cm.comment_id AND cm.meta_key = 'grade'
JOIN wp_bp_groups_members gm
ON u.ID = gm.user_id
JOIN wp_posts p
ON c.comment_post_id = p.ID
WHERE gm.group_id = 4 AND p.post_type LIKE 'lesson'
Which almost works but returns all student grades, not just the ones in the group (though it only gives the one name of the student in the group).
This is the familiar key/value store problem, that comes up with commentmeta, postsmeta, and usermeta. When you JOIN two tables and the left one might not have a corresponding row, you need to use LEFT JOIN. When the left one is a key/value table, you need to adjust the ON condition accordingly. Finally, when you LEFT JOIN two tables and there's no matching row in it it, you get back NULLs for those columns, so you must allow for that.
So this kind of SQL pattern will do the trick for you
SELECT whatever, IFNULL(cm.meta_value,'--nothing--') grade
FROM whatever
LEFT JOIN wp_comments c ON whatever.id = c.user_id
LEFT JOIN wp_commentmeta cm ON c.comment_id = cm.comment_id
AND cm.meta_key = 'grade'
JOIN whatever
Is there a user_id in your comments table? I dont see where you joined which user posted which comment. So when you join the group_members to the users table, you are specifying which users to show, but since you are not joining on user id when you join to your comments table, it will show all the comments for all the users. Im not sure if this will work for your table, but try:
SELECT u.display_name, p.post_title, IFNULL(cm.meta_value,'--nothing--') grade
FROM wp_users u
LEFT JOIN wp_comments c
JOIN wp_commentmeta cm
ON c.comment_ID = cm.comment_id AND cm.meta_key = 'grade'
JOIN wp_bp_groups_members gm
ON c.user_ID = gm.user_id
ON u.user_id = c.user_id
JOIN wp_posts p
ON c.comment_post_id = p.ID
WHERE gm.group_id = 4 AND p.post_type LIKE 'lesson'
Hope this helps!
I have 2 tables:
posts: userid, lastuserid
users: id, name
I need to join posts.userid = users.id and posts.lastuserid = users.id to get username and lastusername.
My query did as below:
SELECT posts. * , users.name, vUsers.name
FROM posts
INNER JOIN users ON users.id = posts.userid
INNER JOIN Users ON vUsers.id = posts.lastuserid
Is there any other (better) way to do this?
Your query is probably correct. I would encourage you to use table aliases that are abbreviations for the things you are looking for:
SELECT p. * , u.name as username, l.name as lastusername
FROM posts p INNER JOIN
users u
ON u.id = p.userid INNER JOIN
users l
ON l.id = p.lastuserid;
Your query has something called vUsers, which is not defined.
I have two table, users and comments.
In the users table, there're columns id and username.
In the comments table I have user_id and his message.
And I wanted to create a table that select the username and his comment count when I search a particular username.
How do I write this?
my testing attempt:
SELECT COUNT(*) AS comment_count
FROM song_comments
RIGHT JOIN users
WHERE user_id = 7 AND comments.user_id = users.id
Try this:
SELECT U.Username, COUNT(SC.message) AS comment_count
FROM song_comments SC JOIN
users U ON U.id=SC.user_id
WHERE U.user_id = 7
GROUP BY U.Username
This gives you users and count
select u.username, count(c.user_id) as comment_count
from users u
join comments c on u.id = c.user_id
group by u.username
You can add a where to get one user's count
select u.username, count(c.user_id) as comment_count
from users u
join comments c on u.id = c.user_id
where u.username = 'Hogan'
group by u.username
I have a database design for a survey.
I have a Table USERS with user_id PK and name.
QUESTION with question_id and question_text.
POSSIBLE_ANSWER with answer_id PK ,value, text.
Also, I have USER_ANSWERS with user_id from users , Question_id , answer_id .
Also some 3 other tables not relevant to my question.
What I need is select avg(value) for all users where name like'm%'.
Hope this is clear thanks.
try the following:
select avg(PA.value)
from POSSIBLE_ANSWER PA
inner join USER_ANSWERS UA on PA.answer_id = UA.answer_id
inner join USERS U on U.user_id = UA.user_id
where U.name like 'm%'
SELECT u.user_id, u.name, AVG(pa.value)
FROM USER_ANSWERS ua
INNER JOIN POSSIBLE_ANSWER pa ON pa.answer_id = ua.answer_id
INNER JOIN USERS u ON ua.user_id = u.user_id
WHERE u.name LIKE 'm%'
GROUP BY u.user_id
Remove GROUP BY if you want the average of all users with name starting with 'm'. This query gives the average PER user, whose name starts with m.
You could do this:
SELECT u.name, AVG(pa.value) AS avgvalue
FROM USER_ANSWERS ua
INNER JOIN USERS u ON u.user_id = ua.user_id
INNER JOIN POSSIBLE_ANSWER pa ON pa.answer_id = ua.answer_id
WHERE u.name LIKE 'm%'
GROUP BY u.name
Though I'm still interested in reading what you have tried so far. And you could add u.user_id to your select list and group by that instead (like PatrikAkerstrand did) - which you'd have to do if your names aren't unique
I have a payments table that has the following structure.
**Payments**
id
name
created_by - user_id
closed_by - user_id
**Users**
user_id
name
surname
What is the best way to show both the name and surname of the user who has created and closed the payment file.
The only way i can think this could work would be using a subquery for both(created_by,closed_by fields) thanks
Try this:
SELECT p.id, p.name,
CONCAT (u1.name,' ', u1.surname) created,
CONCAT (u2.name,' ', u2.surname) closed,
FROM payments p INNER JOIN users u1
ON p.created_by = u1.user_id
INNER JOIN users u2
ON p.closed_by = u2.user_id
EDITED: if you want name and surname splitted
SELECT p.id, p.name,
u1.name created_name, u1.surname created_surname,
u2.name closed_name, u2.surname closed_surname,
FROM payments p INNER JOIN users u1
ON p.created_by = u1.user_id
INNER JOIN users u2
ON p.closed_by = u2.user_id
A strict join is not enough, I think you can use joins in the where clause
select name, surname
from
payments p, users u
where
u.user_id = p.created_by
and u.user_id = p.closed_by
You can join to a single table multiple times, but you have to use an alias.
select
p.*,cr.*,cl.*
from
payments p
join users cr
on p.created_by = cr.user_id
join users cl
on p.closed_by = cl.user_id