I want to select the last 10 rows from my messages table that have either the receiver_id = 1 or the sender_id = 1 but those which have the receiver_id = 1 have DISTICT applied on the sender_id and those which have the sender_id = 1 have the DISTINCT property applied on the receiver_id.
Basically, what am I trying to say is that I want to select the very last 10 messages either sent either received by the user with the id of 1, sent to or sent by 10 different other users.
What have i tried so far:
SELECT * FROM (SELECT DISTINCT sender_id FROM messages WHERE receiver_id = 1) ORDER BY id DESC LIMIT 10) tmp ORDER BY id ASC
SELECT * FROM (SELECT * DISTINCT receiver_id FROM messages WHERE sender_id= 1) ORDER BY id DESC LIMIT 10) tmp ORDER BY id ASC
But trying to get the separately and adding the arrays (and sorting them by the ids of the messages) didn't quite work.
So I tried making a query like so:
SELECT * FROM (SELECT DISTINCT receiver_id, sender_id FROM messages WHERE (receiver_id = 1 OR sender_id = 1)) ORDER BY id DESC LIMIT 10) tmp ORDER BY id ASC
but it turned out my query is a total mess and, of corse, doesn't return me what I wanted.
P.S.: I am using XAMPP MySQL: MariaDB
Your second paragraph is:
Basically, what am I trying to say is that I want to select the 10
very last messages (rows) stored in the database that were either sent
or received by the user with the id of 1.
This is a fairly basic query:
SELECT m.*
FROM messages m
WHERE 1 IN (m.receiver_id, m.sender_id)
ORDER BY m.id DESC
LIMIT 10;
Related
Hello friends i want to display the last message from each user in this table, lets assume the $_SESSION['id'] is 1 so i want to display the last messages by each from or to user 1 here is my query:
SELECT * FROM message WHERE (msg_from='1') OR (msg_to='1') GROUP BY msg_from,msg_to ORDER BY MAX(msg_id) DESC
but when i run this it displays two messages from a user that is in the msg_from column and also in the msg_to column and it doesn't display the last inserted message, please guys, I need help.
since you are ordering by MAX(msg_id) DESC then you reqire only the first result because it will be the latest then you may do something like
SELECT TOP 1 * FROM message where msg_id IN (SELECT msg_id FROM message WHERE (msg_from='1') OR (msg_to='1') GROUP BY msg_from,msg_to ORDER BY MAX(msg_id) DESC);
or
SELECT * FROM message where msg_id IN (SELECT msg_id FROM message WHERE (msg_from='1') OR (msg_to='1') GROUP BY msg_from,msg_to ORDER BY MAX(msg_id) DESC) LIMIT 1;
hope it helps :)
Fetching the last messages for user-1 from others:
SELECT * FROM message WHERE msg_to = '1'
GROUP BY msg_from ORDER BY msg_date DESC
Fetching the last messages from user-1 to others:
SELECT * FROM message WHERE msg_from = '1'
GROUP BY msg_to ORDER BY msg_date DESC
I didn't use your MAX(msg_id) for ordering the data, instead, I used the date to get the latest messages.
Hi I'm trying to make a messaging system with php and mysql.
The mysql table is simple:
id
sender
receiver
text
timestamp
I'm trying to make the messaging somewhat like Facebook/Twitter so the list is in 'conversations' and the last message in the conversation is viewed.
This is what I have atm:
(SELECT * FROM messages WHERE receiver = 13 OR sender = 13 GROUP BY receiver,sender ORDER BY id ASC) ORDER BY id ASC
SELECT messages.* FROM messages, (SELECT MAX(id) as lastid FROM messages
WHERE receiver = 13 OR sender = 13
GROUP BY CONCAT(LEAST(receiver,sender),'.',GREATEST(receiver,sender))) as conversations
WHERE id = conversations.lastid
ORDER BY timestamp DESC
what you need is a unique conversation id between the chat-partners. i've simulated this with the subquery, hope this helps
Use DESC for fetch new rows, default is ASC
(SELECT * FROM messages WHERE receiver = 13 OR sender = 13 GROUP BY receiver,sender ORDER BY id DESC)
AND SET LIMIT 1 ,1 AFTER ORDER BY
I think you need to
(receiver = '{receiver id}' AND sender = '{sender id}' ) OR (receiver ='{sender id}' AND sender = '{receiver id}' )
UPDAte:
I'm not sure if it works perfect:
SELECT * FROM messages
WHERE receiver = 13
GROUP BY receiver,sender
ORDER BY timestamp DESC
LIMIT 1
UNION ALL
SELECT * FROM messages
WHERE sender = 13
GROUP BY receiver,sender
ORDER BY timestamp DESC
LIMIT 1
to reverse the order:
ORDER BY timestamp DESC
$query = mysql_query("SELECT *, MAX(date_time) FROM messages
WHERE user_to='$current_user' OR user_from='$current_user'
GROUP BY conversation
ORDER BY date_time DESC LIMIT 0,5") or die(mysql_error());
echo $message['MAX(date_time)'];
How can I display the column 'content' that is in the same row as the date_time being selected here, so the time matches with the message? Would it be something like
echo $message['MAX(content)'];
I am trying to have 'messages' in groups (called conversations) and I want to display the most recent message. It is currently displaying the most recent time but not the most recent message.
Thanks.
If your table has an autoincrement id column to use as the message id, then the highest id value should also be the latest message in the conversation
Try Below:
SELECT * from messages as ts
LEFT JOIN (select max(id) as maxid from messages group by date_time) as tsm
ON ts.id=tsm.maxid
WHERE (ts.user_to='$current_user' OR ts.user_from='$current_user')
ORDER BY ts.date_time DESC LIMIT 0,5
Assuming id is primary key column in your message table
Order your messages by date_time and only take the first tuple (LIMIT 1).
I don't get why you aggregate if you just want the latest tuple from messages. Perhaps you can elaborate this and provide some more information about the table.
SELECT content, date_time
FROM messages
WHERE user_to='$current_user' OR user_from='$current_user'
ORDER BY date_time DESC
LIMIT 1
If you have auto incrementing key, you can try:
$query = mysql_query("SELECT * FROM messages as m
WHERE user_to='$current_user' OR user_from='$current_user' AND
id IN (SELECT MAX(id) FROM messages as m2 GROUP BY m2.conversation)
ORDER BY date_time DESC LIMIT 0,5") or die(mysql_error());
I want to select the newest posts from users, I am looking for the most efficient way to do this.
Currently this selects the first post, not the last:
$query = mysql_query("
SELECT *
FROM posts
WHERE toID=fromID
GROUP BY fromID
ORDER BY date DESC LIMIT 3");
Table Structure:
Table: posts
id ToID FromID Post State Date
1 1 1 Hey 0 1325993600
2 1 6 okay yeah 0 1325993615
3 1 2 again 0 1325994600
4 6 6 yeah2 0 1325995615
so from this above example it would return id: 1 and 4.
toID=fromID is just to get the post that is a status message, meaning the user posted something on their own page, not someone elses.
I want to get the most recent status from the last 3 users that have updated their status.
The ID thing would still work theoretically, provided that the ID's never change...
I would recommend using a timestamp field in the table structure called "date" and use the "CURRENT_TIMESTAMP" as default value, this will auto-populate the date/time on the record upon insert...
Order by this field DESC, limit x
Also, I have experienced many cases of the wrong data appearing thanks to grouping... Make sure your data is correct before ORDER BY and LIMIT is applied
For getting posts from user1 to user1 there's no need to group by:
SELECT * FROM posts
WHERE toID=fromID
ORDER BY date DESC LIMIT 3
For getting posts from * to user1:
SELECT * FROM posts
WHERE toID="USER1_ID"
ORDER BY date DESC LIMIT 3
For getting posts from * to user1, only unique users:
SELECT * FROM posts
WHERE toID="USER1_ID"
GROUP BY FromID
ORDER BY date DESC LIMIT 3
Somtimes you will run into the problem where GROUPED records are not ordered by ORDER BY, because the ORDER BY is applied to the result AFTER the grouping is applied... To achieve a workaround:
SELECT * FROM (
SELECT * FROM posts
WHERE toID="USER1_ID"
ORDER BY date DESC
) as `derived` GROUP BY FromID LIMIT 3
To Get the last 3 users who have most recently sent themselves a post:
SELECT * FROM (
SELECT * FROM posts
WHERE toID=fromID
ORDER BY date DESC
) as `derived` GROUP BY FromID LIMIT 3
try this query.
$query = mysql_query("SELECT * FROM posts WHERE toID=fromID GROUP BY id ORDER BY date DESC LIMIT 3");
This is a more detailed question as my previous attempt wasn't clear enough. I'm new to MySQL and have no idea about the best way to do certain things. I'm building a voting application for images and am having trouble with some of the finer points of MySQL
My db
_votes
id
voter_id
image_id
_images
id
file_name
entrant_id
approved
_users
id
...
Basically I need to do the following:
tally up all votes that are approved
return the top 5 with the most votes
check if the user has voted on each of these 5 (return Boolean) from another table
I've tried variations of
SELECT i.id, i.file_name, i.total_votes
FROM _images i WHERE i.approved = 1
CASE WHEN (SELECT count(*) from _votes v WHERE v.image_id = i.id AND v.voter_id = ?) > 0 THEN '1' ELSE '0' END 'hasvoted'
ORDER BY i.total_votes DESC LIMIT ".($page*5).", 5
is that something I should try and do all in one query?
This query was working fine before I tried to add in the 'hasvoted' boolean:
SELECT id, file_name, total_votes FROM _images WHERE approved = 1 ORDER BY total_votes DESC LIMIT ".($page*5).", 5
At the moment I'm also storing the vote count in the _images table and I know this is wrong, but I have no idea about how to tally the votes by image_id and then order them.
Let me give this a shot to see if I understand your question:
SELECT i.*,(SELECT COUNT(*) FROM _votes WHERE i.id = image_id) AS total_votes, (SELECT count(*) from _votes where i.id = image_id and user_id = ?) as voted FROM _images AS i WHERE i.approved = 1