twitter-style follower/following/friend sql query - mysql

I'm working on a twitter type of following system. I'm joining two tables, users and followers to get the first and lastname of users who are in the followers table. Then I'm running an inner join on the followers table to capture follower and friend relationships. I'm displaying the results as followers (who follows you), following (who you follow), and friends (mutual following).
With the query below, I'm only able to show the name of the user who wants to see their friends. I'd like to show the FRIENDS of the user, not the user's own name, but can't figure out how to get the users table to do double duty--that is, show me the name of the user and the name of their friend, or just the friend's name.
Thanks.
SELECT users.id, users.firstname, users.lastname, followers.follower_user_id, followers.followee_user_id
FROM users
JOIN followers ON followers.follower_user_id = users.id
INNER JOIN followers ff ON followers.followee_user_id = ff.follower_user_id AND followers.follower_user_id = ff.followee_user_id

I believe that your schema requires a union table to assemble the information you need; and it may be more efficient to do this in multiple tables. To maintain a separate table of followers with (possible) duplicate information from users may also be undesireable. A more efficient schema would be:
mysql> select * from users;
+-----+------------+---------+
| uid | fname | lname |
+-----+------------+---------+
| 1 | Phillip | Jackson |
| 2 | Another | Name |
| 3 | Some Crazy | User |
| 4 | Nameless | Person |
+-----+------------+---------+
4 rows in set (0.00 sec)
mysql> select * from follows;
+---------+-----------+
| user_id | follow_id |
+---------+-----------+
| 1 | 4 |
| 2 | 3 |
| 3 | 2 |
| 4 | 2 |
+---------+-----------+
4 rows in set (0.00 sec)
And then your query would look like:
select users.uid,
users.fname,
users.lname,
u.uid,
u.fname,
u.lname from users
inner join follows f on (f.user_id=users.uid)
inner join users u on (u.uid=f.follow_id)
Which returns:
mysql> select users.uid,
-> users.fname,
-> users.lname,
-> u.uid,
-> u.fname,
-> u.lname from users
-> inner join follows f on (f.user_id=users.uid)
-> inner join users u on (u.uid=f.follow_id);
+-----+------------+---------+-----+------------+--------+
| uid | fname | lname | uid | fname | lname |
+-----+------------+---------+-----+------------+--------+
| 1 | Phillip | Jackson | 4 | Nameless | Person |
| 4 | Nameless | Person | 2 | Another | Name |
| 2 | Another | Name | 3 | Some Crazy | User |
| 3 | Some Crazy | User | 2 | Another | Name |
+-----+------------+---------+-----+------------+--------+
4 rows in set (0.00 sec)

SELECT u.id, u.first_name, u.last_name, uf.id, uf.first_name, uf.last_name
FROM users u
JOIN followers f
ON f.follower_user_id = u.id
JOIN followers ff
ON (ff.followee_user_id, ff.follower_user_id) = (f.follower_user_id, f.followee_user_id)
JOIN users uf
ON uf.id = f.followee_user_id

Related

