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 8670689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:46:02+00:00 2026-06-12T18:46:02+00:00

I have a MYSQL ‘activity’ table where all activity by a user gets logged.

  • 0

I have a MYSQL ‘activity’ table where all activity by a user gets logged. Let’s say that a user’s activities can be either to ask a question or answer a question. So if a user asks a question, their question gets put into the ‘question’ table and also the activity gets put into the ‘activity’ table. Same thing if user provides an answer, except that the answer goes into the ‘answers’ table and the ‘activity’ table.

The question table’s fields are:

q_id          question

The answers table’s fields are:

a_id          q_id        answer

The activity table’s fields are:

activity_id   q_id    user_id     action_type

(There are more columns then shown above, but these illustrate the point.)

What I want to be able to do is show a user’s recent actions on the site, and actually show what the actions were.

My current MYSQL query simply joins the activity table with the question table as follows (using PDO):

"SELECT * 
FROM activity 
LEFT JOIN question ON activity.q_id = question.q_id 
WHERE activity.user_id = :user_id 
ORDER BY activity.a_id desc"  

This works okay, and can always give me the question for display in the activities list. However, if the activity that is returned is an answer, I would like to be able to show the answer as well. I tried just using a triple join as follows:

"SELECT * 
FROM activity 
LEFT JOIN question ON activity.q_id = question.q_id
LEFT JOIN answers ON activity.q_id = answers.q_id  
WHERE activity.user_id = :user_id 
ORDER BY activity.a_id desc"

But this just gives me a list of rows that is twice as long as the first query, and does not display the answers when the action_type was an answer.

I figure this should be possible, but the more I think about it I believe that maybe I should scrap the activity table and just perform the query directly on the question and answers tables with a UNION? Any opinions on this decision, and any thoughts on the queries?

  • 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-06-12T18:46:03+00:00Added an answer on June 12, 2026 at 6:46 pm

    You could use an outer join, which would return everything in the activity table, even if there isn’t a corresponding row in the answer table:

    SELECT *
    FROM activity
    LEFT JOIN question ON activity.q_id = question.q_id
    LEFT OUTER JOIN answers ON activity.q_id = answers.q_id
    WHERE activity.user_id = :user_id
    ORDER BY activity.a_id desc
    

    I’m assuming that for every q_id value in the activity table, there’s always going to be one corresponding row in the question table.

    To fix the problem with the duplicate answers, I’d add an a_id field to the activity table. For the question activity, you’d set it to NULL, and for each answer you’d set the a_id field to the appropriate a_id value to identify which answer goes with the activity. You’d then adjust the SQL above as follows:

    SELECT *
    FROM activity
    LEFT JOIN question ON 
        activity.q_id = question.q_id
    LEFT OUTER JOIN answers ON 
        activity.q_id = answers.q_id AND 
        activity.a_id = answers.a_id
    WHERE activity.user_id = :user_id
    ORDER BY activity.a_id desc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have MySQL table that contains all messages from user to user: id |
I have MySQL running such that I can open a client command line and
I have mysql table that has a column that stores xml as a string.
I have a MySQL server with many databases that have a similar table structure.
i have MySql Table name 'category', in that table i have id catname parent_id
I have MySQL table that doesn't have AUTO_INCREMENT field. In PHP I run this
I have mysql table tmp with columns pid,city,state,country. I write queries so i can
I have MySQL tables looking like that regions table id | region ------------------- 1
I have MySQL query that selects user names and theirs knowledge (may be more
I have mysql table with collumns like 'operation.date', 'operation.name' and etc. After fetching that

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.