Get unique records based on multi-join with conditionals - mysql

I have 4 tables: posts, users, mentions, following
posts
----------------------------
id | user_id | post_text
1 1 foo
2 1 bar
3 2 hello
4 3 jason
users
------------
id | name
1 jason
2 nicole
3 frank
mentions
--------------------------
id | post_id | user_id
1 4 1
following
-------------------------------------------------
id | user_id | user_id_of_user_being_followed
1 1 2
posts includes the user_id of the user who posted some text
users has the user id and name of the user
mentions has the post id and user id of any post which has mentioned 1 or more other users
following has a the user id and the user they are following (user can follow 0 to many users)
What I'm trying to do is return all posts from users a that a given user follows, PLUS any posts that have mentioned that user (whether or not the given user is following), without returning any duplicates.
SELECT p.id, p.post, u.name,
FROM following f
JOIN posts p ON f.following = p.user_id
JOIN users u ON u.id = p.user_id
WHERE f.user_id = :user;
The above returns all posts from users that a given user is following, but I'm struggling figuring out how to include mentions as well (remember, a user does not have to follow someone to be able to see the post they've been mention in).
UPDATE:
Thanks to John R I was able to figure this out:
SELECT DISTINCT(p.id), p.post, u.name
FROM posts p
LEFT JOIN following f ON f.following = p.user_id
LEFT JOIN mentions m ON m.posts_id = p.id
JOIN users u ON u.id = p.user_id
WHERE (f.user_id = :user_id OR m.user_id = :user_id)

if i understand your querstion correctly you would want a left join to include any mentions.. but not filter out any followers/posts
if you can add some sample data to play with I can make sure its working how you want it to...
SELECT
if(p.id is not null, p.id, p1.id) as post_id,
if(p.post is not null, p.post, p1.post) as post_text,
u.username, m.id, m.user_id
FROM posts p
JOIN users u on u.id = p.user_id
JOIN following f on f.user_id_of_user_being_followed = u.id
LEFT JOIN mentions m on m.user_id = f.user_id
LEFT JOIN posts p1 on p1.id = m.post_id
WHERE f.user_id = :user or m.user_id = :user;
I left join mentions to the post made and also when the user_id in the mention table is equal to the specified user to filter out other users. the left join shouldn't change the number of rows returned.. but only include any mentions
EDIT: WORKING FIDDLE
after playing around with it I realised it was trying to put all of the data into one row.. try this:
(
SELECT p.id, p.post_text, u.name
FROM posts p
JOIN users u on u.id = p.user_id
JOIN following f on f.user_id_of_user_being_followed = u.id
WHERE f.user_id = 1
)
UNION
(
SELECT p.id, p.post_text, u.name
FROM following f
JOIN mentions m on m.user_id = f.user_id
JOIN posts p on p.id = m.post_id
join users u on u.id = p.user_id
WHERE f.user_id = 1
);

Maybe you inherited this db; but the last table is not really in line with good data normalization. The table should be the id and following_id; as set up you'll eventually run out of columns (or have to keep adding them when a user gets an error) - new users won't be able to follow anyone.

Related

Checking result count

