Select first row within same value of one column - mysql

I have these values in a table called message:
id message_id message
1 1 Hello sir
2 1 Hi dear.
3 2 Send by admin.
4 2 send by helper.
5 3 created by me.
6 3 Done by user.
What i wnat in result the table is below.
id message_id message
1 1 Hello sir
3 2 Send by admin.
5 3 created by me.
Any help please.

you can try as,all you need is just use a distinct clause which selects unique values.
SELECT DISTINCT * from meassage;

select M.id, M.message_id, M.message
from message M
WHERE M.ID = (SELECT MIN(M1.ID) FROM MESSAGE M1 WHERE M1.MESSAGE_ID = M.MESSAGE_ID) ;

Related

How to check if two users have a common chat room?

Currently, I am trying to figure out how to find the common chat rooms between two users by checking their email addresses.
chat_rooms table:
id email chat_room_id
----------------------------------------------
1 johndoe#gmail.com 3
2 janedoe#gmail.com 3
3 test#gmail.com 42
4 check#gmail.com 64
5 janedoe#gmail.com 7
6 test#gmail.com 19
7 johndoe#gmail.com 6
How can I write a MySQL query that can return the common chat_room_id of johndoe#gmail.com and janedoe#gmail.com. Please advise.
You can do this with aggregation:
select chat_room_id
from mytable
where email in ('johndoe#gmail.com', 'janedoe#gmail.com')
group by chat_room_id
having count(*) = 2
This assumes that the same user cannot appear twice in the same room. If that's not the case, then you would just change the having clause to:
having count(distinct email) = 2
Or:
having min(email) <> max(email)

Getting only one row per combination of two IDs [duplicate]

I have a simple table maintaining messages between users. The table structure looks like
sender receiver message sendtime
1 2 m1 2012-01-01 12:12:12
2 1 m2 2012-01-01 12:50:20
1 2 m3 2012-01-01 12:55:55
1 3 m4 2012-01-02 05:05:05
1 4 m5 2012-01-05 05:20:20
4 1 m6 2012-01-06 06:05:00
4 1 m7 2012-01-07 11:11:11
2 4 m8 2012-01-08 05:01:01
Now, for the user with ID 1, I need the result like this
sender receiver message sendtime
1 2 m3 2012-01-01 12:55:55
1 3 m4 2012-01-02 05:05:05
4 1 m7 2012-01-07 11:11:11
That is I need the recent messages for the particular user whether he is sender or receiver.
Though, it seem easy, I cannot find a way to write a single mysql query.
Also, I want suggestion if for this kind of solution, my table design is poor.
Note: One thing, which I think I should mention is, for instance there are multiple communication between user 1 and user 2. I need the latest communication between them two whether user 1 is a sender or receiver, only the recent.
Most users are suggesting the query, that will bring one record with the sender 1, receiver 2 and then sender 2, receiver 1. I don't need these two record because this is a communication between same user 1 and user 2. I need one the latest one for the user specified with each of other users.
Thanks,
SELECT data.* FROM
(SELECT MAX(sendtime) AS sendtime
FROM data
WHERE 1 IN (sender,receiver)
GROUP BY IF (1 = sender,receiver,sender)) AS latest
LEFT JOIN data ON latest.sendtime = data.sendtime AND 1 IN (data.sender, data.receiver)
GROUP BY IF (1 = data.sender,data.receiver,data.sender)
I recommend to use unique sequence id in your table to avoid external GROUP BY.
If you have incrementing unique message_id (i.e. bigger message_id corresponds to later sendtime), the query would be much simpler:
SELECT data.* FROM
(SELECT MAX(message_id) AS message_id
FROM data
WHERE 1 IN (sender,receiver)
GROUP BY IF (1 = sender,receiver,sender)) AS latest
LEFT JOIN data USING(message_id)
Try making message as the primary key and put sender and receiver in 2 separate tables for the same message like this:
Table 1: {message, sender, sendtime}
Table 2: {message, receiver, sendtime}
Try this:
SELECT Distinct (*)
FROM tableName m
WHERE date =
(
SELECT MAX (date)
FROM tableName
)
To me it feels simpler to use an own ID based on the sender and receiver. No JOIN and just one GROUP BY needed:
SELECT sender, receiver, message, MAX(time)
FROM (
SELECT *,
CASE WHEN sender < receiver
THEN CONCAT(sender,'&', receiver)
ELSE CONCAT(receiver,'&', sender) END AS fromto
FROM data) AS a
GROUP BY fromto
I take 30 mins to find the solution for this problem because I have thew same problem
just take it simple
SELECT DISTINCT sent_by FROM messages WHERE sent_to = 1 UNION SELECT DISTINCT sent_to FROM messages WHERE sent_by = 1
This will work out
$query = "SELECT * FROM tableName WHERE `sender`='$userNumber' OR `receiver`='$userNumber' ORDER BY `sendtime` DESC LIMIT 0,1";

MySQL exclude ids using joins

I have 2 tables:
uid uname
1 alex
2 anna
3 sergey
4 arnold
5 john
mid message uid
1 hello 3
2 DELETED 3
3 xcvcxv 4
4 bye 1
5 DELETED 2
6 4452 5
I would like to get all messages, but if message contains "DELETED", exclude this message' userID from all messages (after this message do not return messages from userID 3 and 2), using JOINs and without NOT IN.
Thanks for the help.
This should select all messages of users that don't have DELETED messages:
SELECT m.*
FROM message m
LEFT JOIN message m2
ON m2.uid = m.uid
AND m2.message = 'DELETED'
WHERE m2.mid IS NULL;

