two way joins mysql query - mysql

message table
id user_id| message
1 1 | this is my cruel message
2 1 | this is my happy message
3 2 | this is happy messgae
message_tags table
id message_id| tags
1 2 | happy
2 3 | happy
what i want to acess all the messages that have the the tag happy, how would construct the query in the best possible way :)) thanks
p.s. this is just an example database

select m.id, m.user_id, m.message,
u.Username
from message m
inner join user_table u on m.user_id = u.id
where m.id in (select message_id from message_tags where tags = 'happy')

Related

Join 3 tables only if they have information on 1 table

I have 3 table
| Likes | | Friends | | Item |
----------------------------- ------------------ -------------------
| UserID | LikeID | itemID | | user1 | user2| | itemID |
I would like to join the 3 tables based on if the user has liked an item in the Likes table and the user must also be friends with the user in the Friends table and both of them have liked the item.
The way the Friends table is set up is if a user adds a friend the userID will be placed in either user1 or user2 depending who requested the friend first, so the userID of say 1 can be in either user1 column or user2 column and users friend will be in the opposing column.
Then I would do an inner join on itemID table to get the item both of the users liked.
I am no SQL expert so I am finding this very difficult and any code examples I give you would be rubbish. I have tried all types of left joins inner joins right joins I've joined the crap out of these tables but I do not get the correct data.
Any steer in the right direction on how to accomplish this would be great I am out of ideas. Please let me know of any further information I can give you.
I believe this does what you are after:
select a.user1, b.likeid, a.user2, c.likeid, d.itemid
from friends a
inner join likes b on a.user1 = b.userid
inner join likes c on a.user2 = c.userid
inner join item d on (b.itemid = d.itemid and c.itemid = d.itemid);
Here's an sqlfiddle demonstrating it: http://sqlfiddle.com/#!4/ff9d59/11

Selecting 2 rows from same table for different criterias

