Been coding for 48 hours straight and am banging my head against the wall here. Please help me with this small issue.
My SQL query is this:
SELECT u.Firstname, u.Lastname, u.Rep, u.Email, u.Password, u.Gender, u.Level,
u.Birthday, u.Achievements, u.Height, u.Unit, u.cityid, u.countryid,
r.RegDate, ci.Name AS City, co.Name AS Country
FROM Users u, Registry r, Cities ci, Countries co
WHERE u.id = 1 AND r.uid = u.id AND u.cityid = ci.id AND u.countryid = co.id
LIMIT 1
My problem is that I just noticed that sometimes Users.cityid and Users.countryid are NULL (which is OK).
I want the query to give me all the other info (like, return NULL for City and Country) for this user even if one or both those fields are NULL. How to make the AND-parts conditional?
I hope I'm making myself clear in my fogginess.
I think you need a couple of OUTER joins if I have understood your situation correctly.
SELECT ...
FROM Users u
INNER JOIN Registry r ON r.uid = u.id
LEFT JOIN Cities ci ON u.cityid = ci.id
LEFT JOIN Countries co ON u.countryid = co.id
WHERE u.id = 1
You need to use a LEFT JOIN on your tables instead of using a WHERE.
So your FROM turns into:
FROM Users u JOIN Registry r on u.id = r.uid
LEFT JOIN Cities ci ON u.cityid = ci.id
LEFT JOIN Countries co ON u.countryid = co.id
WHERE u.id = 1 LIMIT 1
The LEFT JOIN is an OUTER join; it will join across the tables where the leftmost (hence the LEFT in the JOIN) term in the JOIN (i.e., the first one that appears) has an entry but the other table does not. OUTER JOINs are useful for these situations where you don't necessarily have data entries in all the tables for what you want from a query; they can be confusing at first, but they become very important to using SQL well.
Related
I am stuck with some SQL query.
I have four tables. Which are connected:
user =>user_account=>acount_profile_entries=>profile_entries
From left to right they are one to many.
user_account has a user_id field as FK.
account_profile_field has user_account_id and profile_entry_id.
Profile_entries has a text field that I need to show for each user (account).
I need to write a query that will show me, all accounts for every user, and its profile entries.
I am sorry if this is confusing, I tried to make it simple
This is what I have done so far. I can show all accounts for every user and this is the point I am stuck with. Last two commented out Joins are not working properly. I believe I am close somewhat, I just need a push :)
SELECT
u.email AS Email,
u.id AS UserId,
ua.id AS UserAccountId,
ua.app_id AS Application
FROM users AS u
INNER JOIN user_accounts ua ON ua.user_id = u.id
-- INNER JOIN account_profile_entries ape ON ape.user_account_id = ua.id
-- INNER JOIN profile_entries as pe ON pe.id = ape.profile_entry_id
limit 10
Try this SQL Query with using LEFT JOIN
Description :- The MySQL LEFT JOIN joins two tables and fetches rows based on a condition, which is matching in both the tables and the unmatched rows will also be available from the table written before the JOIN clause.
SYNTAX
SELECT column_name(s)
FROM table1
LEFT JOIN table2 ON table1.column_name = table2.column_name;
SELECT u.*,
u.id AS UserId,
ua.id AS UserAccountId,
ua.app_id AS Application,pe.* FROM `users` u
LEFT JOIN user_accounts ua ON ua.user_id = u.id
LEFT JOIN account_profile_entries ape ON ape.user_account_id = ua.id
LEFT JOIN profile_entries as pe ON pe.id = ape.profile_entry_id LIMIT 10
I have to use LEFT JOIN on 3 tables: UNITS, ROOMS_CHECK_IN and COMMENTS. Basically I want to show UNITS and for each unit count of rooms check in and count of comment. But I am getting same 4 digit number when I am running for rooms check in and comment count. If I separate the 2 queries with single left join, it works fine.
Below is the query:
SELECT u.ID,
u.unit_name,
count(c.checkin_status) as total_chekin ,
count(com.ID) as total_comment ,
h.hospital_name
FROM HOSPITALS h, UNITS u
LEFT OUTER JOIN ROOMS_CHECK_IN c ON c.unit_id = u.ID AND c.room_status=0
LEFT OUTER JOIN COMMENTS com ON com.unit_id = u.ID
WHERE h.ID = u.hospital_id AND u.hospital_id=3
GROUP BY u.ID;
Kindly help.
Never use commas in the FROM clause. Always use explicit proper JOIN context.
Then, you probably want count(distinct) (or to aggregate before joins):
SELECT u.ID, u.unit_name,
count(distinct c.checkin_status) as total_chekin,
count(distinct com.ID) as total_comment,
h.hospital_name
FROM HOSPITALS h JOIN
UNITS u
ON h.ID = u.hospital_id LEFT OUTER JOIN
ROOMS_CHECK_IN c
ON c.unit_id = u.ID AND c.room_status = 0 LEFT OUTER JOIN
COMMENTS com
ON com.unit_id = u.ID
WHERE u.hospital_id = 3
GROUP BY u.ID, u.unit_name, h.hospital_name;
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 3 Tables: Jobs, Users, Applications.
I'd like a list of all jobs and optionally "any" applications submitted per each job but only if I have data for the user who submitted the application.
I vaguely remember seeing some obscure syntax in which 2 tables are joined to a query as if being a single table, something like:
select j.title, a.id, u.name from jobs j
left join applications a join users u on a.job_id=j.id and a.user_id = u.id
I know I could accomplish this with a subquery, but does a syntax of this nature exist?
Update
The answers I've received so far assume I want only a basic join. Though they break the intended logic posed in the question. Perhaps I've should've posted the subquery equivalent to illustrate the type of query I'd like to run.
select j.job_id, au.application_id, au.user_id from jobs j
left join (
select a.job_id, u.user_id from applications a
join users u on u.user_id = a.user_id
) au on j.job_id = au.job_id
You need both of these to be outer joins if you chain them the "simple" way:
select *
from jobs j
left outer join application a on a.job_id = j.id
left outer join users u on u.user_id = a.id
Since I take it that an application requires a user you can force the inner join to happen first in this way. Maybe that's the syntax question you were asking about. The parens are generally optional in my experience but I don't know a lot of MySQL:
select *
from jobs j
left outer join (application a
inner join users u on a.user_id = u.id) on a.job_id = j.id
This is where a right join comes up sometimes so the query can be written from top to bottom without any nesting:
select *
from application a
inner join users u on u.user_id = a.id on
right outer join jobs j on a.job_id = j.id
You were almost there, your only mistake was to put the both join conditions after the second join.
select *
from jobs j
left join application a on on.job_id = j.id
left join users u on u.user_id = u.id
Note that the maximum number of tables that can be referenced in a join is 61 (in MySQL and probably most of the popular DBMS).
See documentation.
I have a table of ratings for comments, when I fetch comments, I also fetch the ratings and I also want to be able to display which comments the logged user has already voted on. This is what I am doing now
SELECT
c.id,
c.text,
c.datetime,
c.author,
u.email AS author_name,
SUM(cr.vote) AS rating,
cr2.vote AS voted
FROM comments c
LEFT JOIN users u ON u.id = c.author
LEFT JOIN comments_ratings cr ON c.id = cr.comment
LEFT JOIN comments_ratings cr2 ON c.id = cr2.comment AND cr2.user = :logged_user_id
GROUP BY c.id ORDER BY c.id DESC
But I don't like how I'm performing a second join on the same table. I know it is perfectly valid but if I could get the information I want from the first join, which is there anyway, why perform a second one?
Is it possible to figure out if a row with column user equal to :logged_user_id exists on table comments_ratings cr before executing the aggregate function(s)?
P.S.: If someone could come up with a better title, people can find in future, I'd also appreciate that.
You can do what you want with conditional aggregation:
SELECT c.id, c.text, c.datetime, c.author, u.email AS author_name,
SUM(cr.vote) AS rating,
MAX(cr.user = :logged_user_id) as voted
FROM comments c LEFT JOIN
users u
ON u.id = c.author LEFT JOIN
comments_ratings cr
ON c.id = cr.comment
GROUP BY c.id
ORDER BY c.id DESC;