MySQL query with 2 tables join - php

I have a users like this:
id username
1 user1
2 user2
3 user3
and a msgs like this:
t_id sent_by id msg
1 2 1 whatever
2 3 1 is
3 2 1 here
Where users.id is a primary key and msgs.id is a foreign key. In the msgs table, id is the destination of the message sent by sent_by.
I want to select and display the username of sent_by as long as the logged in user (via sessions) is the msgs.id.
To clarify things, here is the pseudocode of what I wanted to do:
Users has logged in. Store its userid to session.
Display the distinct usernames of who sent me (the logged in user) the messages. In the example above, the display will be: user2, user3 if I have a user id of 1.
I was thinking of using join but ended up doing 2 queries for the sent_by and ids. It seems not an efficient query.
What should I do?

This is a straightforward JOIN since you only wish to return usernames of the sent_by ids.
$sql = "SELECT DISTINCT
users.username
msgs.sent_by
FROM users JOIN msgs ON users.id = msgs.sent_by
WHERE id = {$_SESSION['my_userid']}";

SELECT DISTINCT u.username, m.sent_by
FROM msgs m
INNER JOIN users u ON u.id = m.sent_by
WHERE m.id = {$_SESSION['userid']}
ORDER BY m.t_id DESC

Related

top first friends then the other users from like list

I have tables:
likes - id, user_id, like_user_id<br>
users - id, name, email ...,<br>
friends - id, user_id, friend_id, status<br>
Is it possible to sort it with one SQL query first to show the friends then the other users.
Any help would be appreciated.
Thank you.
I tried this and it works, but the problem is it give me double results of users:
select *
from `likes`
left join `users` on `users.id` = `likes.user_id`
left join `friends` on `friends.user_id` = `likes.user_id`
or `friends.friend_id` = `likes.user_id`
where `likes.id` = 1
order by `friends.user_id` = 5
or `friends.friend_id` = 5
You need to work with a UNION here to merge the liked users with the befriended users. Upon doing this, you can create an artificial column friend, that you fill with 1 in the friend query and 0 in the like query. Later on you can order by that column.
SELECT
friends.user_id,
1 as friend,
users.*
FROM
friends
JOIN users ON users.id = friends.friend_user_id
UNION SELECT
likes.user_id,
0 as friend,
users_liked.*
FROM
likes
JOIN users as users_liked ON likes.like_user_id = users_liked.id
WHERE
user_id = '$userId'
ORDER BY friend DESC, id ASC
This will return a list of all friends, followed by a list of all liked users.

Getting Friend list having information in same table

