I have two tables activity_log and user_followers. I have to join these two tables and get the activity of a user with the user activity that he is following (let's say user_id 6 is following user_id 4). But the below query only returning the activity of the user having the id of 6. I want to get the activity of the user with the id of 6 plus the activity of the user he is following.
Query
SELECT activity_log.*
FROM activity_log
join user_followers ON activity_log.user_id = user_followers.follow_id
AND activity_log.user_id = 6;
activity_log:
user_followers:
Sounds like you want user_id 6's activity plus activity of users he's following. That would be:
SELECT activity_log.*
FROM activity_log
LEFT OUTER JOIN user_followers
ON activity_log.user_id = user_followers.follow_id
WHERE (activity_log.user_id = 6 OR ISNULL(user_followers.user_id,0) = 6);
The LEFT OUTER is used in case user_id 6 has no followers.
Related
I'm building an activities website and I want to show on user's profiles the common activities I'm going to with this other user.
So I have a table as follow :
Every time a user clicks "join" on an activity page, a new row is added to this table with :
the user_id who joined
the activity_id (event) attended
So on each user's profile, I want to list the different activities we have in common.
If the user's IDs are for example 1 and 10, then I can list the activities they go to with this query :
SELECT * FROM `activity_user` WHERE user_id IN(1, 10)
However, how to update this query to return only activities IDs they have in common?
It's a simple JOIN with the same table:
SELECT a1.activity_id
FROM activity_user a1
JOIN activity_user a2 ON a2.activity_id = a1.activity_id
WHERE a1.user_id = 1
AND a2.user_id = 10
Another way is to GROUP BY activity_id and only return activities, which both users have, by counting the rows per group:
SELECT activity_id
FROM activity_user
WHERE user_id IN(1, 10)
GROUP BY activity_id
HAVING COUNT(*) = 2
If the number of user is only limited to 2, this is quite simple using intersect clause -
SELECT activity_id
FROM `activity_user`
WHERE user_id = 1
INTERSECT
SELECT activity_id
FROM `activity_user`
WHERE user_id = 10
For two users you can join two SELECT against same table on activity_id
SELECT a.activity_id
FROM test a
JOIN (SELECT activity_idFROM test WHERE user_id = 10) b ON a.activity_id = b.activity_id
WHERE a.user_id = 1
The main table has 4 columns:
User Activity Table
userActivityId userId therapistId activityId
1 1 1 1
Each of these columns is a table and these values are all foreign keys.
Basically im trying to run a query that will join to the users table and pull their first and last name based off the user Id.Same thing with therapist - join to the therapist table, pull first + last name.And finally Join to the Activity table and pull the activity name and path from the activity Id
The other tables look like this:
User Table
userId fName lName
Therapist Table
therapistId therapistFirstName therapistLastName
Activity Table
activityId activityTitle activityPath
So far my query looks like
SELECT
User_Activities.userId,
User_Activities.therapistId,
User_Activities.activityId,
Activities.activityTitle,
Activities.activityPath,
Users.fName,
users.lName,
Therapists.therapistFirstName,
Therapists.therapistLastName
FROM
User_Activities
INNER JOIN Users
ON User_Activities.userId = Users.userId
INNER JOIN Therapists ON
User_Activities.therapistId = Therapists.therapistId
INNER JOIN Activities ON
Activities.activityId = User_Activities.userActivityId
WHERE
User_Activities.userId = 1;
When I run this query It only returns 1 row as a result. However there are two activities in the User_Activites table assigned to userId 1.
If I change : INNER JOIN Activities ON
Activities.activityId = User_Activities.userActivityId
from an INNER JOIN to the LEFT JOIN it will display the second row, however the activityTitle and activityPath will be displayed as NULL in the second row.
userActivityId userId therapistId activityId activityId activityTitle activityPath fName lName therapistFirstName therapistLastName
1 1 1 1 1 Brain GZZ0zpUQ S C M D
11 1 1 1 NULL NULL NULL S C M D
You have pretty much answered your question. The second activity does not have a valid ActivityId.
If you want all activities for a user, then you should phrase the query as:
SELECT . . .
FROM Users u LEFT JOIN
User_Activities ua
ON ua.userId = u.userId LEFT JOIN
Therapists t
ON ua.therapistId = t.therapistId LEFT JOIN
Activities a
ON a.activityId = ua.userActivityId
WHERE u.userId = 1;
You want to start with the table where you want to keep all the rows. Then use LEFT JOIN to bring in other tables.
Two other changes of note:
Table aliases are used to simplify reading and writing the query. The SELECT needs to change to use the aliases.
The WHERE clause refers to the Users table rather than UserActivities.
I need to create a mysql query that should get only one row result from two tables.
I have a table called users and a table called users_sports.
SELECT * FROM users WHERE id = 71
SELECT user_sport_sport FROM users.sports WHERE user_sport_user = 71
I would need a query result in only one row that has all users values from table users and all user_sport_sport values for a specific user.
For example: id=71, username="mike2"...sport1="Basketball",sport2="Soccer".
EDIT tb_user_sports
Table users
Table tb_sports
You can use group_concat to fetch all the sports for that user id
select u.name, GROUP_CONCAT(ts.sport_name) sports from users u
join tb_user_sports tus ON tus.user_sport_user = u.id
left join tb_sports ts ON ts.id_sport = tus.user_sport_sport
where u.id = 72
group by u.id
Source: https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html#function_group-concat
For alias: link1 and link2
I am wondering if I can have join between two tables but on different columns? Let me explain because it is different with most of the cases that I've seen...
I have a table for all the messages between users and each user has a unique user id. so In the first table I have:
Tx User Id .......... Rx. User Id .......... Date ............ Message
and in user tables I have
user Id .............. User name
Can I have a join query that gives me
Tx User "Name" ........... Rx. User "Name: ....... Date ....... Message
The problem is that in my join apparently I can only define
SELECT messages.* users.name
FROM messages JOIN
users
ON messages.RxId = users.id OR messages.TxId = users.id
which is only 1 field, but as I explained above I need 2 field as Rx user name and Tx. User Name based on which id in my messages table is matched.
Thanks a lot in advanced.
You want two joins. And for this you need to learn about table aliases (a good thing):
SELECT m.*, urx.name, utx.name
FROM messages m LEFT JOIN
users urx
ON m.RxId = urs.id LEFT JOIN
users utx
ON m.TxId = utx.id;
I am trying to pull both owner and editby. Both of those fields are INT. Inside a simple table, for example:
users:
user_id user_name
-----------------
2 johnny
3 mecca
doc:
owner content editby
----------------------
2 misc 3
SQL:
SELECT doc.owner, doc.content, doc.editby, users.user_name
FROM doc
LEFT JOIN
users
ON
users.user_id = doc.owner
WHERE
doc_id = $id
I can grab owner user_name, but I am not sure how to obtain editby on the same table. How do I go about pulling the different user names for different id fields multiple times?
Join the users table twice with different aliases
SELECT doc.owner, doc.content,
e.user_name as editor,
o.user_name as owner
FROM doc
LEFT JOIN users o ON o.user_id = doc.owner
LEFT JOIN users e ON e.user_id = doc.editby
WHERE doc_id = $id