I am working on a project that requires getting posts from my database. I am trying to get the posts that are posted by the user and other users unless they are private.
My SQL query is
SELECT . . .,COUNT(F.user_id) AS is_following FROM posts AS P INNER JOIN users AS U ON U.id = P.user_id LEFT JOIN followers AS F ON F.user_id = :userid AND F.following_id = U.id WHERE *If matches search* AND U.private = 0 OR U.id = :userid OR is_following = 1 ORDER BY. . .
I am selecting where the user is following the posters and counting it if 0 it means they are not following and 1 would mean 1 result, therefore, following them. Also I am checking if private is 0 meaning the account is public.
The problem is while I have the is_following = 1 it returns no results if I remove it though it does return results but not all.
Tables:
users: id|username|private|...
followers: userid|followingid
posts: id|userid|body
Content:
users:
user1|test1|1 (This is the user running the request)
user2|test2|1
user3|test3|1
followers:
user1|user2 (User searching follows test2)
posts:
post1|user1|HelloWorld
post2|user2|Hello_World
post3|user3|Hello-World
The expected outcome is to return post1 and post2 but not post3 since user3 is private (private is set to true/1) and user1 does no follow test3, but it should display user2's post since user1 follows user2
If I understand correct.
There are two kinds of groups
private is 0 meaning the account is public
private is 1 need to find the following User on followers table.
A simple way you can make two query one is for private is 0 , another is for private is 1 ,then use UNION ALL combine those.
SELECT * FROM
(
SELECT p.*
FROM users as u
INNER JOIN posts AS P ON p.userid = u.id
WHERE u.private = 0
UNION ALL
SELECT p.*
FROM users as u
INNER JOIN posts AS P ON p.userid = u.id
RIGHT JOIN followers AS F ON F.followingid = u.id
WHERE u.private = 1 AND f.userid = :userid
)t
Or use exists to check it
SELECT p.*
FROM users as u
INNER JOIN posts AS P ON p.userid = u.id
WHERE u.private = 0 OR exists
(
SELECT 1
FROM followers AS F LEFT JOIN users ON F.followingid = users.id
WHERE u.id = users.id
and users.private
and f.userid = :userid
)
sqlfiddle:http://sqlfiddle.com/#!9/50ef05/1
EDIT
if you want to get the user is or isn't is_following,you could follow like this.
then you could find which user have been is_following on outer where clause .
SELECT * FROM
(
SELECT p.*,0 as 'is_following'
FROM users as u
INNER JOIN posts AS P ON p.userid = u.id
WHERE u.private = 0
UNION ALL
SELECT p.*,1
FROM users as u
INNER JOIN posts AS P ON p.userid = u.id
RIGHT JOIN followers AS F ON F.followingid = u.id
WHERE u.private = 1 AND f.userid = 'user1'
)t
/*where is_following = 1*/
sqlfiddle:http://sqlfiddle.com/#!9/b7f6c/10
The reason it returns no results when you have the is_following condition is that you are trying to use an alias in a WHERE clause which is not permitted by MySQL. Replace is_following with COUNT(F.user_id) and it should work.

select all posts with different post id in mysql

SELECT *
FROM post p
JOIN user u ON p.user_id = u.id
JOIN friendships f ON f.friend_id = u.id
WHERE f.user_id = 1 OR u.id = 1
ORDER BY p.created_at DESC;
working on a projects where I'm trying to get all the post of the user as well as the user currently on.
So far i have this query working but is giving me duplicate posts of users.id = 1
is a user self join many to many where each user become friends and each user has their posts
The problem is probably because you select all columns from all tables involved.
Now for every new friendship with friend_id=1 you will receive a new record, with post duplicated. I guess you need:
select distinct p.*

How to exclude rows when using a LEFT JOIN (MySQL)

