Note that there are some explanatory texts on larger screens.

plurals
  1. POOrder data within group by that has 2 different columns in MySQL
    primarykey
    data
    text
    <p>I am working with a discussion board where there will be a certain topic and the user can comment or upvote another comment. Each comment can also be replied by another user and will recieve notifications.</p> <h3>My notification table</h3> <ul> <li><code>notification_id</code> is the user_id of the logged in user that will receive the notification</li> <li><code>notification_from</code> is the user_id of the one who commented or like the users post.</li> <li><code>notification_topic</code> is the id of the topic plus the type of notification that will distinguish if it is a comment or an upvote</li> <li><code>notification_comment</code> is the comment_id in the topic</li> </ul> <pre> +-----------------+-------------------------+-------------------+--------------------------------------+----------------------+-------------------+-------------------+--------------------+----------------------+---------+--------+ | notification_id | notification_pic | notification_name | notification_title | notification_user_id | notification_date | notification_from | notification_topic | notification_comment | type | status | +-----------------+-------------------------+-------------------+--------------------------------------+----------------------+-------------------+-------------------+--------------------+----------------------+---------+--------+ | 1 | 32_1380182434_thumb.jpg | Sheena Salazar | Chrysler files papers for share sale | 2 | 1380188338 | 32 | 83_upvote | 1 | upvote | read | | 2 | 32_1380182434_thumb.jpg | Sheena Salazar | Chrysler files papers for share sale | 2 | 1380188342 | 32 | 83_comment | 1 | comment | read | | 3 | 93_1379990163_thumb.jpg | vhon samson | Chrysler files papers for share sale | 2 | 1380188505 | 93 | 83_upvote | 1 | upvote | read | | 4 | 93_1379990163_thumb.jpg | vhon samson | Chrysler files papers for share sale | 2 | 1380188509 | 93 | 83_comment | 1 | comment | read | | 5 | 93_1379990163_thumb.jpg | vhon samson | Chrysler files papers for share sale | 0 | 1380246975 | 93 | 83_comment | 1 | comment | unread | | 6 | 93_1379990163_thumb.jpg | vhon samson | Toyota and Nissan in vehicle recall | 2 | 1380247149 | 93 | 225_comment | 3 | comment | read | | 7 | default.gif | kath aguilar | Chrysler files papers for share sale | 2 | 1380253584 | 7 | 83_comment | 1 | comment | read | | 8 | default.gif | kath aguilar | Chrysler files papers for share sale | 93 | 1380253870 | 7 | 83_comment | 2 | comment | unread | +-----------------+-------------------------+-------------------+--------------------------------------+----------------------+-------------------+-------------------+--------------------+----------------------+---------+--------+ </pre> <h3>My query</h3> <p>This is how I query and group my table:</p> <pre class="lang-sql prettyprint-override"><code>SELECT *, COUNT(notification_topic) AS topic_count FROM tbl_notification WHERE notification_user_id = '{$_SESSION['id']}' GROUP BY notification_topic ORDER BY notification_date DESC LIMIT 8 </code></pre> <p>This is the result of the query:</p> <pre> +-----------------+-------------------------+-------------------+--------------------------------------+----------------------+-------------------+-------------------+--------------------+----------------------+---------+--------+-------------+ | notification_id | notification_pic | notification_name | notification_title | notification_user_id | notification_date | notification_from | notification_topic | notification_comment | type | status | topic_count | +-----------------+-------------------------+-------------------+--------------------------------------+----------------------+-------------------+-------------------+--------------------+----------------------+---------+--------+-------------+ | 6 | 93_1379990163_thumb.jpg | vhon samson | Toyota and Nissan in vehicle recall | 2 | 1380247149 | 93 | 225_comment | 3 | comment | read | 1 | | 2 | 32_1380182434_thumb.jpg | Sheena Salazar | Chrysler files papers for share sale | 2 | 1380188342 | 32 | 83_comment | 1 | comment | read | 3 | | 1 | 32_1380182434_thumb.jpg | Sheena Salazar | Chrysler files papers for share sale | 2 | 1380188338 | 32 | 83_upvote | 1 | upvote | read | 2 | +-----------------+-------------------------+-------------------+--------------------------------------+----------------------+-------------------+-------------------+--------------------+----------------------+---------+--------+-------------+ </pre> <p>See it on <a href="http://sqlfiddle.com/#!2/aad545/1/0" rel="nofollow noreferrer">sqlfiddle</a>.</p> <h3>What I need</h3> <p>I need to get the count so that I can come up with a notification like on facebook, like this:</p> <p><img src="https://i.stack.imgur.com/oFwf6.jpg" alt="enter image description here"></p> <p>My main problem is I can't <code>ORDER</code> it by <code>notification_date</code> before the <code>GROUP BY</code> because I need to isolate each <code>notification_comment</code> by its <code>notification_topic</code>. I want the latest user that commented or like each topic to be displayed. How will I do this?</p>
    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