I have this kind of table for simple chat:
messages table structure
+----+---------+-----------+---------+------+------+
| id | to_user | from_user | message | read | sent |
+----+---------+-----------+---------+------+------+
And i need to get list of each conversation which looks like that
Username ---- Message ---- Date
I am using this query to do it:
SELECT *
FROM `messages`
WHERE `sent`
IN (
SELECT MAX( `sent` )
FROM `messages`
WHERE `from_user` = '1' --id of user who is requesting the list
OR `to_user` = '1' --id of user who is requesting the list
GROUP BY `to_user` , `from_user`
)
LIMIT 0 , 30
And this works almost fine, my problem is that it returns me not the last message of that conversation but last message from each user so let's say user 1 and 2 is talking and i'm getting this list, this is what i get:
+----+---------+-----------+-----------------------+------+---------------------+
| id | to_user | from_user | message | read | sent |
+----+---------+-----------+-----------------------+------+---------------------+
| 3 | 2 | 1 | Message 1 from user 1 | 0 | 2012-01-11 13:20:54 |
| 4 | 1 | 2 | Message 1 from user 2 | 0 | 2012-01-11 13:24:59 |
+----+---------+-----------+-----------------------+------+---------------------+
And i would like to get only last message which is 4, cause sent field is the highest in 4th record so how can i solve it?
EDIT After deleting group by i'm getting only one message even if user was talking with more than one user
Here's how you do it:
SELECT *
FROM (SELECT *
FROM messages
WHERE from_user = ?
OR to_user = ?
ORDER by from_user, to_user, sent DESC
) x
GROUP BY from_user, to_user
ORDER BY sent DESC
LIMIT 1;
In mysql, a group by without aggregating the other columns returns the first row for each group. By selecting form an ordered row set (the inner query) we get the most recent row for each conversation.
SELECT *
FROM `messages`
WHERE `sent`
IN (
SELECT MAX( `sent` )
FROM `messages`
WHERE (`from_user` = '1' OR `to_user` = '1')
)
LIMIT 0 , 30
the groupby is going to conbine them i believe.
You are getting the last message from each user because you have done GROUP BY for both: to_user and from_user.
There is no need to use GROUP BY clause in your query.
Remove the group by clause in your in statement--it's useless in this case. It's returning a sent timestamp for each distinct pairing of to_user and from_user. You really just want the max sent where to_user or from_user equal some value. Lose the group by, and you'll return exactly one record showing the latest message either to or from a user.
It looks like this:
SELECT *
FROM `messages`
WHERE `sent`
IN (
SELECT MAX( `sent` )
FROM `messages`
WHERE `from_user` = '1' --id of user who is requesting the list
OR `to_user` = '1' --id of user who is requesting the list
)
LIMIT 0 , 30
Related
I have two tables:
chat:
id
name
chat_messages:
id
chat_id
user_id
message
created_at
I want to get a list of chats names ordered by recent activity. In other words, I want to get a list of chats where the first one is that with the bigger created_at field, and the last one is that with the smaller created_at field.
For example:
chat table:
1 General
2 News
chat_messages:
1 | 1 | 20 | Hello everybody | 2020-10 18:00:00
1 | 1 | 23 | this is a me... | 2020-10 18:00:05
1 | 1 | 15 | another message | 2020-10 18:00:15
1 | 2 | 22 | Anybody there? | 2020-10 17:00:00
1 | 2 | 45 | Hello?????????? | 2020-10 16:00:00
The desired result would be: ['News', 'General']
Any help?
Thanks
Try this:
SELECT DISTINCT `chat`.`name`
FROM `chat` JOIN `chat_message` ON `chat`.`id` = `chat_message`.`chat_id`
ORDER BY `chat_message`.`created_at` DESC
Explanation:
You only want the chat name to be returned. And you also want each value only once (that's the DISTINCT).
You ORDER BY the created_at field, and then the only thing left to do is to JOIN the tables.
Edit: you can try / improve here.
Select last Created_at for every chat:
SELECT m.chat_id,
max(m.created_at) as LastActivity
FROM chat_message as m
group by m.chat_id;
Let's find some infos about chats, by joining the chat table:
SELECT c.name,
info.LastActivity
FROM ( SELECT m.chat_id,
max(m.created_at) as LastActivity
FROM chat_message as m
group by m.chat_id) info -- virtual table "info" from first query
JOIN chat as c ON c.id = info.chat_id; -- add table chat to query
Now we want to add some info about the LastActivity
SELECT c.name,
info.LastActivity,
m2.user_id, -- Print Infos of LastActivityMessage
m2.message
FROM ( SELECT m.chat_id,
max(m.created_at) as LastActivity
FROM chat_message as m
group by m.chat_id) info
JOIN chat as c ON c.id = info.chat_id
JOIN chat_message as m2 ON info.LastActivity = m2.created_at; -- Search for Message at LastActivity
In out last query we see a problem: We do not want to search for a messag with it's date!
We're missing a propper primary-Key in table chat_message.
I'd suggest to calculate a unique id:
create table chat_message
(
id int not null auto_increment primary key, -- this is the message-id which will automatically set
chat_id int not null, -- where did the user post?
user_id int not null, -- which user?
message text,
created_at datetime
);
If we insert our Messages now like this:
INSERT INTO chat_message (chat_id, user_id, message, created_at)
VALUES
(2, 45, "Hello?", "2020-10-01 16:00:00"),
(2 , 22 , "Anybody there?" , "2020-10-01 17:00:00"),
(1, 20, "Hello everybody", "2020-10-01 18:00:00"),
(1, 23, "this is a me...", "2020-10-01 18:00:05"),
(1, 15, "another message", "2020-10-01 18:00:15");
..Ids are generated automatically:
id chat_id user_id message created_at
1 2 45 Hello? 2020-10-01T16:00:00Z
2 2 22 Anybody there? 2020-10-01T17:00:00Z
3 1 20 Hello everybody 2020-10-01T18:00:00Z
4 1 23 this is a me... 2020-10-01T18:00:05Z
5 1 15 another message 2020-10-01T18:00:15Z
This leads to a better query without searching for a single message with it's date:
-- Now we want to add some info about the LastActivity
SELECT c.name,
m2.id,
m2.created_at,
m2.user_id,
m2.message
FROM ( SELECT m.chat_id,
max(m.id) as LastActivity -- select last Id
FROM chat_message as m
group by m.chat_id) info
JOIN chat as c ON c.id = info.chat_id
JOIN chat_message as m2 ON info.LastActivity = m2.id; -- search for message with correct id
Table: statistics
id | user | Message
----------------------
1 | user1 |message1
2 | user2 |message2
3 | user1 |message3
I am able to find the count of messages sent by each user using this query.
select user, count(*) from statistics group by user;
How to show message column data along with the count? For example
user | count | message
------------------------
user1| 2 |message1
|message3
user2| 1 |message2
You seem to want to show Count by user, which message sent by user.
If your mysql version didn't support window functions, you can do subquery to make row_number in select subquery, then only display rn=1 users and count
CREATE TABLE T(
id INT,
user VARCHAR(50),
Message VARCHAR(100)
);
INSERT INTO T VALUES(1,'user1' ,'message1');
INSERT INTO T VALUES(2,'user2' ,'message2');
INSERT INTO T VALUES(3,'user1' ,'message3');
Query 1:
SELECT (case when rn = 1 then user else '' end) 'users',
(case when rn = 1 then cnt else '' end) 'count',
message
FROM (
select
t1.user,
t2.cnt,
t1.message,
(SELECT COUNT(*) from t tt WHERE tt.user = t1.user and t1.id >= tt.id) rn
from T t1
join (
select user, count(*) cnt
from T
group by user
) t2 on t1.user = t2.user
) t1
order by user,message
Results:
| users | count | message |
|-------|-------|----------|
| user1 | 2 | message1 |
| | | message3 |
| user2 | 1 | message2 |
select user, count(*) as 'total' , group_concat(message) from statistics group by user;
You could join the result of your group by with the full table (or vice versa)?
Or, depending on what you want, you could use group_concat() using \n as separator.
Use Group_concat
select user, count(0) as ct,group_concat(Message) from statistics group by user;
This will give you message in csv format
NOTE: GROUP_CONCAT has size limit of 1024 characters by default in mysql.
For UTF it goes to 1024/3 and utfmb4 255(1024/4).
You can use group_concat_max_len global variable to set its max length as per need but take into account memory considerations on production environment
SET group_concat_max_len=100000000
Update:
You can use any separator in group_concat
Group_concat(Message SEPARATOR '----')
Try grouping with self-join:
select s1.user, s2.cnt, s1.message
from statistics s1
join (
select user, count(*) cnt
from statistics
group by user
) s2 on s1.user = s2.user
In my MySQL database I have a table like this used for storing conversation messages from any people
id int(11) id of the message
from member_id int(11) id of the person the message was sent from
to member_id int(11) id of the person the message was sent to
date sent datetime date of when it was sent
active tinyint(1) if the message is deleted
text longtext the text of the message
from_read tinyint(1) boolean to know if the person who sent it read it
to_read tinyint(1) boolean to know if the person who it got sent to read it
So for example, it could have like:
from_member_id to_member_id date sent
1 2 june 12
1 3 june 13
2 3 june 14
3 1 june 9
So we have a conversation between person 1 and 2, 1 and 3, 2 and 3.
I am trying to get a select statement which will give me the most recent message that the current user is involved with from every conversation that user is in. So if 1 is logged in then I would expect to get 2 rows. The first row in the result set would be the second row above (july 13) because its the most recent, then then the second row in the result set would be the first row above (june 12), which are the most recent from 1's two conversations. The result set also needs to be sorted by date sent, so newer conversations are listed on top.
What I am trying to do is like the texting in android phones, where you see the list of conversations, and the most recent message in each listing.
This is my sql query
SELECT *
FROM (
SELECT *
FROM message
WHERE `from member_id`=1 OR `to member_id`=1
ORDER BY IF(`from member_id`=1, `to member_id`, `from member_id`)
) as t
GROUP BY IF(`from member_id`=1, `to member_id`, `from member_id`)
I just hardcoded 1 for now to be the current user. What I am doing is, sorting them by the id of the other person which I can check using the if statement, then grouping that result so I try to get the recent one from each conversation.
The problem is that when grouping, each group can have more than 1 rows, and it just seems to pick some random row. How can I get it to pick the row that has the most recent date sent value?
Are you looking for something like this?
SELECT m.*
FROM message m JOIN
(
SELECT from_member_id, to_member_id, MAX(date_sent) date_sent
FROM message
WHERE from_member_id = 1
GROUP BY from_member_id, to_member_id
) q
ON m.from_member_id = q.from_member_id
AND m.to_member_id = q.to_member_id
AND m.date_sent = q.date_sent
ORDER BY date_sent DESC
Sample output:
| FROM_MEMBER_ID | TO_MEMBER_ID | DATE_SENT |
----------------------------------------------
| 1 | 3 | 2013-06-13 |
| 1 | 2 | 2013-06-12 |
Here is SQLFiddle demo
UPDATE
SELECT m.*
FROM message m JOIN
(
SELECT LEAST(from_member_id, to_member_id) least_id,
GREATEST(from_member_id, to_member_id) greatest_id,
MAX(date_sent) date_sent
FROM message
WHERE from_member_id = 1
OR to_member_id = 1
GROUP BY LEAST(from_member_id, to_member_id),
GREATEST(from_member_id, to_member_id)
) q
ON LEAST(m.from_member_id, m.to_member_id) = q.least_id
AND GREATEST(m.from_member_id, m.to_member_id) = q.greatest_id
AND m.date_sent = q.date_sent
ORDER BY date_sent DESC
Sample output:
| FROM_MEMBER_ID | TO_MEMBER_ID | DATE_SENT |
----------------------------------------------
| 3 | 1 | 2013-06-14 |
| 1 | 2 | 2013-06-12 |
Here is SQLFiddle demo
SELECT
*
FROM message m INNER JOIN
(
SELECT
from_menber_id,
MAX(date_sent) AS sentdate
FROM message s
GROUP BY from_menber_id
) AS a
ON m.date_sent = a.sentdate AND a.from_menber_id = m.from_menber_id
I have table of user actions, each having a user associated, a type, and a timestamp. Here's a simplified example:
TABLE USER_ACTIONS
------------------------
USER | TYPE | TIMESTAMP
------------------------
a | OPEN | 0
b | OPEN | 1
a | CLOSE | 2
a | OPEN | 3
b | CLOSE | 4
a | CLOSE | 4
a | OPEN | 5 <-- "orphaned" OPEN, with no corresponding CLOSE. Should be ignored.
c | OPEN | 3
c | CLOSE | 5
a | OPEN | 6
a | CLOSE | 8
I'd like to get a list of transaction times out of this. Each CLOSE should match the previous OPEN, for a particular user.
The results I'd like will look something like this:
USER | TRANSACTION_TIME
-----------------------
a | 2
b | 3
a | 1
c | 2
a | 2
I don't care about the ordering.
I know that this is possible to do programmatically, but is it possible to do with some clever SQL?
UPDATE:
To do this programmatically, the general idea would be to...
Select all of the "CLOSE" actions, ordering by TIMESTAMP descending.
For each of those in that list, try to find a previous "OPEN" action made by the same user. Limit the TIMESTAMP to be before the "CLOSE" action TIMESTAMP, sort the results by TIMESTAMP DESC, and limit them to 1.
For that pair, calculate the time difference, and ouput the result.
Here's some pseudocode, but really I'd like SQL that does this cleverly:
for each CLOSE_ACTION IN ("SELECT USER, TYPE, TIMESTAMP FROM USER_ACTIONS WHERE TYPE='CLOSE' ORDER BY TIMESTAMP DESC;") {
OPEN_ACTION = "SELECT USER, TYPE, TIMESTAMP FROM USER_ACTIONS
WHERE TYPE='OPEN'
AND USER='<CLOSE_ACTION.USER>'
AND TIMESTAMP='<CLOSE_ACTION.TIMESTAMP>'
ORDER BY TIMESTAMP DESC
LIMIT 1";
if OPEN_ACTION != empty/null then {
print CLOSE_ACTION.USER, CLOSE_ACTION.TIMESTAMP - OPEN_ACTION.TIMESTAMP;
}
}
This takes each CLOSE event and matches it to the preceding event, if and only if the preceding event is an OPEN.
SELECT
OPEN.user,
OPEN.transaction_time
CLOSE.transaction_time
FROM
user_actions as CLOSE
INNER JOIN
user_actions as OPEN
ON OPEN.user = CLOSE.user
AND OPEN.transaction_time = (SELECT MAX(transaction_time) FROM user_action
WHERE user = CLOSE.user
AND transaction_time < CLOSE.transaction_time
AND type='OPEN')
WHERE
CLOSE.type = 'CLOSE'
Try:
select user,
timestamp,
(select min(timestamp)
from user_actions u
where a.user = u.user and
u.type = 'CLOSE' and
u.timestamp > a.timestamp) - timestamp
from user_actions a
where type = 'OPEN'
(Assumes there will always be a matching close for each open.)
The below query works fine for the question posed. I have executed it on my local machine and it seems to work fine.
select u1.user_name ,u2.timestamp- max(u1.timestamp) difference
from user_actions u1,user_actions u2
where u1.type = 'OPEN' and
u2.type = 'CLOSE' and
u1.timestamp <u2.timestamp and u1.user_name = u2.user_name
group by (u1.user_name , u2.timestamp);
I have one table with two queries and I need to sort it with descending type using ORDER BY. Here is my MySQL query that does not work properly:
(SELECT `text`
FROM `comments`
WHERE user_fr='".$user."' && archive='1'
ORDER BY `is_new_fr` DESC)
UNION
(SELECT `text`
FROM `message`
WHERE user_to='".$user."' && archive='1'
ORDER BY `is_new_to` DESC)
Description!
is_new_fr and is_new_to counts total new messages.
Here is my table contant:
user_fr | user_to | archive | is_new_fr | is_new_to| text
name1 | name2 | 1 | 2 | 0 | testing...
name2 | name1 | 1 | 0 | 5 | testing ...
I want to make an order that 1st will display note that has more messages to few, or by another words using DESCending type.
This is the display on the page I want to do:
Open dialog with name2. Messages (5)
Open dialog with name1. Messages (2)
Thank you!
The only way I know is a subquery:
SELECT `text`
FROM (
SELECT `text`, `is_new_fr` AS `is_new`
FROM `comments`
WHERE user_fr = '".$user."'
AND archive = '1'
UNION
SELECT `text`, `is_new_to` AS `is_new`
FROM `message`
WHERE user_to = '".$user."'
AND archive = '1'
) ORDER BY `is_new` DESC