I have users with many posts. I want to build an SQL query that would do the following in 1 query (no subquery), and hopefully no unions if possible. I know I can do this with union but I want to learn if this can be done using only joins.
I want to get a list of distinct active users who:
have no posts
have no approved posts
Here's what I have so far:
SELECT DISTINCT u.*
FROM users u
LEFT JOIN posts p
ON p.user_id = u.id
LEFT JOIN posts p2
ON p2.user_id = u.id
WHERE u.status = 'active'
AND (p.status IS NULL
OR p2.status != 'approved');
The problem is when a user has multiple posts and one is active. This will still return the user which I do not want. If a user has an active post, he should be removed from the result set. Any ideas?
Here's what the data looks like:
mysql> select * from users;
+----+---------+
| id | status |
+----+---------+
| 1 | active |
| 2 | pending |
| 3 | pending |
| 4 | active |
| 5 | active |
+----+---------+
5 rows in set (0.00 sec)
mysql> select * from posts;
+----+---------+----------+
| id | user_id | status |
+----+---------+----------+
| 1 | 1 | approved |
| 2 | 1 | pending |
| 3 | 4 | pending |
+----+---------+----------+
3 rows in set (0.00 sec)
The answer here should be only users 4 and 5. 4 doesn't have an approved post and 5 doesn't have a post. It should not include 1, which has an approved post.
Not exists:
SELECT u.*
FROM users u
WHERE NOT EXISTS (
SELECT 1
FROM posts p
WHERE p.user_id = u.id AND p.status = 'approved');
Or equivalent LEFT JOIN
SELECT u.*
FROM users u
LEFT JOIN posts p
ON p.user_id = u.id AND p.status = 'approved'
WHERE p.user_id IS NULL;
Taking your requirements and translating them literally to SQL, I get this:
SELECT users.id,
COUNT(posts.id) as posts_count,
COUNT(approved_posts.id) as approved_posts_count
FROM users
LEFT JOIN posts ON posts.user_id = users.id
LEFT JOIN posts approved_posts
ON approved_posts.status = 'approved'
AND approved_posts.user_id = users.id
WHERE users.status = "active"
GROUP BY users.id
HAVING (posts_count = 0 OR approved_posts_count = 0);
For your test data above, this returns:
4|1|0
5|0|0
i.e. users with ids 4 and 5, the first of which has 1 post but no approved posts and the second of which has no posts.
However, it seems to me that this can be simplified since any user that has no approved posts will also have no posts, so the union of conditions is unnecessary.
In that case, the SQL is simply:
SELECT users.id,
COUNT(approved_posts.id) as approved_posts_count
FROM users
LEFT JOIN posts approved_posts
ON approved_posts.status = 'approved'
AND approved_posts.user_id = users.id
WHERE users.status = "active"
GROUP BY users.id
HAVING approved_posts_count = 0;
This also returns the same two users. Am I missing something?
Please explain why you don't want JOINs or UNIONs. If it is because of performance, then consider the following:
CREATE TABLE t ( PRIMARY KEY(user_id) )
SELECT user_id, MIN(status) AS z
FROM Posts
GROUP BY user_id;
SELECT u.id AS user,
IFNULL(z, 'no_posts') AS status
FROM users u
WHERE u.status = 'active'
LEFT JOIN t ON t.user_id = u.id
HAVING status != 'approved';
It will make only one pass over each table, thereby being reasonably efficient (considering the complexity of the query).
This one may help:
SELECT DISTINCT u.*
FROM users u
LEFT JOIN posts p ON 1=1
-- matches only if user has any post
AND p.user_id = u.id
-- matches only if user has any active post
AND p.status = 'approved'
WHERE 1=1
-- matches only active users
AND u.status = 'active'
-- matches only users with no matches on the LEFT JOIN
AND p.status IS NULL
;
I think this should be easy.
SELECT u.`id`, u.`status` FROM `users` u
LEFT OUTER JOIN `post` p ON p.`user_id` = u.`id` AND p.`status` = 'approved'
WHERE u.`status` = 'active' AND p.`id` IS NULL
Gives a result of 4 & 5.
[Edit] Just wanted to add why this works:
u.status = 'active'
This results into exclusion of all users that are not active.
p.status = 'approved'
This excludes all posts that are approved.
Hence, by using these two lines, we have excluded all users that qualify as approved for your criteria.
[Edit 2]
If you also need to know how many pending and how many approved, here is an updated version:
SELECT u.`id`, u.`status`, SUM(IF(p.`status` = 'approved', 1, 0)) AS `Approved_Posts`, SUM(IF(p.`status` = 'pending', 1, 0)) AS `Pending_Posts`
FROM `test_users` u
LEFT OUTER JOIN `test_post` p ON p.`user_id` = u.`id`
WHERE u.`status` = 'active'
GROUP BY u.`id`
HAVING SUM(IF(p.`id` IS NOT NULL, 1, 0))
Try this
SELECT DISTINCT u.*
FROM users u LEFT JOIN posts p
ON p.user_id = u.id
WHERE p.status IS NULL
OR p.status != 'approved';
Can you try with the below query:
SELECT DISTINCT u.*
FROM users u
LEFT JOIN posts p
ON p.user_id = u.id
WHERE
u.status = 'active' AND (
p.user_id IS NULL
OR p.status != 'approved');
EDIT
As per the updated question, the above query will include User 1. If we want to prevent that, and don't want to use inner query, we can use group_concat function of MySQL to get all the (distinct) statuses and see if it contains 'active' status, below query should give the desired output:
SELECT u.id, group_concat(distinct p.status) as statuses
FROM users u
LEFT JOIN posts p
ON u.id = p.user_id
WHERE
u.status = 'active'
group by u.id
having (statuses is null or statuses not like '%approved%');

