Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3214004
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:00:35+00:00 2026-05-17T15:00:35+00:00

I have two tables: messages (messages users posted), likes (many to many relationship between

  • 0

I have two tables: messages (messages users posted), likes (many to many relationship between users and messages – it says that user1 likes message5).

messages
---------
id, id_user, message, created_at

likes
-----
id_user, id_message, created_at

If I send message, it goes to the messages table. If I like somebody’s message, a new record will be created in likes table (is_user=me, id_message=message that I like).

The problem is, I would like to show history of my actions = messages and likes together in one list ordered by “created_at”.

Something like:

- 1/1/2010 i sent message "aaa"
- 2/1/2010 i sent message "bbb"
- 3/1/2010 i liked somebodys's message "ccc"
- 4/1/2010 i send message "ddd"

EDIT
And what’s more, I want to also show details of the status I liked:

- 3/1/2010 i liked somebodys's message **"ccc"**

How to do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:00:35+00:00Added an answer on May 17, 2026 at 3:00 pm

    It would be a good idea to create a new table called ‘actions’ where adding a message or liking a message are recroded in this table, then it is simple to get what you want by querying this table.

    If you can’t do that then you use your existing schema by UNIONing the data from each table and querying the resulting table but it won’t be as efficient as an index on created_at cannot be used:

    SELECT * FROM
    (
        SELECT 1 AS type, id, id_user, message, created_at FROM messages
        UNION ALL
        SELECT 2 AS type, NULL, id_user, id_message, created_at FROM likes
    ) T1
    ORDER BY created_at
    

    Add joins to get related information about the message you liked such as the username of the sender of the message and the text of the message. The task of converting this information into strings is usually best done at the application level to reduce unnecessary data transfer between your database server and your application.

    If you really want to do it in the database then you can use CONCAT to build the strings:

    SELECT messagetext
    FROM
    (
        SELECT
            created_at,
            CONCAT('I sent message "', message, '"') AS messagetext
        FROM messages
        WHERE id_user = 1
        UNION ALL
        SELECT
            likes.created_at,
            CONCAT('I liked ', messages.id_user ,'\'s message "', messages.message, '"')
        FROM likes
        JOIN messages
        ON likes.id_message = messages.id
        WHERE likes.id_user = 1
    ) T1
    ORDER BY created_at
    

    Result:

    I sent message "aaa"
    I sent message "bbb"
    I liked 2's message "ccc"
    I sent message "ddd"
    

    Note that the user_id is shown instead of the user name. If you want the user name then you will also need to join to the user table (this table was not shown in your question, but I assume that you have one).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table called Comments that stores messages between two users, and the
I have to tables:users and messages.And create the one-many relations between these tables.And,If I
I have two tables Users and messages: How i can make select full the
Here's the situation. I have two tables: users (registered users of the website), messages
I have two tables pertaining to this question: conversations has many messages . The
I have a usermessages table in which all messages sent between two users on
I have two tables called messages and users . In the messages table, there's
I have two tables: messages - with following structure id sender_id receiver_id message_text users
I have two MySQL tables: stats (left) and messages (right) +------------+---------+ +---------+------------+-----------+----------+ | _date
I have two tables that I join on the id-column, they look like: +-------+

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.