I have following messages table:
I want to list all messages in a inbox like for example,
lets assume I am user number 1 so my inbox should show
me following:
Messages with user 2
Messages with user 3
Messages with user 4
What is the best way to build a query for this?
I've came up with somewhat weird query that looks like: (pseudo code)
SELECT
*
FROM
`message`
WHERE
(`user_id_sender` = 1 AND `user_id_receiver` NOT 1)
OR
(`user_id_sender` NOT 1 AND `user_id_receiver` = 1)
Thank you
That's very simple
SELECT user_id_sender ID,
(SELECT user_id_reciever
FROM messages
WHERE user_id_sender NOT IN(ID)
GROUP BY user_id_reciever
)
FROM message
Related
I have some analytical data for different cases. Each case is associated with one or more photos. Each photo is analyzed by two users.
The stored data looks like
What I want is to have SQL query to generate agreement result as shown below
So, for case 17116 there is agreement on photo 175062 from user id 26 and 27. Similar case is with photo id 176031 from user id 24 and 29.
Can somebody help me out to achieve this.
Thanks for sharing your valuable time.
Here is sample data to test with
Case Id,Photo Id,FeatureCheck,Result,CheckedBy
17116,173442,severity,none,24
17116,173442,severity,low,25
17116,175062,severity,none,26
17116,175062,severity,none,27
17116,175427,severity,medium,24
17116,175427,severity,high,28
17116,175748,severity,low,22
17116,175748,severity,none,30
17116,176031,severity,low,24
17116,176031,severity,low,29
17277,175309,severity,none,24
17277,175309,severity,none,25
17277,175649,severity,none,24
17277,175649,severity,none,25
You can try below query:
select PhotoId,
max(FeatureCheck),
max(Result),
max(CheckedBy),
min(CheckedBy)
from MyTable
group by PhotoId
having count(distinct FeatureCheck) = 1
and count(distinct Result) = 1
SELECT caseid, photo_id , feature_check, agreedupon,
group_concat(checkedby SEPARATOR ',') as listusers
FROM table1
GROUP BY case_id, photo_id
Asssuming the possibility of more than 2 users checked the data. Then grouping them is more dynamic.
i have a table filled with mails messages that contains all mails from certain domains. columns are - domain_name, subject and receipt.
i want to find all the domains in my database that have sent a 'welcome' mail and a receipt through out time.
in order for a mail to be a receipt :
g.receipt != '{}'
in order for a mail to be a 'Welcome' mail:
g.subject regexp 'Welcome'
the problem is that the two conditions create a conflict. i can't search for both of them because i will have 0 results. a single mail can be a Welcome OR Receipt.
but i need to find domains who sent both through out time.
all i could do is narrowing the possibilities with this:
select
*
from
(select
if(g.receipt != '{}', #R:=1, #R:=0) As Receipt,
if(g.subject regexp 'Welcome', #M:=1, #M:=0) AS Welcome,
g.domain_name,
g.subject,
g.receipt
from
table as g
) as B
where
(#M:= 1 and #R:=0) or (#R:=1 and #M:=0)
group by domain_name, Welcome, Receipt
This code gives me all domains that sent a 'Welcome' Or a receipt. (i have other type of messages in the database). is there any way to code this so i wont need to find them with my eyes?
Thank you, i use mysql.
This should give you the results you need. Note that I've called your table domains due to table being a reserved keyword.
select distinct domain_name from domains welcome_messages
where
subject like '%Welcome%'
and exists
(
select * from domains receipts
where
receipts.receipt != '{}'
and welcome_messages.domain_name = receipts.domain_name
)
Here's a sqlfiddle of the script above.
Here's my current query I'm using
SELECT *
FROM accounts
WHERE source = 0
AND account_id NOT IN(SELECT receive FROM actions WHERE follow = '$account')
AND status = 0
ORDER BY RAND() LIMIT
I want to somehow do something like this
SELECT *
FROM accounts
WHERE source = 0
AND account_id NOT IN(SELECT * FROM actions WHERE follow = '$account')
AND COUNT((SELECT * FROM actions WHERE follow = '$account')) AS `total_received_follows`
AND total_received_follows < max_follows
AND status = 0
ORDER BY RAND() LIMIT
I basically need to get COUNT the amount of rows that have a follow value of '$account' from the actions table I then want to check this value has a lower value than the row max_follows from the accounts table
I understand that's probably not the correct syntax, but is their anyway I can do something like that using the query I have?
I'm a little confused, but here goes... In your second query, you're looking for accounts that don't have any corresponding rows in actions? But it also looks like you're trying to find accounts that have less than max_follows rows in actions. Am I right? You realise this is contradictory?
Regardless, this might be a place to start:
SELECT acc.*, COUNT(act.id) act_count FROM accounts acc
LEFT JOIN action act ON act.follow = acc.id
WHERE ...
GROUP BY acc.id
HAVING act_count < acc.max_follows
I'm trying to create a query that checks an inserted value against a field. I'm trying to accomplish this using a sub-query, but got stuck. What I'm trying to accomplish:
(message1) User1 says: Hello User2
(message2) ChatX says: User1, user2 said hello to you!
I figured that in order to accomplish that, I need a subquery within the like statement.
SELECT chat.id, chat.userid, chat.message, user.userid, user.username
FROM chat, user
WHERE LOWER(message) LIKE CONCAT('hello ', (SELECT user.username FROM user WHERE XXX = user.username))
AND chat.userid = user.userid
The XXX in the LIKE statement, is the username someone said hello to. It's there it should check against the user table and if the message matches a user output ChatX's line. My question, how do I make XXX work and how do I set it?
SELECT c.id, c.message,
sender.userid, sender.username,
receiver.userid, receiver.username
FROM chat c
JOIN user sender ON c.userid = sender.userid
JOIN user receiver ON LOWER(message) like CONCAT('hello ', receiver.username)
I have a messaging system (very basic) that has a table like this:
**MESSAGE_ID** **RUSER_ID** **SUSER_ID** **MESSAGE_DATA** **DATE**
RUSER is the receiving user, and SUSER is the sending user. If I wanted to output a query that would output a certain users messages, I would currently do:
Select * from PRIVATE_MESG where RUSER_ID=$USER_ID or SUSER_ID=$USER_ID
That would give me all message_id's that are associated with that USER_ID. What I would like, is to create a column that would produce only the ID associated with RUSER_ID or SUSER_ID associated with a specific user. I need it to choose the messages that RUSER_ID or SUSER_ID are equal to a USER_ID but only display the one that isn't USER_ID
I would then like to do a group by the output of that query.
Any help is greatly appreciated!
Thanks!
update I am not really looking for a message_id, I am just looking for a list of users who that person has written to or received from.
UPDATE
Just so everyone knows, I recieved the answer to this question perfectly! I tweaked it later on so that it would also display them by date from newest to oldest. I did this by spliting the DATETIME into DATE and TIME USING the DATE() and TIME() Function. Here was my final query:
SELECT
IF(RUSER_ID = $USER, SUSER_ID, RUSER_ID) as THE_OTHER_GUY, DATE(DATE) as DAY, TIME(DATE) as TIME
FROM PRIVATE_MESG
WHERE RUSER_ID = $USER
OR SUSER_ID = $USER;
group by THE_OTHER_GUY ORDER BY DAY DESC, TIME DESC
Hope this helps the next person!
You can query:
SELECT
*,
IF(RUSER_ID = $USER_ID, SUSER_ID, RUSER_ID) as THE_OTHER_GUY
FROM PRIVATE_MESG
WHERE RUSER_ID = $USER_ID
OR SUSER_ID = $USER_ID;
SELECT SUSER_ID FROM PRIVATE_MESG WHERE RUSER_ID=$USER_ID
UNION
SELECT RUSER_ID FROM PRIVATE_MESG WHERE SUSER_ID=$USER_ID
It retrieves:
- the list of user IDs who sent messages to $USER_ID
- the list of user IDs who received messages from $USER_ID
And UNION groups the 2 lists in a single result set.