tags:

views:

96

answers:

2

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  A: 

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).

Mark Byers
Thanks Mark. I don't need the whole strings, that's the application's responsibility. I need just the text of the message I liked and that could be done very easily using joins.
PetrB
I decided to go the way which involves creating the new 'actions' table but I'm stuck with the database schema. As for now, the actions table has columns action_id, created_at, discriminator. What else shall I put in there and how to query it?
PetrB
@PetrB: That looks like a good start. You don't need to duplicate the data from the other tables in this table.
Mark Byers
Do i need to add some columns to messges, likes? How do I get the whole history using this schema?
PetrB
@PetrB: I just realized that you should add user_id to the actions table too, so that you can filter by this field and sort using a single index.
Mark Byers
To query: `SELECT * FROM actions LEFT JOIN messages ON ... LEFT JOIN likes ON ... WHERE user_id = 1 ORDER BY created_at`.
Mark Byers
That's exactly the query I eventualy built but can't find out how to show "the info about the message I like" using this query :-(
PetrB
A: 
SELECT ....
FROM
(
    SELECT MSGS.
       ......
       MSGS.created_at
    FROM MSGS
    WHERE MSG.id_user = me
  UNION
    SELECT LIKES. 
       ..........
       LIKES.created_at
    FROM MSGS, LIKES
    WHERE MSGS.id_message = LIKES.id_message
      AND MSGS.id_user = me
  ) A
ORDER BY  A.created_at
pinichi