Select entries in relation to fields in another table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have the following tables, Table1 shows all users, and Table2 shows a teacher-to-student relationship:
Table1 (users):
ID name age
-------------
1 Al 30
2 Bob 5
3 Cam 6
4 Dan 7
Table2 (classes):
teacher_id student_id
----------------------
1 2
1 3
Essentially, this shows Al (id=1) is the teacher of Bob (id=2) and also Cam (id=3). I want to return users rows of a teacher and his students. For example, if I'm looking for Al (id=1), I want to return users info of Al and his students (Bob and Cam). What I thought was
SELECT * FROM users
LEFT JOIN classes ON users.id=classes.teacher_id
WHERE id=1 OR teacher_id=1
But I know it must be wrong because "teacher_id=1" doesn't get values from classes.student_id. It should be "... WHERE id={the student_id's from classes where teacher_id=1}". How do you do this?
You can use a Query like this to show the students search by teachers name.
SELECT u.* FROM users u
where u.id IN (
SELECT t.student_id
FROM classes t
LEFT JOIN users tu ON t.`teacher_id` = tu.id
WHERE tu.name = 'Al'
);
Sample
MariaDB [bernd]> SELECT * from users;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | Al | 30 |
| 2 | Bob | 5 |
| 3 | Cam | 6 |
| 4 | Dan | 7 |
+----+------+------+
4 rows in set (0.00 sec)
MariaDB [bernd]> SELECT * FROM classes;
+----+------------+------------+
| id | teacher_id | student_id |
+----+------------+------------+
| 1 | 1 | 2 |
| 2 | 1 | 3 |
+----+------------+------------+
2 rows in set (0.00 sec)
MariaDB [bernd]> SELECT u.* FROM users u
-> where u.id IN (
-> SELECT t.student_id
-> FROM classes t
-> LEFT JOIN users tu ON t.`teacher_id` = tu.id
-> WHERE tu.name = 'Al'
-> );
+----+------+------+
| id | name | age |
+----+------+------+
| 2 | Bob | 5 |
| 3 | Cam | 6 |
+----+------+------+
2 rows in set (0.00 sec)
MariaDB [bernd]>
try this query
SELECT * FROM classes
LEFT JOIN user ON classes.teacher_id= users.id
WHERE classes.teacher_id=1 or user.id = 1
You can join classes to 2 copies of users:
select u1.name teacher, u2.name student
from classes c
inner join users u1 on u1.id = c.teacher_id
inner join users u2 on u2.id = c.student_id
See the demo.
Results:
| teacher | student |
| ------- | ------- |
| Al | Bob |
| Al | Cam |
Or if you want a comma separated list of all the students of each teacher:
select u1.name teacher, group_concat(u2.name) students
from classes c
inner join users u1 on u1.id = c.teacher_id
inner join users u2 on u2.id = c.student_id
group by teacher
See the demo.
Results:
| teacher | students |
| ------- | -------- |
| Al | Bob,Cam |

How to count rows in nested tables with one SQL query?

I have three tables. Each User can have multiple Subscriptions and each Subscription can have multiple Payments.
Me goal is to count all Payments for a single User using one SQL query. Is it possible to do and how?
In the case below, The result for a User with id 1 should be 2 (because the User has two Payments)
Users
+----+------+
| Id | Name |
+----+------+
| 1 | John |
+----+------+
Subscriptions
+----+--------+-----------+
| Id | userId | data |
+----+--------+-----------+
| 1 | 1 | some data |
+----+--------+-----------+
| 2 | 1 | some data |
+----+--------+-----------+
Payments
+----+----------------+--------+
| Id | subscriptionId | amount |
+----+----------------+--------+
| 1 | 1 | 30 |
+----+----------------+--------+
| 2 | 2 | 50 |
+----+----------------+--------+
try like below by using join and aggregation
SELECT u.id, u.Name, COUNT(p.id) AS numberofpayment
FROM users u
Left JOIN Subscriptions s ON u.Id=s.userId
Left JOIN Payments p ON s.id=p.subscriptionId
GROUP BY u.id, u.Name
You can try to do something like this:
SELECT COUNT(p.Id) AS PaymentCount
FROM Users u
LEFT JOIN Subscriptions s ON u.Id=s.userId
LEFT JOIN Payments p ON s.id=p.subscriptionId
WHERE u.Id = #yourUserID
Pay attention on COUNT(p.Id) - it means count of existing payments.
PS: this answer for #Kickstart.

Constrain SQL results with GROUP_CONCAT and LEFT JOIN

