SQL Fiddle
http://sqlfiddle.com/#!2/1c5fc3/1
I am trying to create simple messaging system, but I am having trouble with the desired results from the SQL queries.
Here are the tables I have; I am trying to get INBOX data..
INBOX Definiton for this problem:
This should be threaded display in inbox, ie. google mail, but only to show the last message in that thread with the user who originaly created the thread and the last user who replied in the thread, if the last user is the same user that created the thread and there are no replies in beetween the message doesnt belog in inbox.
TABLES:
THREAD
id_thread
id_last_message
id_user_inital
id_user_last
THREAD_USERS
id
id_thread
id_user
THREAD_MESSAGES
id_thread_messages
id_user_sender
id_thread
datetime
subject
body
MESSAGE_STATUS
id_messsage_status
id_thread_messages
id_user
status
datetime
My logic is:
once a message has been sent
THREAD
id_thread id_last_message id_user_inital id_user_last
1 1 1 1
THREAD_USERS
id id_thread id_user
1 1 1
2 1 2
THEREAD_MESSAGES
id_thread_messages id_user_sender id_thread datetime subject body
1 1 1 07.09.2014 16:02 'title' 'text message'
MESSAGE_STATUS
id_message_status id_thread_messages id_user status datetime
1 1 1 4 07.09.2014 16:02
2 1 2 1 07.09.2014 16:02
Lets say status can be
0 = deleted (do not show at all)
1 = new (show only to user that is on the receiving end)
2 = read (this status will be shown to all users in the thread)
3 = replied (show only to user that makes this action)
4 = sent (show only to user that makes this action)
Query :
SELECT *
FROM thread
JOIN thread_users
ON thread.id_thread = thread_users.id_thread
JOIN thread_messages
ON thread.id_thread = thread_messages.id_thread
JOIN message_status
ON thread_messages.id_thread_messages = message_status.id_thread_messages
WHERE
thread_users.id_user = 2
AND message_status.status != 0
AND message_status.status != 4
AND thread.id_user_last != message_status.id_user
sample data
THREAD
id_thread id_last_message id_user_inital id_user_last
1 4 1 2
2 2 3 3
3 3 4 4
THREAD_USERS
id id_thread id_user
1 1 1
2 1 2
3 2 3
4 2 2
5 3 4
6 3 2
THEREAD_MESSAGES
id_thread_messages id_user_sender id_thread datetime subject body
1 1 1 07.09.2014 16:02 'title' 'text message'
2 3 2 07.09.2014 16:05 'hey two' 'foo'
3 4 2 07.09.2014 16:07 'hey two' 'bar'
4 2 1 07.09.2014 16:10 'title' 'replay on 1st'
MESSAGE_STATUS
id_message_status id_thread_messages id_user status datetime
1 1 1 4 07.09.2014 16:02
2 1 2 1 07.09.2014 16:02
3 2 3 4 07.09.2014 16:05
4 2 2 1 07.09.2014 16:05
5 3 4 4 07.09.2014 16:07
6 3 2 1 07.09.2014 16:07
7 4 2 4 07.09.2014 16:10
8 4 1 1 07.09.2014 16:10
How would you extract INBOX data from this situation, as I am spinning in circles for hours and can't quite get what I am doing wrong.
Thank you.
Updated solution after taking into account explanations for message status:
SELECT DISTINCT t.*, tm.* , ms.*
FROM thread t
-- tm should be last message
INNER JOIN thread_messages tm ON t.id_thread = tm.id_thread
INNER JOIN message_status ms ON (ms.id_thread_messages = tm.id_thread_messages)AND
(ms.id_user=2)AND
(ms.status!=0)
-- try to find message after tm, and then in WHERE filter only those cases where there is no message after tm
LEFT JOIN thread_messages tm_next
INNER JOIN message_status ms_next ON (ms_next.id_thread_messages = tm_next.id_thread_messages)AND
(ms_next.id_user=2)AND
(ms_next.status!=0)
ON (t.id_thread = tm_next.id_thread)and
(tm_next.datetime>tm.datetime)
LEFT JOIN thread_messages tm_other
INNER JOIN message_status ms_other ON (ms_other.id_thread_messages = tm_other.id_thread_messages)AND
(ms_other.id_user=2)AND
(ms_other.status!=0)
ON (t.id_thread = tm_other.id_thread)and
(tm_other.id_thread_messages!=tm.id_thread_messages)and
(tm_other.id_user_sender!=2)
WHERE
-- ensure tm is last message in thread
(tm_next.id_thread is null)and
(
-- there is a non deleted message from another user in current thread
(tm_other.id_thread_messages is not null)or
-- last message is not from current user
(tm.id_user_sender!=2)
)
SqlFiddle is here.
Let me know is this working for you.
i think this is the solution that you are looking for
SELECT * FROM thread
JOIN thread_users ON thread.id_thread = thread_users.id_thread
JOIN thread_messages ON thread.id_thread = thread_messages.id_thread
JOIN message_status ON thread_messages.id_thread_messages = message_status.id_thread_messages
WHERE thread_users.id_user = 2
AND thread_users.id_user = message_status.id_user
AND message_status.status != 0
AND message_status.status != 4
AND thread.id_user_last != message_status.id_user
Now with the description of what INBOX means I suggest you rely heavily on EXISTS clause in your query. Here is just an example of how it can look like:
SELECT *
FROM thread t INNER JOIN
thread_messages tm ON t.id_thread = tm.id_thread
WHERE
EXISTS ( -- User is in the thread.
SELECT * FROM thread_users tu
WHERE t.id_thread = tu.id_thread AND tu.id_user = 2
)
AND NOT EXISTS ( -- Exclude earlier messages for thread.
SELECT * FROM thread_messages WHERE
tm.id_thread = id_thread AND datetime > tm.datetime
AND EXISTS (-- Exclude deleted messages here
SELECT * FROM message_status
WHERE thread_messages.id_thread_messages = id_thread_messages
AND status != 0
AND status != 4
)
)
AND EXISTS ( -- Include messages that were not deleted or send to self.
SELECT * FROM message_status
WHERE tm.id_thread_messages = id_thread_messages
AND status != 0
AND status != 4
AND t.id_user_last != id_user -- not sure what is this for
)
AND EXISTS ( -- Include threads with messages from several users
SELECT * FROM thread_messages
WHERE tm.id_thread = id_thread
AND tm.id_user_sender != t.id_user_inital)
http://sqlfiddle.com/#!2/1c5fc3/39
Related
job table
1.id
2.status = 'active'
3.name
repair table
1.repair id
2.job_id
3.process = 'yes|no'
4.status = '1|2'
job table
id name status
1 test active
2 check active
repair table
repair_id job_id process status
1 1 no 2
2 1 no 1
3 1 yes 2
4 2 no 1
5 2 no 2
here i need to show data which ( process != 'yes' and repair_status != 2 ) group by job_id
i need result after query
---------------------------------------------
job_id name( job.name ) status( job.status )
------------------------------------------------
2 check active
In order to get the results that you specify, you mean that process is not yes for any row for the job_id. And then that at least one row has a status <> 2. That would be:
select j.job_id, j.name, j.status
from repair r join
job j
on r.job_id = r.id
group by j.job_id, j.name, j.status
having max(process) = 'no' and
min(repair_status) = 1;
If you want jobs for which no repair exists with process = 'yes' and status = 2, you can use not exists:
select j.*
from jobs j
where not exists (
select 1
from repair r
where r.job_id = j.id and r.process = 'yes' and r.status = 2
)
I'm trying to run an UPDATE query that uses the same table and I'm getting an error saying "1093 - Table 'queues_monitor_times' is specified twice, both as a target for 'UPDATE' and as a separate source for data".
UPDATE queues_monitor_times
SET queue_id = IF((
SELECT id
FROM queues_monitor_times
INNER JOIN(
SELECT pcc_group, pcc, gds, queue, category, `name`
FROM queues_monitor_times
GROUP BY pcc_group, pcc, gds, queue, category, `name`
HAVING COUNT(id) > 1
)temp ON queues_monitor_times.pcc_group = temp.pcc_group AND
queues_monitor_times.pcc = temp.pcc AND
queues_monitor_times.gds = temp.gds AND
queues_monitor_times.queue = temp.queue AND
queues_monitor_times.category = temp.category AND
queues_monitor_times.`name` = temp.`name`), 1, id)
WHERE
id NOT IN (SELECT MIN(id) FROM queues_old GROUP BY pcc_group, pcc, gds, queue, category, `name`);
I ran the select query by itself and it showed all the rows that were duplicates, which is what I wanted. I want queue_id to be set with the lowest duplicate row's id if the row is a duplicate or the row id if it is not.
Example of what the query should do:
id dup_id name value
1 1 John 13
2 2 John 13
3 3 Sally 6
4 4 Frank 4
5 5 Sally 6
And after running the query it will turn into
id dup_id name value
1 1 John 13
2 1 John 13
3 3 Sally 6
4 4 Frank 4
5 3 Sally 6
Please advise and thank you for your help.
I was able to solve my problem. Thanks for all your help!
UPDATE queues_monitor_times
SET queue_id = (
SELECT
id
FROM
queues_old
WHERE
queues_old.pcc_group = queues_monitor_times.pcc_group
AND queues_old.pcc = queues_monitor_times.pcc
AND queues_old.gds = queues_monitor_times.gds
AND queues_old.queue = queues_monitor_times.queue
AND queues_old.category = queues_monitor_times.category
AND queues_old.`name` = queues_monitor_times.`name`
GROUP BY pcc_group, pcc, gds, queue, category, `name`
HAVING COUNT(id) > 1)
WHERE
id NOT IN (SELECT MIN(id) FROM queues_old GROUP BY pcc_group, pcc, gds, queue, category, `name`);
For those that will want to use this in the future, queues_monitor_times table and queues_old table have the exact same data.
Im trying to make this generic as it might help others in the future.
For an example i have two tables one with books and the other is the user with which book they have read, So ide like to display all the books and include a temporary column value as a (yes / no or 0/1), i have tried a join but the ( WHERE user_id = 3) clause only then return the one row and not all the other rows.
book.book_id book.book_name
10 Book 1
11 Book 2
12 Book 3
-------------
user.user_id user.book_id
1 10
1 12
2 11
3 12
Desired output:
user_id book_id temp_col_read
3 10 0 // yes, on or null
3 12 1 // or yes
3 13 0
This is actually quite simple. In the event that a user could read a book multiple times, I would go with exists in the select:
select b.*,
(case when exists (select 1
from reads r
where r.book_id = b.book_id and r.user_id = 3
)
then 1 else 0
end) as user_read_book
from book b;
In MySQL, the case is not strictly necessary because a boolean expression is treated as 0/1 in many contexts:
select b.*,
(exists (select 1
from reads r
where r.book_id = b.book_id and r.user_id = 3
) as user_read_book
from book b;
You can use a left join and where the join is unresolved then is not read
select
user.user_id
, book.book_id
, case
when book.book_id is null
then 'NO' else 'YES'
end as temp_col_read
from book
left join user on user.book_id = book.book_id
I am creating a small chat/ message system. I have four tables: thread, thread_participant, message and message_read_state. I know it might not be the best solution, but I just wanted to give this a go for a project of mine. How can i select all new messages coming in for a specific user without selecting the his/her own message?
thread table
id
----------
1
2
thread_participant table
thread_id user_id
---------- --------
1 6
1 7
message table
id thread_id send_date message sending_user_id
---------- -------- -------- -------- --------
1 1 2016-05-12 HELLO 7
2 1 2016-05-12 HELLO BACK 6
message_read_state
message_id user_id s read_date
----- -------- --------
1 6 2016-05-12
2 7 2016-05-12
EDIT
What I have so far. This select all the threads I have participated, but I would like to display only the incoming message, without my response.
SELECT message.id, message.send_date, message.message, tbl_users.user_id, message.thread_id
, (SELECT message_read_state.read_date
FROM message_read_state
WHERE message_read_state.message_id = message.id
and message_read_state.user_id = 6) AS ReadState
FROM message INNER JOIN tbl_users ON message.sending_user_id = tbl_users.user_id
WHERE ( message.id in
( SELECT (message.id)
FROM thread_participant INNER JOIN message
ON thread_participant.thread_id = message.thread_id
WHERE thread_participant.user_id = 6
GROUP BY thread_participant.user_id
)
)
ORDER BY message.send_date ASC;
What about this;)
SELECT message.id, message.send_date, message.message, tbl_users.user_id, message.thread_id
, (SELECT message_read_state.read_date
FROM message_read_state
WHERE message_read_state.message_id = message.id
and message_read_state.user_id = 6) AS ReadState
FROM message INNER JOIN tbl_users ON message.sending_user_id = tbl_users.user_id
WHERE ( message.id in
( SELECT (message.id)
FROM thread_participant INNER JOIN message
ON thread_participant.thread_id = message.thread_id
WHERE thread_participant.user_id = 6
GROUP BY thread_participant.user_id
)
)
AND message.sending_user_id <> 6
ORDER BY message.send_date ASC;
I've just add AND message.sending_user_id <> 6 in your original sql. If you don't want response, just let sending_user_id is not you, then you get what you want. :D
Consider the following tables
users tweets
-------------------- -----------------------------
user_id neu pos neg id _date user_id class
-------------------- -----------------------------
1 1 2011-02-13 1 1
2 2 2011-02-13 1 2
3 2011-02-15 2 1
4 2011-02-16 2 3
5 2011-02-16 2 3
Each tweet in the database has been analyzed and classified. tweets.class can be either 1 (neutral), 2 (positive) or 3 (negative). I need a query to count all neu (neutral, pos (positive) and neg (negative) tweets per user. The end result of the query would be and UPDATEd users-table like so:
users
--------------------
user_id neu pos neg
--------------------
1 1 1 0
2 1 0 3
I now have but this doesn't work at all. Am a going into the right direction?
update users u left join (
select count(t.id) as neu from tweets t where class = 1
) on u.user_id = t.user_id
set u.neu = coalesce(neu,0)
this should work:
UPDATE users a
INNER JOIN (SELECT user_id,
SUM(IF(class = 1, 1, 0)) AS class_1,
SUM(IF(class = 2, 1, 0)) AS class_2,
SUM(IF(class = 3, 1, 0)) AS class_3
FROM tweets
GROUP BY user_id
) b
ON a.user_id = b.user_id
SET a.neu = b.class_1,
a.pos = b.class_2,
a.neg = b.class_3;