I've tried many examples for inner joins, outer joins and even tried conjuring my own with guesses (which more than often works) but no luck here.
TABLE 1:
follower_user_id, followed_user_id
(note it's followeR_ and followeD_)
TABLE 2:
user_id, username
So I need two rows from TABLE 2 where one user_id matches follower_user_id and another row where user_id matches followed_user_id
Query within query works but I know this isn't the way to go...
SELECT f.*, u.*
FROM tbl_follows f, tbl_users u
WHERE follower_user_id = u.user_id
That's the basic query
while($row = $r->fetch_assoc()){
//here i make another query to get the username of the followed_user_id
}
Surely this can be done in a single query?
Thanks in advance
--- UPDATE 1: Sample Data ---
tbl_users
user_id | username
--------------------------------------
1 | abc123
2 | xyz789
3 | nosey123
tbl_follows
follower_user_id | followed_user_id
-------------------------------------------
3 | 2 //nosey123 is following xyz789
3 | 1 //nosey123 is following abc123
1 | 2 //abc123 is following xyz789
While Results :
echo "$row[username] is following $row['???????']<br />"
I'm looking for:
nosey123 is following xyz789
nosey123 is following abc123
abc123 is following xyz789
Have you tried something like this?
SELECT f.*, u.*
FROM tbl_follow f
JOIN tbl_users u ON u.user_id = f.follower_user_id OR u.user_id = f.followed_user_id;
This should return two rows for each userID, since there will be one match for follower user and one match for followed user.
SELECT DISTINCT(A.user_id), A.username, B.*
FROM tbl_follow A
JOIN tbl_users B
ON B.user_id = A.follower_user_id
OR B.user_id = A.followed_user_id;

get friends of friend using mysql

I am making a query in which i want to get friends of friends but the problem is that if my friends are also friends it suggesting their ids too . Suppose user id is 1 and he has teo friends 2 and 3 and 2 has is friends with 4 and 3 and 3 is friends with 5 and 2 then its suggesting me 4,5 which it should but also suggesting 2,3 because they are friends and they both are also my friends which is wrong it should only suggest 4,5
My query is
SELECT fr.friend_id
FROM friend_list fl ,friend_list fr
WHERE fl.login_id='27'
AND fl.status='3'
AND fl.friend_id=fr.login_id
AND fr.status='3'
AND fl.login_id!=fr.friend_id
One way to do this is to add WHERE NOT IN predicate :
SELECT fr.friend_id
FROM friend_list fl
INNER JOIN friend_list fr ON fl.friend_id = fr.login_id
AND fl.status = fr.status
WHERE fl.login_id = 27 AND fl.status = '3'
AND fr.friend_id NOT IN(SELECT friend_id
FROM friend_list
WHERE login_id = 27 AND friend_id IS NOT NULL)
The subquery after the NOT IN will select the friends of the friend id you are passing. to exclude them.
SQL Fiddle Demo
This will give you only 4, 5 for the example you gave in your question:
| FRIEND_ID |
|-----------|
| 4 |
| 5 |
Note that: I used the explicit JOIN syntax instead of the old JOIN syntax you are using, it should be the same but it is recommended to use the new one.

How to put logic into SQL?

First at all i am sorry for asking silly question like this.
I am new into sql. Leaned basic things from internet but cant figure this out.
Lets think i have a table named post where i store peoples post. Table structure is like this..
Post_id | poster_id | text
1 | 12 | "hello this is a post"
2 | 15 | "Another post"
3 | 77 | "More counting"
There is a table named "Friends" Where i store whos friend is who. Table structure is like this..
user_one | user_two
1 | 88
84 | 33
1 | 66
Now i can use SQL like this to get post from post
SELECT * FROM POST INNER JOIN frnends ON user_one = poster_id WHERE 1
But this will get a table by joining two table together, Cant i do some logic like this?
SELECT post FROM post WHERE poster_id = (SELECT user_two,user_one FROM friends WHERE user_one = MY_id OR user_two = MY_id
perhaps MY_id is a variable WHERE MY_id = 1
Now to do this kind of logic?
Any help will be so great.
Sorry, I lack the reputation to comment. Gamal's solution should be refined to prevent the return of My_id's posts;
SELECT *
FROM post p
INNER JOIN friends AS f on p.poster_id IN (f.user_one, f.user_two)
WHERE My_id IN (f.user_one, f.user_two)
AND p.poster<>My_id;
Or if you do want those posts as well;
SELECT *
FROM post p
INNER JOIN friends AS f on p.poster_id IN (f.user_one, f.user_two)
WHERE My_id IN (f.user_one, f.user_two)
AND p.poster<>My_id
UNION
SELECT *, My_id AS user_one, NULL AS user_two
FROM post p
WHERE p.poster=My_id;
The problem is that Gamals suggestion will return each post by My_id once for every relation that My_id is part of.
try this:
SELECT *
FROM [sample].[dbo].[posts] a inner join [sample].[dbo].[friends1] b on a.posterId=b.user_one or a.posterId=b.user_two where a.PosterId=12

"Friend" relationships across two mysql tables

I'm not quite sure how to construct an sql join inorder to find who a specific users "friends" are.
For example I have two table
User Table
u_ID | u_Name
-------------
1 | bob
2 | jill
3 | jack
4 | susan
Friends Table
f_ID | u_ID1 | u_ID2
--------------------
1 | 1 | 2
2 | 3 | 1
3 | 4 | 2
I need to find a way of getting all of bobs friends, or all of jills friends, for example.
Friends cannot have duplicate results
I.e. can have a row with either (u_ID1 = 1, u_ID2 = 2) or (u_ID1 = 2, u_ID = 1) but not both, as they are technically the same.
Here is my incorrect query
SELECT u.u_Name
FROM user u
INNER JOIN friends f ON (f.u_ID1 = '1' OR f.u_ID2 = '1')
Thanks in advance
Solution
Check Kris Babic reply for solution,
also thank you for everyone elses help
This uses a STRAIGHT join, but should work:
select u.u_Name
from friends f, user u
where (f.u_ID1 = '1' and u.u_ID = f.u_ID2)
or (f.u_ID2 = '1' and u.u_ID = f.u_ID1)
For all of bob's friends this should work (untested)
select u.u_Name
FROM user u
inner join friends f1 on f1.u_uID1 = u.u_ID
inner join friends f2 on f2.u_uID2 = u.u_ID
where u.u_ID = 1
Try this:
SELECT u1.u_Name as user1 , u2.u_Name as user2
FROM user as u1 INNER JOIN friends ON u1.u_ID=friends.u_ID1
INNER JOIN user as u2 ON u2.u_ID=friends.u_ID2