Note that there are some explanatory texts on larger screens.

plurals
  1. POprovide extra return column of boolean type which evaluates another query
    primarykey
    data
    text
    <p>I am trying to develop a query that will </p> <ol> <li>Give the last message in every thread that was not written by the currentUserID (right now accomplished with group by thread_id, order by message_id desc AND WHERE results.sender_id != currentUserID)</li> <li>If the user started a thread but received no response then the thread should not show up (right now accomplished by simply filtering the data with WHERE results.sender_id != currentUserID)</li> <li>Provide a boolean column to tell if a response was sent in this thread (not accomplished but would involve finding the last element from the query and seeing if the sender_id is = to current logged in user id)</li> </ol> <p><a href="http://sqlfiddle.com/#!2/85971/2" rel="nofollow">http://sqlfiddle.com/#!2/85971/2</a></p> <p><strong>Synopsis</strong></p> <ul> <li>User 1 sends a message with User 2 with</li> <li>Thread ID: 1</li> <li>Subject: 'Hi Subject'</li> <li><p>Body: 'Hi Body'</p></li> <li><p>User 2 responds:</p></li> <li>Thread ID: 1</li> <li>Body: 'Hi Body Response'</li> </ul> <p><strong>As User 1 the SQL Should Return</strong></p> <pre><code>THREAD_ID |SUBJECT | SENDER_ID | BODY | Responded 1 | Hi Subject | 2 | Hi Body Response | false </code></pre> <p><strong>As User 2 the SQL Should Return</strong></p> <pre><code>THREAD_ID |SUBJECT | SENDER_ID | BODY | Responded 1 | Hi Subject | 1 | Hi Body | true </code></pre> <p>Please see fiddle</p> <p>My shot at it was the following which takes acre of 1 and 2 but not 3, I tried to mess around with UNIONS to achieve this but got nowhere.</p> <pre><code>SELECT * FROM ( SELECT t.id as thread_id, t.subject, m.sender_id, m.body FROM messages m JOIN threads t ON (m.thread_id = t.id) ORDER BY t.id asc, m.cdate desc ) as results WHERE results.sender_id != currentUserID GROUP BY results.thread_id; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload