I'm trying to filter my table where a user has a row in one table, and does not have a row in another. Here's my table structure:
Here's an SQL Fiddle instead: http://sqlfiddle.com/#!9/6e27ed/2
CREATE TABLE users (
user_id INT(11) AUTO_INCREMENT,
name VARCHAR(25),
PRIMARY KEY(user_id)
);
CREATE TABLE photos (
photo_id INT(11) AUTO_INCREMENT,
user_id INT(11) NOT NULL,
PRIMARY KEY(photo_id)
);
CREATE TABLE blocked (
user_id INT(11) NOT NULL,
blocked_id INT(11) NOT NULL
);
INSERT INTO users (name)
VALUES ('returned'), ('not returned'), ('not returned'), ('not returned');
INSERT INTO photos (user_id)
VALUES (1), (2);
INSERT INTO blocked (user_id, blocked_id)
VALUES (4, 2);
Here's the query that I'm trying:
SELECT u.*, min(p.photo_id)
FROM users u
INNER JOIN photos p using(user_id)
LEFT JOIN blocked b ON b.user_id = 4 AND b.blocked_id = u.user_id
WHERE u.user_id != 4
GROUP BY u.user_id
LIMIT 9;
The example data makes it very obvious what the results should be, as the "name" field is filled with "returned", "not returned" in this example, user-id 2 is still returned, but he should be removed by the LEFT JOIN, as user-id 4 has user-id 2 in the blocked-id field of the blocked table.
Expected results from table:
+---------+----------+-----------------+
| user_id | name | min(p.photo_id) |
+---------+----------+-----------------+
| 1 | returned | 1 |
+---------+----------+-----------------+
Received results from query:
+---------+--------------+-----------------+
| user_id | name | min(p.photo_id) |
+---------+--------------+-----------------+
| 1 | returned | 1 |
| 2 | not returned | 2 |
+---------+--------------+-----------------+
It is hard to answer this as I see you are comparing blocked_id=user_id, though in blocked table you also have a user_id column. Only you will know that.
But consider the following
SELECT u.*, min(p.photo_id),b.*
FROM users u
INNER JOIN photos p using(user_id)
LEFT JOIN blocked b ON b.user_id = 4 AND b.blocked_id = u.user_id
WHERE u.user_id != 4 and b.user_id is null
GROUP BY u.user_id
LIMIT 9;
+---------+----------+-----------------+---------+------------+
| user_id | name | min(p.photo_id) | user_id | blocked_id |
+---------+----------+-----------------+---------+------------+
| 1 | returned | 1 | NULL | NULL |
+---------+----------+-----------------+---------+------------+
It turns on the sonar by revealing the b.* columns added as columns 4 and 5. And messes with the where clause a bit.
Edit:
Cleaned up for production
SELECT u.*, min(p.photo_id)
FROM users u
INNER JOIN photos p using(user_id)
LEFT JOIN blocked b ON b.user_id = 4 AND b.blocked_id = u.user_id
WHERE u.user_id != 4 and b.user_id is null
GROUP BY u.user_id
LIMIT 9;
I think you want WHERE b.blocked_id IS NULL to return users that have NOT been blocked by user 4
You can try this one..
SELECT u.*, min(p.photo_id)
FROM users u
INNER JOIN photos p using(user_id)
LEFT JOIN blocked b ON b.blocked_id = u.user_id
GROUP BY u.user_id having min(p.photo_id)<=all(select min(p.photo_id) FROM users u
INNER JOIN photos p using(user_id)
LEFT JOIN blocked b ON b.blocked_id = u.user_id
GROUP BY u.user_id)
;
Related
Running into a seemingly simple JOIN problems here..
I have two tables, users and courses
| users.id | users.name |
| 1 | Joe |
| 2 | Mary |
| 3 | Mark |
| courses.id | courses.name |
| 1 | History |
| 2 | Math |
| 3 | Science |
| 4 | English |
and another table that joins the two:
| users_id | courses_id |
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 1 |
I'm trying to find distinct user names who are in course 1 and course 2
It's possible a user is in other courses, too, but I only care that they're in 1 and 2 at a minimum
SELECT DISTINCT(users.name)
FROM users_courses
LEFT JOIN users ON users_courses.users_id = users.id
LEFT JOIN courses ON users_courses.courses_id = courses.id
WHERE courses.name = "History" AND courses.name = "Math"
AND courses.name NOT IN ("English")
I understand why this is returning an empty set (since no single joined row has History and Math - it only has one value per row.
How can I structure the query so that it returns "Joe" because he is in both courses?
Update - I'm hoping to avoid hard-coding the expected total count of courses for a given user, since they might be in other courses my search does not care about.
Join users to a query that returns the user ids that are in both courses:
select u.name
from users u
inner join (
select users_id
from users_courses
where courses_id in (1, 2)
group by users_id
having count(distinct courses_id) = 2
) c on c.users_id = u.id
You can omit distinct from the condition:
count(distinct courses_id) = 2
if there are no duplicates in users_courses.
See the demo.
If you want to search by course names and not ids:
select u.name
from users u
inner join (
select uc.users_id
from users_courses uc inner join courses c
on c.id = uc.courses_id
where c.name in ('History', 'Math')
group by uc.users_id
having count(distinct c.id) = 2
) c on c.users_id = u.id
See the demo.
Results:
| name |
| ---- |
| Joe |
You can use in operator and use select to generate list of potential users_id attending the second course, to find matching ones in the first course. This is many times faster than using joins.
select distinct u.users_id, users.name
from users_courses u, users
where u.users_id in (select distinct users_id from users_courses where courses_id = 2)
and u.courses_id = 1
and users.users_id = u.users_id
Almost similar to what #Nae's solution.
select u.name from users u
where exists
(select 1
from users_courses uc
where uc.course_id in (1, 2)
and uc.user_id = u.id
group by uc.user_id
having count(0) = 2);
Your code is close. Just use GROUP BY and a HAVING clause:
SELECT u.name
FROM users_courses uc JOIN
users u
ON uc.users_id = u.id JOIN
courses c
ON uc.courses_id = c.id
WHERE c.name IN ('History', 'Math')
GROUP BY u.name
HAVING COUNT(DISTINCT c.name) = 2;
Notes:
This assumes that users cannot have the same name. You might want to use GROUP BY u.id, u.name to ensure that you are counting individual users.
If users cannot take the same course multiple times, then use COUNT(*) = 2 rather than COUNT(DISTINCT).
I'd write:
SELECT MAX(u.name)
FROM users_courses uc
LEFT JOIN users u ON uc.users_id = u.id
WHERE uc.courses_id IN (1, 2)
GROUP BY uc.users_id
HAVING COUNT(0) = 2
;
For more complex conditions (for example requiring the user to be in certain classes but also not in certain classes such as "Science") this should also work:
SELECT MAX(u.name)
FROM users_courses uc
LEFT JOIN users u ON uc.users_id = u.id
GROUP BY uc.users_id
HAVING (
SUM(uc.courses_id = 1) = 1
-- user enrolled exactly once in the course 2
AND SUM(uc.courses_id = 2) = 1
-- user enrolled in course 3, 0 times
AND SUM(uc.courses_id = 3) = 0
)
;
I have the following table structure with data
TABLE: USER
USER ID | USER NAME
1 | Joe
2 | Mary
TABLE : USER GROUP
USER ID | GROUP ID
1 | 1
1 | 2
TABLE : GROUP
GROUP ID | GROUP NAME
1 | Company 1
2 | Company 2
TABLE : ROLE
ROLE ID | ROLE NAME
1 | Administrator
2 | Users
TABLE : USER ROLE
USER ID | ROLE ID
1 | 1
2 | 1
As you can see user #2 does not belong to any group. Roles & Groups are optional forcing me to left joint but when I run a query as below
`SELECT a.user_id,
a.user_name
GROUP_CONCAT(r.role_name) AS role_names,
GROUP_CONCAT(g.group_name) AS group_names
FROM user a
LEFT JOIN role_map m ON a.user_id = m.user_id
INNER JOIN role r ON m.role_id = r.role_id
LEFT JOIN user_group s ON a.user_id = s.user_id
INNER JOIN group g ON s.group_id = g.group_id
GROUP BY a.user_id`
I get a cartesian product in the role_names column - the result looks like this
Joe | Administrators, Administrators | Company 1, Company 2
What am I doing wrong?
The easiest way to solve this is by using DISTINCT in your GROUP_CONCAT (SQL Fiddle). Also, you will need to add GROUP BY a.user_id in order to group per user:
SELECT a.user_id,
a.user_name,
GROUP_CONCAT(DISTINCT r.role_name) AS role_names,
GROUP_CONCAT(DISTINCT g.group_name) AS group_names
FROM `user` a
LEFT JOIN `user_role` m ON a.user_id = m.user_id
LEFT JOIN `role` r ON m.role_id = r.role_id
LEFT JOIN `user_group` s ON a.user_id = s.user_id
LEFT JOIN `group` g ON s.group_id = g.group_id
GROUP BY a.user_id;
I'm trying to simplify my query so that it only contains the session ID (SID) once.
The abstract structure of the Users table is:
+----+------+----------+
| ID | Name | Username |
+----+------+----------+
The Friends table has an abstract structure like:
+----+-----------------+----------+--------+---------+
| ID | UserID | FriendID | Hidden | Deleted |
| | (Foreign key | | | |
| | of ID in Users) | | | |
+----+-----------------+----------+--------+---------+
The abstract structure of the Sessions table:
+----+-----------------+-----+
| ID | UserID | SID |
| | (Foreign key | |
| | of ID in Users) | |
+----+-----------------+-----+
I have the following query, which has been adapted from the answer of a previous question of mine. As you can see, the session ID (SID) is repeated 4 times, is it possible to condense the query as a whole so that the SID is only required once?
SELECT *
,CASE
WHEN D.ID IS NULL
THEN "Wants to be your friend"
ELSE "Friends"
END AS STATUS
FROM (
SELECT DISTINCT A.ID
,A.NAME
,E.Hidden
FROM Users A
INNER JOIN Friends E ON A.ID = E.UserID
WHERE A.ID IN (
SELECT A.UserID
FROM Friends A
INNER JOIN Sessions S ON A.FriendID = S.UserID
WHERE S.SID = "1234"
AND Deleted = 'No'
)
) C
LEFT JOIN (
SELECT DISTINCT B.ID
,B.NAME
,F.Hidden
FROM Users B
INNER JOIN Friends F ON B.ID = F.FriendID
WHERE B.ID IN (
SELECT A.FriendID
FROM Friends A
INNER JOIN Sessions S ON A.UserID = S.UserID
WHERE S.SID = "1234"
AND Deleted = 'No'
)
) D ON C.ID = D.ID
UNION
DISTINCT
SELECT *
,CASE
WHEN C.ID IS NULL
THEN "Request Sent"
ELSE "Friends"
END AS STATUS
FROM (
SELECT DISTINCT A.ID
,A.NAME
,E.Hidden
FROM Users A
INNER JOIN Friends E ON A.ID = E.UserID
WHERE A.ID IN (
SELECT A.UserID
FROM Friends A
INNER JOIN Sessions S ON A.FriendID = S.UserID
WHERE S.SID = "1234"
AND Deleted = 'No'
)
) C
RIGHT JOIN (
SELECT DISTINCT B.ID
,B.NAME
,F.Hidden
FROM Users B
INNER JOIN Friends F ON B.ID = F.FriendID
WHERE B.ID IN (
SELECT A.FriendID
FROM Friends A
INNER JOIN Sessions S ON A.UserID = S.UserID
WHERE S.SID = "1234"
AND Deleted = 'No'
)
) D ON C.ID = D.ID
A basic way of explaining the system is that if two users are friends, then there is two records within the database. One from the first user to the second and another record from the second user to the first.
A friend request has been sent if there is a record from the current user to another, and a friend request has been received if there is a record from one user to the current one.
Here is a vann diagram of how it works:
SQL Fiddle - http://sqlfiddle.com/#!2/c5587/1
Sql fiddle : http://sqlfiddle.com/#!2/06e08/68/0
This return Friends and Request Sent :
SELECT
f.FriendID,
u.Name,
f.Hidden,
CASE
WHEN reqs.FriendID IS NULL
THEN "Request Sent"
WHEN reqs.FriendID = f.UserID
THEN "Friends"
END AS Status
FROM
Friends AS f
INNER JOIN
Sessions AS s
ON f.UserId = s.UserID
INNER JOIN
Users AS u
ON u.ID = f.FriendID
LEFT JOIN
Friends AS reqs
ON reqs.FriendID = f.UserID
AND reqs.UserID = f.FriendID
WHERE
s.SID = "sid1"
If you want Also Request Received, append this :
UNION
SELECT
f.UserID,
u.Name,
f.Hidden,
"Request Received" AS Status
FROM
Friends AS f
INNER JOIN
Sessions AS s
ON f.FriendID = s.UserID
INNER JOIN
Users AS u
ON u.ID = f.UserID
WHERE
f.UserID NOT IN
(
SELECT
ff.FriendID
FROM
Friends AS ff
INNER JOIN
Sessions AS ss
ON ff.UserID = ss.UserID
WHERE ss.SID = "sid1"
)
AND s.SID = "sid1"
Can't figure out how to optimise the last part. Since it's a SELF JOIN it's a damn mind twister.
I understand this is not what you expected but, i can't get ride of all the SID, but this request should be faster than the one you currently use
I have the following 3 tables:
users
| id | name | address |
|----+-------+----------+
| 1 | user1 | address1 |
| 2 | user2 | address2 |
locations
| id | user_id | name |
|----+---------+-----------+
| 1 | 1 | location1 |
| 2 | 1 | location2 |
| 3 | 2 | location3 |
orders
| id | user_id | from_id | to_id |
|----+---------+---------+-------+
| 1 | 1 | 1 | 2 |
| 2 | 1 | 1 | 0 |
from_id or to_id can have 0, meaning that user's address was used in this case.
Executing a 'naive' join on these tables:
SELECT u.name uname, fl.location flocation, tl.location tlocation
FROM users u, orders o, locations fl, locations tl
WHERE u.id = o.user_id
AND o.from_id = fl.id
AND o.to_id = tl.id
doesn't show records with 0:
user1 | location1 | location2
what I would like to see is the following data:
user1 | location1 | location2
user1 | location1 | address1
Using mysql, is there a way to extend the join to show such results ?
The following SQL should do it:
SELECT u.name uname, IF(fl.id, fl.location, u.address) AS flocation, IF(tl.id, tl.location, u.address) AS tlocation
FROM users u
INNER JOIN orders o ON u.id = o.user_id
LEFT JOIN locations fl ON o.from_id = fl.id
LEFT JOIN locations tl ON o.to_id = tl.id;
And if I may make a suggestion, I would recommend that you not use the WHERE clause as a way to join tables.
SELECT u.name uname, fl.location flocation, tl.location tlocation
FROM users u
left join orders o
on u.id = o.user_id
left join locations fl
on o.from_id = fl.id
left join locations tl
on o.to_id = tl.id
This will give you data for all users.
And as Tim Burch suggested, its best to use the ANSI SQL Syntax.
To use the user's address as a default for both the from and the to names, you can do a left join and use COALESCE to replace nulls with the address;
SELECT
u.name uname,
COALESCE(fl.name, u.address) flocation,
COALESCE(tl.name, u.address) tlocation
FROM users u
JOIN orders o ON u.id = o.user_id
LEFT JOIN locations fl ON o.from_id = fl.id
LEFT JOIN locations tl ON o.to_id = tl.id
An SQLfiddle to test with.
I recommend abandoning the "comma" syntax for the join operation; use the JOIN keyword, and move the join predicates to an ON clause rather than the WHERE clause.
To get all rows from orders returned, whether or not there is a "matching" row from the other tables, you can use an outer join operation.
In the SELECT list, you can test whether the location column from locations IS NULL, and return the address column from the user table when it is, using the COALESCE function.
SELECT u.name uname
, COALESCE(fl.location,u.address) flocation
, COALESCE(tl.location,u.address) tlocation
FROM orders o
LEFT
JOIN users u
ON u.id = o.user_id
LEFT
JOIN locations fl
ON fl.id = o.from_id
LEFT
JOIN locations tl
ON tl.id = o.to_id
The COALESCE(x,y) function is essentially shorthand for 'CASE WHEN x IS NULL THEN y ELSE x END`.
There's a corner case there, what if a matching row in the locations table is found, but the location column actually contains a NULL.
To more precisely match your specification, you could test for a value of "0" in the to_id column of the orders table:
SELECT u.name uname
, IF(o.from_id = 0, u.address, fl.location) flocation
, IF(o.to_id = 0, u.address, tl.location) tlocation
FROM orders o
LEFT
JOIN users u
ON u.id = o.user_id
LEFT
JOIN locations fl
ON fl.id = o.from_id
LEFT
JOIN locations tl
ON tl.id = o.to_id
But there's a corner case there; what if there is a row in locations that has an id value of "0".
The normative pattern would be to test for whether a row was returned from the locations table by checking a column that is guaranteed to be NOT NULL if a match is found, and will be NULL if a match is not found:
SELECT u.name uname
, IF(fl.id IS NULL, u.address, fl.location) flocation
, IF(tl.id IS NULL, u.address, tl.location) tlocation
FROM orders o
LEFT
JOIN users u
ON u.id = o.user_id
LEFT
JOIN locations fl
ON fl.id = o.from_id
LEFT
JOIN locations tl
ON tl.id = o.to_id
All three of those queries will return identical results for the "normal" cases; they just differ in how the "corner cases" are handled.
In summary:
use LEFT [OUTER] JOIN operations with orders as the driving table.
use conditional expressions in the SELECT list to determine which column value to return
I have the following table structure:
table: user
user_id | join_date
101 | '2012-4-13'
102 | '2012-6-4'
103 | NULL
104 | NULL
table: job
job_id | user_id
20 | 101
21 | 103
I want to return a single result set of user records that contains the following:
The results of all users that have a match in the job table.
The results of all users where the join_date is null, AND do not have a record in the job table.
Here is the result set of user_id's I would like to see:
user_id
101 <-- has a job
103 <-- has a job
104 <-- never joined, AND also does not have a job
Do I need to do this using Common Table Expressions or can I do this in a subquery?
Please note I've left out actual details for the sake of brevity. This is not a simple case of an inner join.
Here are each of my queries running separately:
THIS IS THE QUERY THAT RETURNS THE PROPER RESULT FOR #1
SELECT DISTINCT u.*
FROM [user] u, job uj
WHERE u.user_id = uj.user_id
THIS IS THE QUERY THAT RETURNS THE PROPER RESULT FOR #2
SELECT DISTINCT u.*
FROM [user] u
FULL JOIN job uj ON u.user_id = uj.user_id
WHERE u.join_date IS NULL AND uj.user_id IS NULL
This should do it:
select u.user_id from [user] u
left join job j on u.user_id = j.user_id
where j.job_id is not null or (j.job_id is null and u.join_date is null)
Edit:
This is logically the same and "simpler" but the first way "reads" like the problem statement:
select u.user_id from [user] u
left join job j on u.user_id = j.user_id
where j.job_id is not null or u.join_date is null
To get `101, 103 & 104, try:
select distinct u.user_id
from [user] u LEFT JOIN job j
on u.user_id = j.user_id
where (u.join_date is not null and j.job_id is not null)
or u.join_date is null