I have a table named messages:
date (format DATETIME)
id
text
user_id(FK)
and a table named users:
id
name
how to get count of messages with given date (message date = xxxx-xx-xx) for each user?
like:
user message_count
user1_name 5
user2_name 2
and how get users with 3 or more message today?
SELECT users.name
FROM users
WHERE Users.id IN ( SELECT Messages.user_id
FROM chat.Messages
GROUP BY Messages.user_id
HAVING COUNT(*) >=3
)
It returns users who have 3 or more messages, how to get users, who have 3 or more message today? where date = now()
How to get count of messages with given date (message date = xxxx-xx-xx) for each user?
Query #1:
SELECT
users.name,
COUNT(*) message_count
FROM messages
INNER JOIN users ON messages.user_id = users.id
WHERE messages.date = PUT_YOUR_DATE_HERE
GROUP BY messages.user_id;
How to get count of messages for those users who have 3 or more messages today?
Query #2:
SELECT
users.name,
COUNT(*) message_count
FROM messages
INNER JOIN users ON messages.user_id = users.id
WHERE messages.date = CURDATE()
GROUP BY messages.user_id
HAVING COUNT(*) >= 3;
select b.name,count(*) from messages a,users b where date ='2016-05-28' group by b.name
Related
So i have 2 tables, users and messages
users table
uid, username
messages table
mid, senderid, receiverid, message, timestamp
The query i have at the moment is retrieving the uid, username and last timestamp of each chat conversation
SELECT DISTINCT
IF (messages.senderid = '5e9b95786a71f8.25790415', messages.receiverid, messages.senderid) as uid,
(SELECT username FROM users WHERE uid = IF (messages.senderid = '5e9b95786a71f8.25790415', messages.receiverid, messages.senderid)) as username,
MAX(messages.timestamp) as last_timestamp
FROM messages JOIN users ON users.uid = messages.senderid WHERE messages.senderid = '5e9b95786a71f8.25790415' OR messages.receiverid = '5e9b95786a71f8.25790415'
GROUP BY 1,2
which outputs below
uid | username | last_timestamp
1 | Developer | 1601826378
2 | BetaTester | 1601826301
What i need to add to this query is the message field which is based on the last_timestamp i have in the output.
How can the query be updated to include the extra message field?
Try this.
select A.uid, C.username, B.timestamp, B.message from (SELECT DISTINCT
IF (messages.senderid = '5e9b95786a71f8.25790415', messages.receiverid, messages.senderid) as uid,
MAX(messages.mid) as max_mid
FROM messages WHERE messages.senderid = '5e9b95786a71f8.25790415' OR messages.receiverid = '5e9b95786a71f8.25790415'
GROUP BY 1) A join messages B on A.max_mid = B.mid join users C on A.uid = C.uid
I'm a little confuse, since you already got last_timestamp why don't just left join message table again.
left JOIN messages m2 on last_timestamp = m2.timestamp and mid = m2.mid
and just select message column
select m2.message
SELECT COUNT(*) AS Total, id
FROM (SELECT * FROM orders WHERE status_id=2)
RIGHT JOIN users ON users.id = orders.courier
I'm getting this error
#1054 - Unknown column 'orders.courier' in 'on clause'
Is this what you are looking for?
select user_id
from orders
having sum(status_id = 2) >= 1
group by user_id
This will give you all user_ids that have at least one order with status_id = 2 (assuming that the column in orders that store the id of the user is called user_id).
If you want to list all users that ordered and count how many orders they have with status_id = 2:
select user_id, sum(status_id = 2) count_status_2
from orders
group by user_id
And if you want even users that never ordered:
select u.user_id, coalesce(sum(o.status_id = 2), 0) count_status_2
from users u
left join orders o on o.user_id = u.user_id
group by u.user_id
I have two table User and transactions where transaction table has user id
UserID | UserName
TransactionID | UserID | TransactioDate
I need to get number of distinct users that transact daily within a timeframe, i.e. users with txns everyday in a certain timeframe.
I can get distinct user for each day,
SELECT t.TransactioDate, COUNT(DISTINCT u.UserID)
FROM user u
INNER JOIN transaction t ON u.UserID = t.UserID AND t.TransactioDate BETWEEN '2018-01-13' AND '2018-02-12'
GROUP BY CAST(t.TransactioDate AS DATE)
ORDER BY t.TransactioDate
but i was wondering how to get users with txns everyday in a certain timeframe.
Join it with a subquery that counts the number of distinct days that each user has transactions, and returns the ones where this count is the same as the number of days in the time period.
SELECT DATE(t.TransactioDate) AS date, COUNT(DISTINCT u.UserID)
FROM user u
INNER JOIN transaction t ON u.UserID = t.UserID
INNER JOIN (
SELECT userID
FROM transaction
WHERE TransactioDate BETWEEN '2018-01-13' AND '2018-02-12'
GROUP BY userID
HAVING COUNT(DISTINCT(DATE(transactioDate))) = DATEDIFF('2018-02-12', '2018-01-13') + 1
) AS t1 ON t1.userID = u.userID
WHERE t.TransactioDate BETWEEN '2018-01-13' AND '2018-02-12'
GROUP BY date
ORDER BY t.TransactioDate
I have two tables (messages and user). I want to select the last (msg_id,text) from the messages table for a particular ad_id and need to select the name of the user from the user table.
SELECT u.id
, m.date
, m.ad_id
, max(m.msg_id)as msg_id
, u.first_name
, m.text
, m.u_to_id
, m.u_from_id
FROM user u
JOIN messages m
ON CASE WHEN m.u_from_id ='14' THEN u.id = m.u_to_id
ELSE u.id = m.u_from_id END
AND (m.u_from_id='14' OR m.u_to_id='14')
AND m.ad_id='20'
GROUP BY CONCAT(m.ad_id,u.id)
ORDER by m.msg_id DESC
this query is working but I can't select t the last m.textTable structure
SELECT u.id, m.text
FROM user u
JOIN messages m ON m.msg_id = (SELECT max(msg_id) FROM messages WHERE u_from_id = u.id)
I simplified your query to show the logic relevant to your question. Basically you want to join your messages table on the msg_id that is equal to the inner query of the max msg_id with that user.
After so many experiments added a new column(bargainer) for identify the recipient and this query working fine for me
select m.msg_id,m.text,m.status,m.date,m.bargainer,m.ad_id,u.first_name,u.id from user u JOIN messages m where msg_id in (select max(msg_id) from messages m where m.ad_id=20 and u.id=m.bargainer group by(m.bargainer))group by(m.msg_id) order by msg_id DESC
I have a conversations table that is very simple but important (created_by is the code of the user who sent the message). Then, I have a conversations_messages table (sent_by is the code of the user who sent the message, message is my message text and created_at is the timestamp). Finally I have a conversations_users table that has the user_id (code of the user who received the message) and created_by (code of the user who sent the message).
I am trying to implement the functionality of inbox, I want to show the last message per conversation. user_id in conversations_users is the user who received the message and the user who is consulting his inbox.
I tried this query but I don't know how to deal with the created_at timestamp (in table conversations_messages) in order to get the most recent message per conversation.
select CM.id, CM.message, CM.created_at, C.id as 'conversation_id' from conversations_users CU
join conversations C on CU.conversation_id = C.id join conversations_messages CM on C.id = CM.conversation_id where CU.user_id = 1 and CM.created_at = '2015-03-10 14:18:02'
I have to replace '2015-03-10 14:18:02' for some most recent created_at timestamp thats my problem.
My tables are:
conversations_users: id, conversation_id, user_id, created_by
conversations: id, created_by
conversations_messages: id, conversation_id, sent_by, message, created_at
Thanks!
SELECT cm.*
FROM conversations_messages cm
JOIN conversations c ON cm.conversation_id = c.id
JOIN conversations_users cu ON cu.conversation_id = c.id
WHERE cu.user_id = 1
AND NOT EXISTS ( SELECT 'a'
FROM conversations_messages cm2
WHERE cm2.conversation_id = cm.conversation_id
AND cm2.created_at > cm.created_at
)
GROUP BY c.id
Here's one approach joining the table back to itself using the max aggregate, grouping by each conversation:
select CM.id, CM.message, CM.created_at, C.id as 'conversation_id'
from conversations_users CU
join conversations C on CU.conversation_id = C.id
join conversations_messages CM on C.id = CM.conversation_id
join (
select max(created_at) maxcreated_at, conversation_id
from conversations
group by conversation_id
) T on T.maxcreated_at = CM.created_at
and T.conversation_id = CM.conversation_id
where CU.user_id = 1