I'm struggling with this for hours so please help me.
This is my users table
id | username | last_activity(timestamp for online system)
And this is my friends table
id | uid | fid
What I want is to order the output by last_activity
My current query looks like this:
SELECT fid FROM friends WHERE uid='$user_id'
SELECT f.fid FROM f.friends
LEFT JOIN users u ON f.uid = u.id
WHERE uid=$user_id
ORDER BY u.last_activity DESC
You want to use INNER JOIN to join the two tables:
SELECT f.fid
FROM friends f
INNER JOIN users u
ON u.id = f.id
ORDER BY u.last_activity DESC
Always make sure you type out a real JOIN clause as some ways are old and getting more and more unsupported.
Read more here:
INNER JOIN on w3schools
I think you want
SELECT f.fid AS fid
FROM friends f, users u
WHERE f.uid = u.id AND f.uid = $user_id
ORDER BY u.last_activity DESC
I assume your problem is that fid is on one table, but the ordering criterion last_activity is on another table. The goal of this query is to JOIN each row in the friends table with the corresponding row in the users table (via the WHERE clause).
Assuming uid in friends table is foreign key references to id in users
table. so using INNER JOIN you can retrieve your desired results.
SELECT f.fid
FROM friends f INNER JOIN user_tb u
ON u.id = f.uid
WHERE f.uid = '$user_id'
ORDER BY u.last_activity DESC;
Related
My tables
$sql="SELECT *
FROM addresses
LEFT JOIN users ON address_id = user_id
LEFT JOIN notes ON note_id = user_id
ORDER BY id DESC
LIMIT 1";
This is my SQL query, my task is to show the last records from 3 tables, but the table is blank, I don't know why,thanks in advance people :)
I guess the problem is coming from the ORDER BY id DESC .
Indeed, you have no column so called id.
You should probably remove this clause, in order to make your code work.
If you want to take the last records anyway, you can put an ORDER BY address_id DESC which will do the job !
The code directly edited :
$sql="SELECT *
FROM addresses
LEFT JOIN users ON address_id = user_id
LEFT JOIN notes ON note_id = user_id
ORDER BY adress_id DESC
LIMIT 1";
This may work:
SELECT a.address_id, u.user_id, n.note_id
FROM addresses a
LEFT JOIN users_addresses ua ON ua.ua_address_id = a.address_id
LEFT JOIN users u ON u.user_id = ua.ua_user_id
LEFT JOIN notes n ON n.note_user_id = u.user_id
ORDER BY a.address_id DESC
LIMIT 1
Here is the query to get all data from all the tables, not sure what do you mean last records from 3 tables, I can see four tables there:
SELECT *
FROM `addresses`
LEFT JOIN `users_addresses` ON `users_addresses`.`ua_address_id` = `addresses`.`address_id`
LEFT JOIN `users` ON `users`.`user_id` = `users_addresses`.`ua_user_id`
LEFT JOIN `notes` ON `notes`.`note_user_id` = `users`.`user_id`;
I am working on social networking site.
I've three tables one is user table which is used to store user details, another table is follow table which is used for followers following list.
In this table I am storing user_id and follower_id.
Third table is user_friends in this I'm storing user_id and friend_userid.
I want to search the user from my friends list and follow list. For this i've written query like this:-
select f.follower_id,uf.friend_userid,u.user_id,u.first_name,u.last_name from tbl_user u
LEFT JOIN tbl_userfriends uf ON uf.friend_userid = u.user_id
LEFT JOIN tbl_follow f ON f.follower_id = u.user_id
where uf.friend_userid != '11'
AND u.first_name LIKE '%a%'
This query returning users only who are friends it is not returning the follow users.
Any help will be appreciated. Thanks in Advance.
You're joining tbl_follow on the Follower ID being equal to the User ID. I suspect that's probably not right.
If you don't already have one you'll need a user id key in the follower table to join on, then you can change your join to;
LEFT JOIN tbl_follow f ON f.userid = u.user_id
i've done this by using following query:-
select u.user_id,u.first_name,u.last_name from tbl_user u LEFT JOIN tbl_userfriends uf ON uf.friend_userid = u.user_id LEFT JOIN tbl_follow f1 ON f1.follower_id = u.user_id LEFT JOIN tbl_follow f2 ON f2.user_id = u.user_id where (uf.user_id = '11' OR f1.user_id = '11' OR f2.follower_id = '11') AND (u.first_name LIKE '%s%' OR u.last_name LIKE '%s%') AND u.status = '0' group by u.first_name
This query returning me all the users who are my followers, friends and to whom i am following.
I have three table group, user and belongs where belongs is relation between group and user.
Table group
group_id (pk)
group_name
Table user
user_id (pk)
user_name
Table belongs
user_id
group_id
I want to show a random groups that people in specified group are also in.
For example specified group is where group_id = 1, Of course, i can get people in this group by use
"SELECT user_id FROM user u JOIN belongs b ON u.user_id = b.user_id and b.group_id = '1'"
I have been trying, and now, i have no idea how to select group that these people are in too.
"SELECT g.* FROM group g WHERE ..."
Thanks in advance.
You can get all the groups by using an extra join:
SELECT DISTINCT b2.group_id
FROM user u JOIN
belongs b
ON u.user_id = b.user_id and b.group_id = '1' JOIN
belongs b2
ON b2.user_id = b.user_id;
One way to choose a random such group is to use order by rand() limit 1. You might want to add where b2.group_id <> '1'.
Assuming that you want to count the members of group_id = 1 in these groups, you would just use group by:
SELECT b2.group_id, count(distinct b2.user_id) as numusers
FROM user u JOIN
belongs b
ON u.user_id = b.user_id and b.group_id = '1' JOIN
belongs b2
ON b2.user_id = b.user_id
GROUP BY b2.group_id;
The join to user is superfluous, but I have left it in assuming that it might be used for some other conditions not in the original question.
You can get a list of all the other groups associated with people in group 1 by using a sub-query:
SELECT g.group_name, COUNT(g.group_name)
FROM group g
INNER JOIN belongs b ON b.group_id = g.group_id
WHERE b.user_id IN ( SELECT u.user_id FROM user u JOIN belongs b ON u.user_id = b.user_id and b.group_id = '1' )
GROUP BY g.group_name
ORDER BY COUNT(g.group_name) DESC
I'm trying to select all users that their user id is not in another table.
Users table (user id is user_id)
Ghosts table (user id is id)
this is what I got so far:
$sql = "select * from users where users.user_id <> ghosts.id;";
Please advise...
Cheers!
You can use NOT IN:
select * from users u where u.user_id not in (select id from ghosts);
or NOT EXISTS:
select *
from users u
where not exists (select 1 from ghosts g where g.id = u.user_id)
Something like this may also work, although I like Brian's answer.
SELECT *
FROM users u
LEFT JOIN ghosts g
ON u.user_id = g.id
WHERE g.id IS NULL
I have two table users and album. In users table there is user_id primary key .In other table albums there are multiple rows with that user_id because every time when a user upload a new album it uploads with user_id as foreign key. I want to select only once the user_id with other table(album) ignore other result set.
How can I achieve this?
SELECT a.*, b.*
FROM users a
INNER JOIN album b
ON a.user_ID = b.user_ID
INNER JOIN
(
SELECT user_ID, MAX(photo_id) max_rec
FROM album
GROUP BY user_ID
) c ON b.user_ID = c.user_ID AND
b.photo_id = c.max_rec
SELECT album.* FROM album LEFT JOIN users ON user.id = album.id WHERE user.id = SOMEIDHERE
I believe this will work, your not giving me a whole lot of info to work with.
SELECT *
FROM ( SELECT u.*, a.*
FROM users AS u
INNER JOIN album AS a
ON u.user_ID = a.user_ID
ORDER BY a.created DESC) AS h
GROUP BY user_ID
ORDER BY b.created DESC -> ORDER BY whatever row you wish for. In this case the newest one is chosen.