ORDER BY column FROM another table - mysql

I want to order user's friends by last_act with them. But information about users is in table users and last_act in table friends.
I tried this but it didn't work:
SELECT m.*
FROM users AS m
WHERE m.username = '$username'
JOIN friends AS p
ORDER BY p.last_act DESC

You should have a foreign key on friends table for user.
For eg say it userid.
Then above query will be:
SELECT m.*
FROM users AS m
JOIN friends AS p ON p.userid = m.id
WHERE m.username = '$username'
ORDER BY p.last_act DESC

Your question is too vague, we don't know the structure of tables and we don't see error message neither...
but is see there an order issue, it must go as SELECT, FROM, JOIN, WHERE. And you need use ON to map relation in JOIN.
example
SELECT * FROM table1 JOIN table2 ON table1.id = table2.foreign_id

SELECT m.*, f.*
FROM users AS m
JOIN friends AS p
ON m.id = p.user_id
LEFT JOIN users AS f
ON p.friend_id = f.id
WHERE m.username = '$username'
ORDER BY p.last_act DESC

Related

How to run nested left join in mysql

I have three tables, likes, users and statuses. So I am returning from likes tables and joining likes table with user table to see who liked it and joining likes table with statuses table to see who posted it.
Now want to get the user information from status table to see who wrote the status. Here comes the problem.
SQL Fiddle http://sqlfiddle.com/#!9/d0707b/2
My current query
select l.*, s.* , a.id as aid, a.userName from likes l
left join
statuses s on l.source_id = s.id
left join
users a on l.user_id = a.id
where
l.user_id in (5,7)
or
(s.privacy='Public' and s.interest in ('mobile', 'andriod') )
order by l.id desc
Here s.user_id=a.id I want to join the statuses table with user table.
[If question is not clear please comment, will try to edit]
Thank you.
You have to make a join to the user table again. Take a look here:
SELECT
l.*, s.*,
a.id AS aid,
a.userName,
b.userName
FROM
likes l
LEFT JOIN statuses s ON l.source_id = s.id
LEFT JOIN users a ON l.user_id = a.id
LEFT JOIN users b ON s.user_id = b.id
WHERE
l.user_id IN (5, 7)
OR (
s.privacy = 'Public'
AND s.interest IN ('mobile', 'andriod')
)
ORDER BY
l.id DESC

Sql query correction one to many tables

I have two tables one is users and second is user_education.One users can have more than one education listing so i want to get the latest user education listing
users
===============
1-id
2-email
member_experience
==============
1-id
2-user_id
3-designation
user id 1 has 4 enteries in user_education so i want to get the last record enter designation of the user
original full query is like this
SELECT u.id,u.name,u.gender,u.email,file_managed.file_name,file_managed.file_path
from users as u
INNER JOIN member_experience on (SELECT uid FROM member_experience where member_experience.uid=u.id ORDER BY id DESC LIMIT 1)=u.id
LEFT JOIN file_managed on file_managed.id= u.fid
where u.user_type ='individual' AND u.gender='male'
"INNER JOIN member_experience on (SELECT uid FROM member_experience where member_experience.uid=u.id ORDER BY id DESC LIMIT 1)=u.id "
this portion has problem as users has many record in member_experience table but i want to get only one which is latest.
thanks
Devolve the acquisition of the last record to the where statement.
drop table if exists member_experience;
create table member_experience(id int auto_increment primary key, userid int);
insert into member_experience (userid) values
(1),(2),(1);
select * from member_experience
SELECT u.id,m.id
from users as u
join member_experience m on m.userid = u.id
where m.id = (SELECT max(m.id) FROM member_experience m where m.userid = u.id)
order by u.id
Or if you want to include those with no experience
SELECT u.id,m.id
from users as u
left join member_experience m on m.userid = u.id
where (m.id = (SELECT max(m.id) FROM member_experience m where m.userid = u.id)
or m.id is null)
and u.id < 4
order by u.id

Join two different tables ordered by common value (hotness)

I'm trying to select results from two different unrelated tables, showcase and questions to appear in a feed. They should be ordered by the common column hotness which is a float value.
SELECT s.id,s.date,s.title,s.views,s.image,s.hidpi,s.width,s.description,u.display_name,u.avatar
FROM showcase AS s
INNER JOIN users AS u ON s.user_id = u.id
UNION
SELECT q.id,q.date,q.title,q.views,q.text,u.display_name,u.avatar,0,0,0
FROM questions AS q
INNER JOIN users AS u ON q.user_id = u.id
ORDER BY hotness DESC
LIMIT 10
I've tried UNION, but I have no idea how I should be using it here and get this error unknown column hotness
You need to select the value in order for the ORDER BY to recognize it:
SELECT s.id,s.date,s.title,s.views,s.image,s.hidpi,s.width,s.description,u.display_name,u.avatar, s.hotness
FROM showcase AS s
INNER JOIN users AS u ON s.user_id = u.id
UNION ALL
SELECT q.id,q.date,q.title,q.views,q.text,u.display_name,u.avatar,0,0,0, q.hotness
FROM questions AS q
INNER JOIN users AS u ON q.user_id = u.id
ORDER BY hotness DESC;
Note that I also changed the UNION to UNION ALL. Unless you intend to remove duplicates, there is no reason to incur the extra processing for doing that.
You can try this query:
SELECT r.* FROM (
SELECT s.id,s.date,s.title,s.views,s.image,s.hidpi,s.width,s.description,u.display_name,u.avatar, s.hotness
FROM showcase AS s
INNER JOIN users AS u ON s.user_id = u.id
UNION
SELECT q.id,q.date,q.title,q.views,q.text,u.display_name,u.avatar,0,0,0, q.hotness
FROM questions AS q
INNER JOIN users AS u ON q.user_id = u.id
) as r
ORDER BY r.hotness DESC
LIMIT 10
You need to merge Union result in subquery to apply Order by on the result. I also added hotness in select clause, please check I take field from good table.