I need to constrain a user query regarding user roles (user or admin).
In a datatbase there is a table user_scope containing all user ids and the assigned roles (every user has one entry with a 1 for user, and some have a second entry with 2 for admin). I can't change that database architecture right now.
This is my SELECT on a table users so far which joins data from other tables
SELECT
u.id,
GROUP_CONCAT(DISTINCT scopes.scope ORDER BY scopes.id ASC SEPARATOR ' ') as scope
FROM users as u
LEFT JOIN user_scope on user_scope.user_id = u.id
LEFT JOIN scopes on scopes.id = user_scope.scope_id
The table users
+----+--------+
| id | parent |
+----+--------+
| 1 | Alex |
| 2 | Marc |
| 3 | Cath |
+----+--------+
The table user_scope
+---------+----------+
| user_id | scope_id |
+---------+----------+
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 3 | 1 |
+---------+----------+
The table scopes
+----+--------+
| id | scope |
+----+--------+
| 1 | user |
| 2 | admin |
+----+--------+
This will yield something like this
+----+------------+
| id | scope |
+----+------------+
| 1 | user admin |
| 2 | user |
| 3 | user |
+----+------------+
The problem arises when I want to filter the results based on one particular role. I tried this
SELECT
u.id,
GROUP_CONCAT(DISTINCT scopes.scope ORDER BY scopes.id ASC SEPARATOR ' ') as scope
FROM users as u
LEFT JOIN user_scope on user_scope.user_id = u.id
LEFT JOIN scopes on scopes.id = user_scope.scope_id and scopes.id = 2
Or 1 respectively. However, that will not reduce the number of returned rows but will only NULL the rows with users that have not the scope admin. I also tried to use a CASE but I can't neither use this in a WHERE statement.
How do I reduce the rows returned in this context? Help is REALLY appreciated.
Use a having clause:
SELECT u.id,
GROUP_CONCAT(DISTINCT s.scope ORDER BY s.id ASC SEPARATOR ' ') as scopes
FROM users u LEFT JOIN
user_scope us
ON us.user_id = u.id LEFT JOIN
scopes s
ON s.id = us.scope_id
GROUP BY u.id
HAVING MAX(s.id = 2) > 0;

MySQL - Left Outer Join not updating original table

I created a toy dataset where I am trying to count the number of posts for each user. I seem to be getting the correct count values but the count column in the users table is not updated with the values.
I'm new to mysql and very confused! Can somebody tell me what I'm doing wrong?
users:
+---------+------+-------+
| user_id | user | pword |
+---------+------+-------+
| 1 | Amy | abcd |
| 2 | Jess | efgh |
| 3 | Lori | ijkl |
+---------+------+-------+
posts:
+---------+-------------+------+
| post_id | post | user |
+---------+-------------+------+
| 1 | hi | Lori |
| 2 | hello | Jess |
| 3 | hello again | Jess |
| 4 | and again | Jess |
+---------+-------------+------+
mysql> ALTER TABLE users ADD COLUMN post_count INT;
mysql> SELECT u.user_id, COUNT(p.user) AS post_count FROM users u LEFT JOIN posts p ON u.user LIKE p.user GROUP BY u.user_id;
+---------+------------+
| user_id | post_count |
+---------+------------+
| 1 | 0 |
| 2 | 3 |
| 3 | 1 |
+---------+------------+
mysql> SELECT * FROM users;
+---------+------+-------+------------+
| user_id | user | pword | post_count |
+---------+------+-------+------------+
| 1 | Amy | abcd | NULL |
| 2 | Jess | efgh | NULL |
| 3 | Lori | ijkl | NULL |
+---------+------+-------+------------+
Thanks!!
Please try the following...
UPDATE users
JOIN ( SELECT u.user_id AS user_id,
COUNT( p.user ) AS post_count
FROM users u
LEFT JOIN posts p ON u.user LIKE p.user
GROUP BY u.user_id ) postCountFinder
ON users.user_id = postCountFinder.user_id
SET users.post_count = postCountFinder.post_count;
This question takes your list of users and post counts obtained from the following...
SELECT u.user_id,
COUNT( p.user ) AS post_count
FROM users u
LEFT JOIN posts p ON u.user LIKE p.user
GROUP BY u.user_id;
... and performs an INNER JOIN with Users on shared value of user_id, creating a dataset with every row from users having the corresponding count tacked on the end.
We then use the SET command to set the empty post_count from users to its corresponding joined count.
If you have any questions or comments, thenplease feel free to post a Comment accordingly.
You need update statement to update the value in the newly added column.Try this:
Update usr
set usr.post_count=tbl.post_count
from users usr
inner join
(select u.user_id,COUNT(p.user)
AS post_count FROM users u
LEFT JOIN posts p ON u.user LIKE p.user GROUP BY u.user_id ) tbl
on tbl.user_id=usr.user_id