I have scenario in which I am storing user email in a table user_info. Its structure is given as below.
user_info(_id,email)
However one user can be friend of other users.So for that purpose i have another table user_friend. Its structure is given below
user_friend(_id,userA , userB)
Now suppose there are 5 user A,B,C,D and E. We got table values something like this.
user_info
_id email
1 A#...
2 B#...
3 C#...
4 D#...
5 E#...
user_friends
_id userA userB
1 1 2
2 1 3
3 1 4
4 5 1
it's mean A is friend of B,C,D and E. i want to get email of these but i am unable to think about any proper query.
i am using left join but it does not seems to work properly
Getting friends of 1(A#...). But it's not working properly
SELECT DISTINCT email FROM user_info AS UI
LEFT JOIN user_friends AS UF
ON UI._id = UF.userA
LEFT JOIN user_friends AS UF2
ON UI._id = UF2.userB
WHERE UF2.userA = '1' || UF2.userB ='1'
Any Suggestions?
Use UNION to get all friends email
(SELECT
u2.email
FROM user_info u
LEFT JOIN user_friends f ON f.userA = u._id
LEFT JOIN user_info u2 on u2._id = f.userB
WHERE u._id = 1)
UNION
(SELECT
u2.email
FROM user_info u
LEFT JOIN user_friends f ON f.userB = u._id
LEFT JOIN user_info u2 ON u2._id = f.userA
WHERE u._id = 1)
It will be Something like this.
SELECT email from user_info WHERE _id IN (select userA from user_friends where userB = '1' Union All select userB from user_friends where userA = '1')

Select each first record with id in another table's result

I have two tables, Users(id, username) and Posts(id, user_id, content). I want to list a summary of them which, I want to list all users and the first post of each user. How can I realize this in one query?
I tried something like.
QUERY
SELECT Users.*, Posts.content
FROM Users, Posts
WHERE Posts.user_id=T_Users.id
But it will return all posts for each user (I cannot add LIMIT 1 at the tail of course which only returns one user).
Some sample records:
Users table:
id username
1 test1
2 test2
Posts table:
id user_id content
1 1 This is a test1's content.
2 1 This is another test1's content.
3 2 This is a test2's content.
And I want the result:
Users.id Users.username Posts.content
1 test1 This is a test1's content.
2 test2 This is a test2's content.
Here is one approach to get the latest record per user i assume the latest record will be considered as the minimum post id
SELECT u.*, p.content
FROM Users u
join Posts p on p.user_id=u.id
join (select user_id ,min(id) id from Posts group by user_id ) p1
on (p.id = p1.id and p.user_id = p1.user_id )
Demo
Try this
select min(x.id) as Id,x.user_id,x.content from(
select p.id,p.user_id,p.content from Users u inner join Posts p
on u.id=p.user_id and u.gender=1
)x group by x.user_id
Have you tried group by clause.
SELECT Users.*, Posts.content
FROM Users, Posts
WHERE Posts.user_id=T_Users.id GROUP BY Posts.user_id

MySql combining results and applying to a second table

I am having difficulty getting the results from 1 query and making them display in alpha order when the results are numerical.
TABLE1: "users" ROWS: user_id, username
TABLE2: "friends" ROWS: user_id, friend_id, confirmed
When YOU friend someone or they friend you, and you confirm, you create a "confirmed state" of 1.
With that in mind:
If I make the following query:
SELECT user_id
FROM friends
WHERE friend_id = 4
AND confirmed = 1
UNION
SELECT friend_id
FROM friends
WHERE user_id = 4
AND confirmed=1
I get a list of all my friends id's. I am user '4'.
What I would like to do now is take this result and look up their usernames from "users" and put them into Alphabetical order to compile a "Friends List"
I've tried all combinations of JOIN etc, but just cant get my head round it.
Please help, Any ideas?
SELECT u.*
FROM user u
JOIN
( SELECT user_id
FROM friends
WHERE friend_id = 4
AND confirmed = 1
UNION
SELECT friend_id
FROM friends
WHERE user_id = 4
AND confirmed=1
) x
ON x.user_id = u.user_id
ORDER
BY something

WHERE query inside an AND statement? PHP/PDO/MySQL

I have 2 tables on my website, a users table and a user_friendships table each structured as so...
Users
id | user_id | credits_bank | credits_offered
User Friendships
id | user_id | user_followed_id
When my user logs in he is presented with a list of other users on the website - the list of other users are those who are stored in the users table and have a greater value in the credits_bank table than that of the credits_offered table.
When a friendship is created, the session users id is stored in the user_friendships table, and the id of the other member he followed is also stored in the user_friendships table under the column user_followed_id.
The problem is I now need a query to return all users who have move credits_bank than credits_offered and users that aren't already in the user_frienships table in the same record as the session user.
I'm currently using...
SELECT DISTINCT u.*
FROM users u
LEFT JOIN user_friendships uf ON u.user_id = uf.user_followed_id
WHERE u.user_id <> ?
AND u.credits_offered <= credits_bank
AND uf.user_followed_id IS NULL
Update
I want to see a list of users whose credits_bank is a greater value than credits_offered and I only want to show them if they dont already exist in a record in my user_friendships table in the same row as my session user.
Users
id | user_id | credits_bank | credits_offered
___________________________________________________
1 123 10 2
2 231 6 3
3 312 6 5
4 213 2 1
User Friendships
id | user_id | user_followed_id
___________________________________________________
1 123 231
2 123 312
Result
If session user_id = 123 then...
user_id 231 and 312 WOULDN'T show as they are in the user friendships table alongside session user id
user_id 213 WOULD show as they have more credits_bank than credits_offered and arent in friendships table
IF the session user_id was 312 then he would see all results as he isnt friends with anybody in the user_friendships table...
As far as I can tell, you're close. If the user id of the current user is called SESS_USER_ID, something like this should work for you;
SELECT DISTINCT u.*
FROM users u
LEFT JOIN user_friendships uf
ON uf.user_followed_id = u.user_id
AND uf.user_id = SESS_USER_ID
WHERE u.credits_offered <= credits_bank
AND uf.user_followed_id IS NULL
AND u.user_id <> SESS_USER_ID
(note that SESS_USER_ID is used twice in the query to make it simple)
An SQLfiddle to test with.
Try this:
SELECT u.id, u.user_id, u.credits_bank, u.credits_offered
FROM users u
WHERE u.credits_bank>u.credits_offered
AND u.user_id = [ENTER LOGGED IN USERS ID HERE]
AND u.user_id NOT IN (
SELECT f.user_ol
FROM user_friendships f
)
Let me know if you have any issues
EDIT
Latest SQL:
SELECT u.id, u.user_id, u.credits_bank, u.credits_offered
FROM users u
INNER JOIN user_friendships f
ON f.user_followed_id = u.user_id
AND u.credits_bank > u.credits_offered
AND f.user_id != [CURRENT_USER_ID]
AND u.user_id != [CURRENT_USER_ID]

Categories