-2

I have SQL query:

Here is code which I actually using:

SELECT m.message_id, m.message_subject, m.message_message, m.message_smileys, m.message_datestamp, u.user_id, u.user_name, u.user_avatar
            FROM ".DB_MESSAGES." m
                LEFT JOIN (
                    SELECT user_id, user_name, user_avatar 
                    FROM ".DB_USERS." 
                ) u ON m.message_from=u.user_id

            WHERE message_folder='0' AND ((message_to='".$userdata['user_id']."' AND message_from='2')
            OR (message_to='2' AND message_from='".$userdata['user_id']."'))
            ORDER BY message_datestamp

Result:

Result Image

What I need:

Needed Result Image

Database: prntscr.com/9vm1s4

Example:
me: Hello 12:10 AM
he: Hi 12:20AM
he: Have are you ? 12:22AM
me: Fine 1:10PM

Any advice?

Well thank you

divibisan
  • 11,659
  • 11
  • 40
  • 58
  • This should answer your question (possible duplicate): http://stackoverflow.com/questions/3128043/limit-results-on-sql-request – Topological Sort Jan 27 '16 at 20:31
  • 2
    A more informative reply would, well, provide more information. Help us help you. – Topological Sort Jan 28 '16 at 03:51
  • DB: [link](http://prntscr.com/9vm1s4) I'ts simple chat based on PHP Fusion. I need show mesages from me to him and from him to me ordered by time. Example: **me:** Hello 12:10 AM, **he:** Hi 12:20AM, **he:** Have are you ? 12:22AM, **me:** Fine 1:10PM – Marek Brutus Gajdoš Jan 28 '16 at 14:04
  • 1
    Welcome to SO. Your question is getting down-voted because it doesn't include enough useful detail. Please take a moment to read these two links, then consider editing your question if you're still looking for help. https://stackoverflow.com/help/how-to-ask and, regarding images, https://meta.stackoverflow.com/a/285557/5790584 – Eric Brandt Jun 29 '18 at 20:41

2 Answers2

0

Try to add GROUP BY m.message_from before the ORDER BY part

Mahmoud Tantawy
  • 713
  • 5
  • 12
0

PHP Fusion add message to DB duplicated.

Here is the code which I am actually using:

SELECT m.message_id, m.message_subject, m.message_message, m.message_smileys, m.message_datestamp, u.user_id, u.user_name, u.user_avatar
            FROM ".DB_MESSAGES." m
                LEFT JOIN (
                    SELECT user_id, user_name, user_avatar 
                    FROM ".DB_USERS." 
                ) u ON m.message_from=u.user_id

            WHERE message_folder='0' AND ((message_to='".$userdata['user_id']."' AND message_from='2')
            OR (message_to='2' AND message_from='".$userdata['user_id']."'))
            ORDER BY message_datestamp
Jason Aller
  • 3,541
  • 28
  • 38
  • 38