I have one chat table there is reference id of user table use in user_from and user_to.
Now I have to a query for get a chat member list from chat table like facebook caht list.
with whom I have chat and I have to find name of user from user table and unread counter
Please give me solution for this
Thanks
I think your subquery needs to be correlated, which will be slow.
Use JOIN instead.
SELECT
users.id AS userid,
chat_box.id AS msgid,
first_name,
last_name,
profile_pic,
LEFT(message, 50) AS message,
u.unreadcounter
FROM
chat_box
LEFT JOIN
users ON (chat_box.user_from = users.id)
LEFT JOIN
(SELECT
u.id, COUNT(c.id) unreadcounter
FROM
chat_box c
JOIN users u ON (c.user_from = u.id)
WHERE
c.read = 0 && c.user_to = 45
GROUP BY u.id) u ON users.id = u.id
WHERE
(user_from = 45 OR user_to = 45)
&& users.id != 45
GROUP BY users.id
UNION SELECT
users.id AS userid,
chat.id AS msgid,
first_name,
last_name,
profile_pic,
LEFT(message, 50) AS message,
0 AS unreadcounter
FROM
chat_box AS chat
LEFT JOIN
users ON (chat.user_to = users.id)
WHERE
(user_from = 45 OR user_to = 45)
&& users.id != 45
GROUP BY users.id
ORDER BY msgid DESC
Related
A student can register for more than one class. I want to list the students who are not registered in the class. But I couldn't find where I went wrong.
Here is my code listing what's in the class:
SELECT users.id as id, users.rank, users.name_surname, users.status
FROM userClasses,users
WHERE userClasses.user = users.id
AND userClasses.classes = 2
Here is my code that lists those not registered with the class:
SELECT users.id as id, users.rank, users.name_surname, users.status
FROM userClasses,users
WHERE userClasses.user = users.id
AND userClasses.classes <> 2
Can you help with this?
I also tried this code, it does not give correct results.
SELECT users.* FROM
( SELECT users.*, userClasses.classes
FROM users
LEFT JOIN userClasses
ON users.id = userClasses.user
WHERE ((userClasses.classes <> 2)
OR (userClasses.classes IS NULL)) AND users.`rank`=1 )
users
LEFT JOIN classes
ON users.classes = classes.id;
SELECT users.id as id, users.rank, users.name_surname, users.status
FROM userClasses
INNER JOIN users ON userClasses.user = id
WHERE userClasses.classes <> '2'
I want to show all those users from Users table, whose id is present in table favorite_group_users as user_id for a user whose favorite_groups.id = favorite_group_users.id.
I used below query but it is returning null.
select users.id as user_id, users.first_name as name,
favorite_groups.id as group_id,
favorite_groups_users.user_id as carrier_id
from users
inner join favorite_groups
on users.id = favorite_groups.user_id
inner join favorite_groups_users
on favorite_groups.id = favorite_groups_users.favorite_group_id
where users.id = 38;
You may try nested select
select
favorite_groups.group_id, users_group.user_id
from
favorite_groups_users ,
(select
favorite_groups_users.favorite_group_id,
users.user_id
from
users, favorite_groups_users
where
users.id = 38 and
users.id = favorite_groups.user_id
) users_group
where
users_group.favorite_group_id=favorite_groups.group_id
Table -1 : Comment id, comment,user_id,comment Date
Table -2: Users id, user_name, full_name, password
now i want to get user detaiils records who is last comment
like
query is :
select c.*,
(select user_name
from users
where id = c.user_id
) as user_name,
(select full_name
from users
where id = c.user_id
) as full_name
from comment as c, users as u
group by c.user_id
order by comment_date DESC
SELECT Users.*,
Comment.*
FROM Users
INNER JOIN Comment ON (Comment.user_id = Users.id)
GROUP BY Users.id
ORDER BY Comment.id DESC
that should work
Here is your query
select users.* from users inner join comments on
users.user_id = comments.user_id
order by comments.comment_date desc limit 1
Another way to do this
select * from users where user_id =
(select user_id from comments order by comment_date desc limit 1)
For my mobile App i am using a web service to get the images and all records from the MySQL database, its like Instagram.
But SELECT DISTINCT taking more than 7 - 8 seconds.is there any thing that i can do for reduce the time?
SELECT distinct count(*) as totalrecord ,p.description,p.photo_id ,p.user_id,p.imagepath1 ,p.friends, p.is_report_photo,
p.imagepath2,p.imagepath3,p.imagepath4,p.imagepath5,p.count_comment,p.count_like,p.created as photo_created ,
(select username from users where id = p.user_id) as username ,
(select country from users where id = p.user_id) as country,
(select email from users where id = p.user_id) as email ,
(select image_path from users where id = p.user_id) as image_path ,
(select gender from users where id = p.user_id) as gender,
(select privacy from users where id = p.user_id) as privacy,
(select audio_privacy from users where id = p.user_id) as audio_privacy,
(select video_privacy from users where id = p.user_id) as video_privacy,
(select photo_privacy from users where id = p.user_id) as photo_privacy,
If(pl.user_id && pl.photo_id !='',like_status,0) as islike ,
If(pr.user_id && pr.photo_id !='',1,0) as isreport,
p.user_visibility
from friends as fr
inner join users as u on u.id = (select distinct if (fr.user_id != $user_id,fr.user_id,(fr.to_user_id)) as rd
from friends) && fr.read_status = '1'
inner join photos as p on (p.user_visibility = 'p')
LEFT JOIN photolike as pl ON pl.photo_id = p.photo_id && pl.user_id = $user_id
LEFT JOIN photo_report as pr on pl. photo_id = pr.photo_id && pr.user_id = $user_id
ORDER BY p.photo_id DESC;
Use mysql indexing that will reduce the time
Try using group-by clause instead of distinct.
These 4 fields are related to each other
I want it to output it as:
In my query:
SELECT users.firstname, users.lastname, users.screenname, posts.post_id, posts.user_id,
posts.post, posts.upload_name, posts.post_type,
DATE_FORMAT(posts.date_posted, '%M %d, %Y %r') AS date,
COUNT(NULLIF(feeds.user_id, ?)) AS everybody, SUM(feeds.user_id = ?) AS you,
GROUP_CONCAT(CASE WHEN NOT likes.user_id = ? THEN
CONCAT_WS(' ', likes.firstname, likes.lastname)
END
) as names
FROM website.users users
INNER JOIN website.posts posts ON (users.user_id = posts.user_id)
LEFT JOIN website.feeds feeds ON (posts.post_id = feeds.post_id)
LEFT JOIN website.users likes ON (feeds.user_id = likes.user_id)
GROUP BY posts.pid
ORDER BY posts.pid DESC
Now, I am having a problem on which part should I join the friends table,
I want to display all the posts from friend_id or user_id and also the post from user who is currently logged in. If no friend matched on the friend table, then just output all the posts from user. Please guys I need your help.
friends.friend_id = friend of the current user
friends.user_id = current friend of the user
Thus, friends.friend_id = posts.user_id or friends.user_id = posts.user_id
If my friends table is not understandable, please help me change it to make it better.
You would like to see posts either from the user, or from his friends. Therefore, instead of joining with users, join with the subquery, like this:
SELECT users.firstname, users.lastname, users.screenname,
posts.post_id, posts.user_id, posts.post, posts.upload_name,
posts.post_type, DATE_FORMAT(posts.date_posted, '%M %d, %Y %r') AS date,
COUNT(NULLIF(feeds.user_id, ?)) AS everybody,
SUM(feeds.user_id = ?) AS you,
GROUP_CONCAT(CASE WHEN NOT likes.user_id = ? THEN
CONCAT_WS(' ', likes.firstname, likes.lastname) END) as names
FROM (SELECT user_id FROM website.users WHERE user_id = ?
UNION ALL
SELECT user_id FROM website.friends WHERE friend_id = ?
UNION ALL
SELECT friend_id FROM website.friends WHERE user_id = ?) AS who
JOIN website.users users ON users.user_id = who.user_id
JOIN website.posts posts ON users.user_id = posts.user_id
LEFT JOIN website.feeds feeds ON posts.post_id = feeds.post_id
LEFT JOIN website.users likes ON feeds.user_id = likes.user_i)
GROUP BY posts.pid
ORDER BY posts.pid DESC;
Test output here.
If i well understood you want to JOIN the friends table based on the friends = user_id and if not match JOIN on user_id of the friends table, so you can try with something like this :
SELECT users.firstname, users.lastname, users.screenname, posts.post_id, posts.user_id,
posts.post, posts.upload_name, posts.post_type,
DATE_FORMAT(posts.date_posted, '%M %d, %Y %r') AS date,
COUNT(NULLIF(feeds.user_id, ?)) AS friends, SUM(feeds.user_id = ?) AS you,
GROUP_CONCAT(CASE WHEN NOT likes.user_id = ? THEN
CONCAT_WS(' ', likes.firstname, likes.lastname)
END
) as names
FROM website.users users
INNER JOIN website.posts posts ON (users.user_id = posts.user_id)
LEFT JOIN website.feeds feeds ON (posts.post_id = feeds.post_id)
LEFT JOIN website.users likes ON (feeds.user_id = likes.user_id)
LEFT JOIN website.friends friends ON ((posts.user_id = friends.user_id) OR (posts.user_id = friends.friends_id) )
GROUP BY posts.pid
ORDER BY posts.pid DESC
I have basically added a JOIN with friends table with an OR on the two fields that you seem want to JOIN ...