SQL join count(*) of one table to another table as aliases

I have two table, users and comments.
In the users table, there're columns id and username.
In the comments table I have user_id and his message.
And I wanted to create a table that select the username and his comment count when I search a particular username.
How do I write this?
my testing attempt:
SELECT COUNT(*) AS comment_count
FROM song_comments
RIGHT JOIN users
WHERE user_id = 7 AND comments.user_id = users.id
Try this:
SELECT U.Username, COUNT(SC.message) AS comment_count
FROM song_comments SC JOIN
users U ON U.id=SC.user_id
WHERE U.user_id = 7
GROUP BY U.Username
This gives you users and count
select u.username, count(c.user_id) as comment_count
from users u
join comments c on u.id = c.user_id
group by u.username
You can add a where to get one user's count
select u.username, count(c.user_id) as comment_count
from users u
join comments c on u.id = c.user_id
where u.username = 'Hogan'
group by u.username

SQL Statement that orders selected IDs first with condition

I have the following tables:
team_members
userid
teamid
users
uid
name_f
name_l
friends
friend_id
friend_one
friend_two
I use the following statement to select uid and profile_pic of the users that belong to a certain team.
SELECT DISTINCT u.uid, u.profile_pic
FROM friends f, users u, team_members m
WHERE m.teamid =$team_var
AND u.uid = m.userid
I also have to run the following to select the uid and profile_pic of the users who are friends with the logged-in user and belong to a certain team.
SELECT DISTINCT u.uid, u.profile_pic
FROM friends f, users u, team_members m
WHERE m.teamid =$team_var
AND u.uid = m.userid
AND m.userid = f.friend_two
AND f.friend_one =$session_id
I'm looking for a way to join these two and instead of running 2 queries, run one single query that can order and list the users who are friends with the logged-in user at the top. So let's say that a certain team has 30 users and 5 of those users are friends with the logged-in user, I would like to have the first 5 listed in the while loop following the statement to be those of the friends with the rest of the 25 randomly shown.
Thanks for your time.
This can easily be solved with an outer join. You will probably not be able to use an outer join without the explicit join syntax. Here:
SELECT
u.uid,
u.profile_pic,
(friend_id IS NOT NULL) AS is_friend
FROM team_members m
INNER JOIN users u ON m.userid = u.uid
LEFT JOIN friends f ON m.userid = f.friend_two AND f.friend_one = $session_id
WHERE m.teamid = $team_var
ORDER BY
is_friend DESC,
m.userid
The first two tables are joined using an inner join, so only the members of a specific team are returned (because we are filtering on teamid).
The friends table is outer-joined to the result of the previous join. More specifically, we a joining the subset of friends where friend_one is the current user. All rows from the previous result set are returned, but also rows from the friends's subset are returned where matched. Where not matched, the friends columns are filled with NULLs.
Using the NULL (or rather NOT NULL) test, we can see which team member is a friend and which isn't. The result of the test is returned as a column and also used as a sorting criterion for the output rows.
You would need to create another column which has a value of "friend" or not "friend". You would then use that column as an ORDER BY. The column you create could be a subquery to determine if the other user is a friend or not. The following code is wrong because you need to hook the data from the outer query into the subquery, but it should look something like:
SELECT DISTINCT u.uid, u.profile_pic,
EXISTS (SELECT DISTINCT u.uid, u.profile_pic
FROM friends f, users u, team_members m
WHERE m.teamid =$team_var
AND u.uid = m.userid
AND m.userid = f.friend_two
AND f.friend_one =$session_id
) AS myColumn
FROM friends f, users u, team_members m
WHERE m.teamid =$team_var
AND u.uid = m.userid
ORDER BY myColumn
I might suggest this:
select u.uid, u.profile_pic
from team_members t
join team_members m on t.teamid = m.teamid
join users u on m.userid = u.uid
left join friends f on t.userid = f.friend_one and m.userid = f.friend_two
where m.userid != t.userid
and t.userid = $session_id -- This line can be removed to view all (test)
order by
--t.teamid, t.userid, -- This line can be added to order all (test)
(case when f.friend_id is null then 1 else 0 end case),
f.friend_id, m.userid
I'm using explicit join syntax (which is normally recommended these days) and using that case statement in the order by to get friends to the top. I don't have MySQL running here to test it, but the syntax should be pretty standard (I'm running something very similar on SQL Server).