Sum values in mysql table where userid is identical

I have read the different answers here on SO, but I am stuck on this question. Please help.
I have this mysql view named "activeuser":
userid COUNT(*) ACRONYM
1 23 admin
2 2 doe
3 4 tompa
12 4 Marre
13 1 Mia
1 2 admin
3 1 tompa
12 1 Marre
13 1 Mia
2 1 doe
3 1 tompa
12 1 Marre
How can I sum the COUNT column so that I get the following wanted result?
userid COUNT(*) ACRONYM
1 25 admin
2 3 doe
3 6 tompa
12 6 Marre
13 1 Mia
EDITED:
I used this query to create the view:
CREATE VIEW activeuser AS
(SELECT boats_comments.userid, COUNT(boats_comments.userid), boats_user.acronym, boats_user.email
FROM boats_comments
INNER JOIN boats_user
ON boats_comments.userid = boats_user.id
GROUP BY boats_comments.userid
ORDER BY COUNT(boats_comments.userid) DESC)
UNION ALL
(SELECT boats_answers.userid, COUNT(boats_answers.userid), boats_user.acronym, boats_user.email
FROM boats_answers
INNER JOIN boats_user
ON boats_answers.userid = boats_user.id
GROUP BY boats_answers.userid
ORDER BY COUNT(boats_answers.userid) DESC)
UNION ALL
(SELECT boats_questions.userid, COUNT(boats_questions.userid), boats_user.acronym, boats_user.email
FROM boats_questions
INNER JOIN boats_user
ON boats_questions.userid = boats_user.id
GROUP BY boats_questions.userid
ORDER BY COUNT(boats_questions.userid) DESC)
My goal is to see which users are the most active by checking the number of comments, questions and answers... but I got stuck...
As the results in your view has duplicates I guess the underlying code for the view is grouping on something it maybe shouldn't be grouping on.
You can get the results you want by applying SUM to it:
select userid, sum("whatever column2 is named") as "Count", Acronym
from activeuser group by userid, Acronym;
select userid, count(*) from activeuser group by userid;

Select the Latest Message Between the communication of two user in mysql

I have a simple table maintaining messages between users. The table structure looks like
sender receiver message sendtime
1 2 m1 2012-01-01 12:12:12
2 1 m2 2012-01-01 12:50:20
1 2 m3 2012-01-01 12:55:55
1 3 m4 2012-01-02 05:05:05
1 4 m5 2012-01-05 05:20:20
4 1 m6 2012-01-06 06:05:00
4 1 m7 2012-01-07 11:11:11
2 4 m8 2012-01-08 05:01:01
Now, for the user with ID 1, I need the result like this
sender receiver message sendtime
1 2 m3 2012-01-01 12:55:55
1 3 m4 2012-01-02 05:05:05
4 1 m7 2012-01-07 11:11:11
That is I need the recent messages for the particular user whether he is sender or receiver.
Though, it seem easy, I cannot find a way to write a single mysql query.
Also, I want suggestion if for this kind of solution, my table design is poor.
Note: One thing, which I think I should mention is, for instance there are multiple communication between user 1 and user 2. I need the latest communication between them two whether user 1 is a sender or receiver, only the recent.
Most users are suggesting the query, that will bring one record with the sender 1, receiver 2 and then sender 2, receiver 1. I don't need these two record because this is a communication between same user 1 and user 2. I need one the latest one for the user specified with each of other users.
Thanks,
SELECT data.* FROM
(SELECT MAX(sendtime) AS sendtime
FROM data
WHERE 1 IN (sender,receiver)
GROUP BY IF (1 = sender,receiver,sender)) AS latest
LEFT JOIN data ON latest.sendtime = data.sendtime AND 1 IN (data.sender, data.receiver)
GROUP BY IF (1 = data.sender,data.receiver,data.sender)
I recommend to use unique sequence id in your table to avoid external GROUP BY.
If you have incrementing unique message_id (i.e. bigger message_id corresponds to later sendtime), the query would be much simpler:
SELECT data.* FROM
(SELECT MAX(message_id) AS message_id
FROM data
WHERE 1 IN (sender,receiver)
GROUP BY IF (1 = sender,receiver,sender)) AS latest
LEFT JOIN data USING(message_id)
Try making message as the primary key and put sender and receiver in 2 separate tables for the same message like this:
Table 1: {message, sender, sendtime}
Table 2: {message, receiver, sendtime}
Try this:
SELECT Distinct (*)
FROM tableName m
WHERE date =
(
SELECT MAX (date)
FROM tableName
)
To me it feels simpler to use an own ID based on the sender and receiver. No JOIN and just one GROUP BY needed:
SELECT sender, receiver, message, MAX(time)
FROM (
SELECT *,
CASE WHEN sender < receiver
THEN CONCAT(sender,'&', receiver)
ELSE CONCAT(receiver,'&', sender) END AS fromto
FROM data) AS a
GROUP BY fromto
I take 30 mins to find the solution for this problem because I have thew same problem
just take it simple
SELECT DISTINCT sent_by FROM messages WHERE sent_to = 1 UNION SELECT DISTINCT sent_to FROM messages WHERE sent_by = 1
This will work out
$query = "SELECT * FROM tableName WHERE `sender`='$userNumber' OR `receiver`='$userNumber' ORDER BY `sendtime` DESC LIMIT 0,1";