How To Combine 2 MySQL Tables Like This? (Many-To-One Relationship) - mysql

I have UserComments table like this :
1 | Frank | hello world
2 | Jane | Hi there
3 | Frank | this is my comments
4 | Frank | I think I need some sleep
5 | Jason | I need to buy new MacBook
6 | Jane | Please invite my new Blackberry PIN
On the other hand, I have FriendList table contains :
1 | Jason
2 | Jane
Let's say my friends ID always BETWEEN 1 AND 5.
And since Frank is not my friend, I'm not able to see his comments. how to have combined tables like this (ORDER BY UserComments.ID DESC) :
1 | Jane | Please invite my new Blackberry PIN
2 | Jason | I need to buy new MacBook
3 | Jane | Hi there
thanks.

Try this:
SELECT A.ID, B.UserName, B.Comment
FROM FriendList A
INNER JOIN UserComments B ON A.ID = B.ID
ORDER BY A.ID DESC

Try this::
Select
*
from
user_comments inner join friend_List on (join criteria)
where user_Id = ? order by user_comments.id desc

Related

MySQL - Select from 2 tables even the other table is empty

I have 2 tables consisting of artists and tracks.
Artist
| id | name |
| -------- | -------------- |
| 1 | John Doe |
| 2 | Dave Wang |
Tracks
| id | artist_id | title |
| -------- | -------------- | -------------- |
| 1 | 1 | Song 1 |
| 2 | 1 | Song 2 |
I tried
SELECT a.name, b.title FROM Artist a, Tracks b WHERE a.id = b.artist_id
It returns all the songs of John Doe.
Is there a way to add Dave Wang on the result even it's just null on the title?
For example result
name
title
John Doe
Song 1
John Doe
Song 2
Dave Wang
null
Use an explicit left join:
SELECT a.name, b.title
FROM Artist a
LEFT JOIN Tracks b
ON a.id = b.artist_id;
As a side note, your current query is using the old school implicit join syntax. The version I gave above is the correct way of writing the join.
Please try this query
SELECT a.id, a.name,b.title FROM artist as a LEFT JOIN tracks as b on a.id = b.artist_id;

MySQL mentor student table

This is te problem,
I have a database with a table called Users with 3 columns UserID Mentor and Name, I want to show the users and their mentors. and if i do it like this:
SELECT UserID, mentorID, Name
FROM Users;
I will get this:
| UserID | Mentor | Name
| 1 | NULL | Walter
| 2 | 1 | Jesse
| 3 | 1 | Todd
But I want to get it like this:
| UserID | Mentor | Name
| 1 | NULL | Walter
| 2 | Walter | Jesse
| 3 | Walter | Todd
Thanks for helping,
have a nice day :)
P.S.
The real database a bit more complex but i simplify it here.
You could use a self join ( a join with the same table)
select a.userID, a.Name as user_name , a.mentorID, b.Name as mentor_name
from user a
left join user b on a.mentorID = b.userID
To get the data you need to use self join(A self join is a join in which a table is joined with itself) --
SELECT u.UserID, m.Name AS mentor, u.Name FROM user as u LEFT JOIN user AS m ON u.MentorID = m.UserID

SQL join with missing rows

I have the following two tables.
SurveyTable:
QID | Text
----------------------------------------
1 | Favorite movie
2 | Favorite book
3 | Favorite city
SurveyResponses:
UserID | QID | Answer
----------------------------------------
1001 | 1 | StarWars
1001 | 2 | Harry Potter
1001 | 3 | Los Angeles
1003 | 3 | New York
I would like to get a response which also has all the questions that the User did not answer in the survey.
Expected Output of SQL join:
UserID | QID | Answer
----------------------------------------
1001 | 1 | StarWars
1001 | 2 | Harry Potter
1001 | 3 | Los Angeles
1003 | 1 | -
1003 | 2 | -
1003 | 3 | New York
I tried various SQL query combinations but no luck. Please help.
Since you don't provide a Users table, you may end up with something like this:
SELECT
U.UserId, Q.QID, A.Answer
FROM SurveyTable Q
CROSS JOIN (SELECT DISTINCT UserId FROM SurveyResponses) U
LEFT JOIN SurveyResponses A ON A.QID = Q.QID AND U.UserId = A.UserId
You'll really want to avoid having to do this in real life though, the cross join is likely going to suck all performance out of the server if your tables get anywhere near large.
Better would be to have a Users table so you can use that and left join against it like this:
SELECT
U.UserId, Q.QID, A.Answer
FROM Users U
INNER JOIN SurveyTable Q
LEFT JOIN SurveyResponses A ON A.UserId = U.UserId AND A.QID = Q.QID

SQL Query Multiple Conditions

I have two tables like these.
They represent an interaction of following/followers between two users.
users
---------------------------------------
id | username | <br>
1 | bobby | <br>
2 | jessica | <br>
3 | jonny | <br>
4 | mike | <br>
follows
----------------------------------------
id | userid_1 | userid_2 | <br>
1 | 1 | 2 | <br>
2 | 3 | 4 | <br>
3 | 4 | 1 | <br>
Set my user id to 1 for example, I want a query that returns something like this
-----------------------------------------
username | userid_1 | userid_2 |<br>
jessica | 1 | 2 |<br>
mike | 4 | 1 |<br>
Any help to solve this problem? :)
This is a bit more complicated than it first seems, because you want the name of the other user:
select u.username, f.*
from follows f join
users u
on (u.id = f.userid_1 and f.userid_2 = 1) or
(u.id = f.userid_2 and f.userid_1 = 1)
where 1 in (f.userid_1, f.userid_2);
Actually, the where clause is not needed (it is covered by the on). I think it clarifies the filtering however.
You can JOIN tables in following:
SELECT u.username, f.userid_1, f.userid_2
FROM users u
JOIN follows f
ON u.Id = f.Id
Select username, userid_1, userid_2
from users, follows
where users.id = follows.id

MYSQL IN alternative

I am coding a social networking type website, users have friends and I am trying to create a news feed with actions that their friends have done
I have three tables
Users
id | username
---------------------
1 | john
2 | nicole
3 | bob
Friends
id | uid | who
----------------------------
1 | 1 | 2
2 | 2 | 1
3 | 2 | 3
4 | 3 | 2
5 | 3 | 1
6 | 1 | 3
Actions
id | owner | to_id | message
-------------------------------------------
1 | 3 | 2 | 'hello'
2 | 3 | 1 | 'yoooo'
Since 'john' is friends with 'bob, 'john' should be able to see 'bob' action to 'nicole'
EDIT:: I also want to get the actions from the other direction, if 'nicole' sends an action to 'bob'
My current solution is:
have a string that contains all the friends of a user: $friends = '1,2,3,4....etc'
query:
SELECT
`Actions`.*
FROM
`Actions`
WHERE
(`Actions`.`to_id` IN (${friends}) OR `Actions`.`from_id` IN (${friends}))
AND
( `Actions`.`to_id` != (${logged_id} AND `Actions`.`from_id` != (${logged_id})
ORDER BY
`Actions`.`time` ASC
LIMIT
15
The above query works, but my problem is when users start have hundreds of friends this query will be horrendously slow, what can I do as an alternative to prevent this?
If you want to try a different and better solution, please try using any popular graph database. It is well suited for your current requirement.
What about using a JOIN to achieve this?
SELECT actions.*
FROM actions a
JOIN friends f ON f.who = a.owner
WHERE f.uid = (current user's ID)
LIMIT 15
(Returns 15 actions owned by friends of the current user.)