Joining table issues

I have 3 tables that I'm looking to join:
pictures
--------
id user_id link
users
-----
id name
votes
-----
id user_id picture_id
Want I want to do is find the total number of votes for every picture for the specific user logged in. Pretty much I loop every picture out and if the user has votes on the picture they can't vote on it again.
Desired output:
---------------
id user_id link user_name total_votes
1 5 [link] Sean 5
So far I have something like this:
SELECT
p.*, u.username, d.total_votes
FROM pictures p
LEFT JOIN users u
ON p.user_id = u.id
LEFT JOIN
(
select id, picture_id, count(id) as has_voted from votes
) d on d.picture_id = p.id
I get all the pictures but all the votes are being added up on the first record.
EDIT
Sorry for being so unclear
So this is every image in my database. Say I'm logged in as Sean (user_id 1) I want to show how many times I votes on each image.
user_id is who uploaded the image.
(Updated) Try:
Select p.id, p.user_id, p.link, u.name, count(v.id) As total_votes
from pictures p
join users u on p.user_id = u.id
left join votes v on p.id = v.picture_id and v.user_id = ?
group by p.id
Try this
Select p.id, p.user_id, p.link, u.name,(CASE count(v.id) WHEN NULL THEN 0 ELSE count(v.id) END ) as total_votes
from pictures p
join users u on p.user_id = u.id
join votes v on (v.user_id = u.id and v.picture_id = p.id)
where v.id is not null
group by p.id

Left Join 2 tables on 1 table

It must be pretty easy, but i can't think of any solution nor can I find an answer somewhere...
I got the table 'users'
and one table 'blogs' (user_id, blogpost)
and one table 'messages' (user_id, message)
I'd like to have the following result:
User | count(blogs) | count(messages)
Jim | 0 | 3
Tom | 2 | 3
Tim | 0 | 1
Foo | 2 | 0
So what I did is:
SELECT u.id, count(b.id), count(m.id) FROM `users` u
LEFT JOIN blogs b ON b.user_id = u.id
LEFT JOIN messages m ON m.user_id = u.id
GROUP BY u.id
It obviously doesn't work, because the second left join relates to blogs not users. Any suggestions?
First, if you only want the count value, you could do subselects:
select u.id, u.name,
(select count(b.id) from blogs where userid = u.id) as 'blogs',
(select count(m.id) from messages where userid = u.id) as 'messages'
from 'users'
Note that this is just a plain sql example, I have no mysql db here to test it right now.
On the other hand, you could do a join, but you should use an outer join to include users without blogs but with messages. That would imply that you get several users multiple times, so a group by would be helpful.
If you use an aggregate function in a select, SQL will collapse all your rows into a single row.
In order to get more than 1 row out you must use a group by clause.
Then SQL will generate totals per user.
Fastest option
SELECT
u.id
, (SELECT(COUNT(*) FROM blogs b WHERE b.user_id = u.id) as blogcount
, (SELECT(COUNT(*) FROM messages m WHERE m.user_id = u.id) as messagecount
FROM users u
Why you code does not work
SELECT u.id, count(b.id), count(m.id)
FROM users u
LEFT JOIN blogs b ON b.user_id = u.id <<-- 3 matches multiplies # of rows *3
LEFT JOIN messages m ON m.user_id = u.id <<-- 5 matches multiplies # of rows *5
GROUP BY u.id
The count will be off, because you are counting duplicate items.
Simple fix, but will be slower than option 1
If you only count distinct id's, you will get the correct counts:
SELECT u.id, count(DISTNICT b.id), count(DISTINCT m.id)
FROM users u
LEFT JOIN blogs b ON b.user_id = u.id
LEFT JOIN messages m ON m.user_id = u.id
GROUP BY u.id