MySQL GroupBy with null/zero results

I'm currently writing a ticket system that has three tables
one for users:
users
+----+-----------+----------+
| ID | FirstName | LastName |
+----+-----------+----------+
| 1 | First | User |
| 2 | Second | User |
| 3 | Third | User |
| 4 | Fourth | User |
| 5 | Fifth | User |
+----+-----------+----------+
one for tickets:
ticket
+----+---------------+
| ID | TicketSubject |
+----+---------------+
| 1 | Ticket #1 |
| 2 | Ticket #2 |
| 3 | Ticket #3 |
| 4 | Ticket #4 |
+----+---------------+
and one to assign users to tickets to action (can be more than one user per ticket):
ticket_assigned
+----+----------+--------+
| ID | TicketID | UserID |
+----+----------+--------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 1 |
| 4 | 3 | 5 |
| 5 | 3 | 3 |
+----+----------+--------+
I'm trying to create a summary to show each user, and how many tickets they have assigned to them, example:
+------------+-------+
| Name | Count |
+------------+-------+
| First | 2 |
| Second | 1 |
| Third | 1 |
| Fourth | 0 |
| Fifth | 1 |
| Unassigned | 2 |
+------------+-------+
Note that the last entry is "unassigned", this is the number of records in the ticket table that DONT appear in the ticket_assigned table (thus being, unassigned). Also further note that user "Fourth" is zero, in that that user has no records in the ticket_assigned table.
Here is the current MySQL query I am using:
SELECT
CASE
WHEN users.FirstName IS NULL
THEN 'Unassigned'
ELSE users.FirstName
END as 'UserName',
COUNT(*) as 'TicketCount'
FROM tickets
LEFT OUTER JOIN ticket_assigned ON tickets.ticket_id = ticket_assigned.ticket_id
LEFT OUTER JOIN users ON ticket_assigned.user_id = users.user_id
GROUP BY ticket_assigned.user_id
ORDER BY UserName;
Problem with this is that it's not showing any of the users that don't feature in the ticket_assigned table, I'm essentially getting this:
+------------+-------+
| Name | Count |
+------------+-------+
| First | 2 |
| Second | 1 |
| Third | 1 |
| Fifth | 1 |
| Unassigned | 2 |
+------------+-------+
Is anyone able to assist and tell me how I can modify my query to include users that have no records in the ticket_assigned table? Thanks in advance!
Use a LEFT JOIN with a subquery to aggregate tickets:
SELECT t1.FirstName,
COALESCE(t2.ticket_count, 0) AS num_tickets
FROM users t1
LEFT JOIN
(
SELECT UserID, COUNT(*) AS ticket_count
FROM ticket_assigned
GROUP BY UserID
) t2
ON t1.ID = t2.UserID
UNION ALL
SELECT 'Unassigned', COUNT(*)
FROM tickets t
WHERE NOT EXISTS (SELECT 1 FROM tickets_assigned ta
WHERE ta.ticketId = t.id)
In MySQL, I think you need a left join and union all:
select u.id, u.firstname, count(ta.userId) as num_tickets
from users u left join
tickets_assigned ta
on ta.userId = u.id
group by u.id, u.firstname
union all
select NULL, 'Unassigned', count(*)
from tickets t
where not exists (select 1
from tickets_assigned
where ta.ticketId = t.id
);
I included the u.id in the aggregations. I'm uncomfortable just aggregating (and reporting) by first name, because different people frequently have the same first name, even in a relatively small group.
SELECT
u2.Firstname, IFNULL(tmp.count, 0) AS count
FROM users u2
LEFT JOIN (
SELECT u.id, u.Firstname, COUNT(1) as count
FROM ticket_assigned ta
LEFT JOIN ticket t ON t.id = ta.ticketID
LEFT JOIN users u ON u.id = ta.userID
GROUP BY u.id
) tmp ON tmp.id = u2.id
UNION
SELECT
'Unassigned', count(1) AS count
FROM ticket
WHERE id NOT IN (SELECT ticketid FROM ticket_assigned)