Question

I have a user table, which contains the user's complete name. The linked tables are:

user

  • id_user unique user ID
  • name user complete name

user_picture

  • id_picture
  • date_added
  • id_user id of the user
  • url url of picture

user_visit

  • id_visit
  • date_visit
  • id_user_from this is you
  • id_user_to this is the visited user

user_question

  • id_question
  • date_question
  • id_user_from this is you
  • id_user_to this is the visited user
  • question this is not relevant
  • answer this is not relevant
  • flg_answered

The activity report must display the activity of:

  • users' uploaded pictures
  • users' answered questions (sent by any user)

...but only from users that I have previously visited, or users that have previously received questions from me.

This is my current query:

    SELECT SQL_CALC_FOUND_ROWS
        user.name,
        user_question.date_question,
        user_question.question,
        user_question.answer,
        user_picture.date_picture,
        user_picture.description
    FROM
        user
        LEFT JOIN user_question ON user_question.id_user_to = user.id_user
        LEFT JOIN user_picture ON user_picture.id_user = user.id_user
    WHERE (
            user.id_user IN (
                SELECT id_user FROM user
                INNER JOIN user_visit ON user_visit.id_user_to = user.id_user
                INNER JOIN user_question ON user_question.id_user_to = user.id_user
                WHERE
                    user_visit.id_user_from = 1
                    OR
                    user_question.id_user_from = 1

            )
    )

In this example, ID 1 is my user ID. Current issues:

  • The order of the results should be chronologically presented (date_visit and date_question)
  • The picture and question records are being blended into the same rows. They should be mutually exclusive.

Any ideas on how to fix those 2 issues? Thanks!

Was it helpful?

Solution

it looks like you're trying to do two queries, and then intersperse their results by date order... you probably need to UNION. then use a subselect in order to ORDER the results.

SELECT SQL_CALC_FOUND_ROWS, *
FROM (

SELECT 
        user.name,
        'picture' as type,
        user_visit.date_visit as date,
        user_picture.date_picture as data1,
        user_picture.description as data2
    FROM
        user
        JOIN user_visit ON (user_visit.id_user_to = user.id_user)
        JOIN user_picture ON (user_picture.id_user_to = user.id_user)
    WHERE user_visit.id_user_from = 1

UNION           

SELECT 
        user.name,  
        'question' as type,
        user_question.date_question as date,
        user_question.question as data1,
        user_question.answer as data2,
    FROM    
        user
        JOIN user_question ON user_question.id_user_to = user.id_user
    WHERE user_question.id_user_from = 1
)

ORDER BY date;

OTHER TIPS

To solve the first problem, I think you need to join the user_visit table as well, and ORDER BY the date that way.

    SELECT SQL_CALC_FOUND_ROWS
        user.name,
        user_question.date_question,
        user_question.question,
        user_question.answer,
        user_picture.date_picture,
        user_picture.description,
        user_visit.date_visit
    FROM
        user
        LEFT JOIN user_question ON user_question.id_user_to = user.id_user
            LEFT JOIN user_picture ON user_picture.id_user = user.id_user
            LEFT JOIN user_visit ON user_visit.id_user_from = user.id_user
    WHERE (
            user.id_user IN (
                SELECT id_user FROM user
                INNER JOIN user_visit ON user_visit.id_user_to = user.id_user
                INNER JOIN user_question ON user_question.id_user_to = user.id_user
                WHERE
                    user_visit.id_user_from = 1
                    OR
                    user_question.id_user_from = 1

            )
    )
    ORDER BY user_visit.date_visit, user_question.date_question

Is that a possibility? Can you please elaborate on your second problem as well? Maybe provide an example?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top