I have the following table structure for notifications table.
id user_id post_id type status date seconduser_id
1 1 23 1 0 somedate 4
2 2 25 2 0 somedate 3
3 3 26 1 0 somedate 4
4 4 28 2 1 somedate 5
5 5 21 2 0 somedate 4
---
---
and so on
Here type = 1 means a like and type = 2 means a comment. status = 0 means seconduser_id hasn't seen the notification yet. seconduser_id is the notification recipient.
Is it possible to get notifications for 1 user (example seconduser_id = 4), with notification grouped by type, showing count and the latest user_id for each type (in one query)?
The implementation of this would be something like User3 and 10 other people liked your post.
Edit: So far I have something that pulls all notification for user 4 and then groups in php. I don't think this is efficient and so am looking for better solutions.
The basic answer is an aggregation query. The complication is getting the latest user id for each type. THere is one method, using substirng_index()/group_concat():
select type, count(*),
substring_index(group_concat(user_id order by id desc), ',', 1) as latest_user
from notifications n
where secondaryuser_id = 4 and status = 0
group by type;
I am not sure if you also want to filter by status.
Edit (added by OP):
Using the above code and grouping by both post_id and type. Because you want to say User1 and 10 others liked your post. Which means for each grouped notification, post_id has to be unique.
SELECT *, substring_index(group_concat(user_id order by id desc), ',', 1) as latest_user, COUNT(post_id) AS total
FROM notifications n
WHERE seconduser_id = 4 and status = 0
GROUP BY post_id, type
Try this:
SELECT MAX(user_id) AS LatestUser, type, COUNT(type) AS total
FROM notifications
WHERE seconduser_id = 4
GROUP BY type
Related
I'm trying to create an sql (mariadb) request that select multiples columns but need two columns to be a unique pair but making sure the pair selected has its created_at value the least than the other duplicata pairs.
Here is what my table approximately looks like :
id
from_user_id
to_user_id
created_at
1
1
2
1000000005
2
2
1
1000000002
3
2
3
1000000008
4
5
6
999999999
5
6
5
100000006
I made this table precise to explain the request I want.
So I want to select the distinct pair (from_user_id, to_user_id) implying that the couple (1,2) which could also be (2,1) should be unique. The second rule is it should pick the couple with the minimum created_at value.
So the result table I want is :
id
from_user_id
to_user_id
created_at
2
2
1
1000000002
3
2
3
1000000008
4
5
6
999999999
2,1,1000000002 because the created_at is lesser than the other same couple case (1,2,1000000005).
In this case if I want only the values above created_at:999999999 to be selected I just have to add one condition.
I really hope my question is clear. I'm struggling to make distinct pairs work with other columns.
Thanks in advance for your answers.
WITH
cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY GREATEST(from_user_id,to_user_id),
LEAST(from_user_id,to_user_id)
ORDER BY created_at) rn
FROM table
)
SELECT *
FROM cte
WHERE rn = 1
I have this table
**applications**
id user_id company_id shortlisted
1 10 99 0
2 10 100 1
3 10 101 1
4 10 102 0
5 11 99 1
6 12 99 0
6 12 101 0
What I want is to select all users
which have been shortlisted at-least once
which have not been shortlisted at all
For the first case, i have the following query:
SELECT user_id
from applications
where shortlisted=1
Group
By user_id
and this gives me the expected result like below
**applications**
user_id
10
11
But I'm trying the following query for the second case and it returns me an empty set:
Select user_id
from applications as Application
where shortlisted=0
and NOT EXISTS(Select user_id from applications where user_id=Application.user_id and shortlisted=1)
What am i missing?
PS: Please ignore any typos as i typed them manually for this post.
To get both results in a single query simply use aggregation:
select user_id, max(shortlisted) as was_shortlisted
from applications
group By user_id
You can use group by and having for both.
For the first:
select user_id
from applications
group By user_id
having max(shortlisted) = 1;
For the second:
select user_id
from applications
group By user_id
having max(shortlisted) = 0;
In all honesty, your version with the where is more efficient for the first query. This is just to show how closely related the queries are.
You can try following query;
select user_id from table1
group by user_id having MIN(shortlisted) = 1
This will give you to at least have shortlisted = 1 condition and don't have shortlisted = 0 records.
I've got a table of statuses.
ID UserID StatusID Description
1 0 0 ready
2 0 1 on hold
3 0 2 cancelled
4 3 1 waiting
5 3 2 deleted
6 3 4 waiting on supplier
7 4 5 postponed
etc...
UserID 0 holds my default descriptions. I want to pull out all the status text for a user and the defaults if there are no user values with the same status ID.
eg user ID 3 should return
ID UserID StatusID Description
1 0 0 ready
4 3 1 waiting
5 3 2 deleted
6 3 4 waiting on supplier
eg user ID 4 should return
ID UserID StatusID Description
1 0 0 ready
2 0 1 on hold
3 0 2 cancelled
7 4 5 postponed
User ID 7 should return
ID UserID StatusID Description
1 0 0 ready
2 0 1 on hold
3 0 2 cancelled
so far I have this:
select description.* from status_description description
join (
select max(id) as maxid from (
select * from status_description default where default.user_id = 0
union
select * from status_description userstatus where ectfsa.user_id = 67
) as subset
group by subset.ID
) as newest_desc on newest_desc.maxid = description.id
order by StatusID asc;
Where I union both the users statuses and the default statuses and then join the max ID per statusID back with the original table to get the user and defaults.
This works fine until someone adds a new USERID0 status with a higher ID. Eg we decide that all users should now have an option of "postponed" and we add a line
ID UserID StatusID Description
8 0 5 next week
which should be the same "status" as "postponed" but worded differently for all users to what UserID 4 has.
If there a more graceful way of doing this without using Max and just selecting defaults and adding user statuses overwriting defaults where they already exist?
I need to keep this within mysql (ie not php) as its going to be a join to another query to pull out user specific descriptions for another report.
I think this may be a solution :
select * from (
select StatusID,Description from status_description default where default.user_id = 0
union
select StatusID,Description from status_description userstatus where ectfsa.user_id = 67
) as StatusID;
I don't need the UserID in the result or the ID. By removing these from the union by replacing the * with field names the union automatically removes duplicates, or at least appears to...
I'm implementing a messaging system on my site, and have a table to store the conversation participants like so:
conversation_id user_id
3 2
3 28
4 1
4 2
5 1
5 2
5 28
I can't find a query that will let me check if a conversation already exists between 2 users (or more). I basically want 3 (conversation_id) to be returned if user 2 is sending to user 28, or vice-versa, like that I can keep the conversation going even if they haven't explicitly replied to a previous message.
With the having clause you can filter to only the conversation_ids where both users participate
select conversation_id
from your_table
where user_id in (2, 28)
group by conversation_id
having count(distinct user_id) = 2
oh, that's driving me crazy. I have messages table, simplified example:
id[int] - sender[int] - receiver[int] - conv_id[bigint] - received[int] - stamp[int]
1 2 1 5 1 timestamp+1
2 2 1 5 1 timestamp+2
3 3 1 6 1 timestamp+3
4 4 1 7 1 timestamp+4
5 5 1 8 1 timestamp+5
6 5 1 8 1 timestamp+6
now I'm interesting a results grouped by receiver, limited to 3 senders and sorted by stamp DESC. How to do that?
I already have this, but it's taking all messages and i believe that's not the best way to not loose performance, even though I'm really not the guru of mysql:
SELECT id, sender, receiver, conv_id FROM
(SELECT m.id, m.sender, m.receiver,m.conv_id
FROM messages AS m WHERE m.receiver = 1 AND m.received = 1
ORDER BY m.stamp DESC) as messages_tmp
WHERE receiver = 1 GROUP BY conv_id ORDER BY NULL LIMIT 0,3
This should return these results in that order:
id = 6, id = 4, id = 3
The thing is I'm already doing to queries of this, for m.received = 0 and than if not enough of the results, for m.received = 1. So far my database isn't too big, but if it's gets bigger I'm afraid it can be slow. I'm thinking about possibility to limit results of the subquery, but have no idea how to that and be sure I'll get enough results after GROUP.
Thanks.
SELECT id, sender, receiver,conv_id
FROM messages WHERE receiver = 1 AND received = 1
GROUP BY conv_id
ORDER BY id DESC LIMIT 3