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

  • Home
  • SEARCH
  • 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 127315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:27:25+00:00 2026-05-11T05:27:25+00:00

I have 3 MySQL tables representing: photos a user post, videos a user post,

  • 0

I have 3 MySQL tables representing: photos a user post, videos a user post, comments a user post and I need to view the 10 (20, 30, 40…) most recent activities by the user.

For example in photos table may be composed by:

user_id | photo_id   | photo_path | photo_name | date_added    5    |     18     |  /photos   | pht_18.png |  2009-02-12    5    |     21     |  /photos   | pht_21.png |  2009-02-15    5    |     29     |  /photos   | pht_29.png |  2009-03-30  

the videos table

user_id | video_id   |  video_url       |   date_added    5    |     36     |  youtube.com/... |   2009-01-09    5    |     48     |  youtube.com/... |   2009-02-18    5    |     90     |  youtube.com/... |   2009-03-19 

the comments table

user_id | comment_id |   comment     |  date_added    5    |     6      |   hi!         |  2009-02-11    5    |     11     |   great photo |  2009-02-13    5    |     19     |   nice shot!  |  2009-03-28 

As you can see the 3 tables have different number of attributes, so how can I do the union? and while fetching the query result how can I understand to which table it belongs to?

So in the user profile page I’d like to show his recent activities of course ordered by DATE DESC this way:

2009-09-01: user posted a video 2009-11-02: user posted a comment 2009-12-02: user posted a photo 2009-13-02: user posted a comment 2009-15-02: user posted a photo 2009-18-02: user posted a video 2009-19-03: user posted a video 2009-28-03: user posted a comment 2009-30-03: user posted a photo 

Can anyone help me please?

  • 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. 2026-05-11T05:27:26+00:00Added an answer on May 11, 2026 at 5:27 am

    A MySQL UNION query could work here:

    (SELECT `user_id`, `date_added`, 'photo' AS `type` FROM `photos` WHERE `user_id` = uid) UNION  (SELECT `user_id`, `date_added`, 'video' AS `type` FROM `videos` WHERE `user_id` = uid) UNION  (SELECT `user_id`, `date_added`, 'comment' AS `type` FROM `comments` WHERE `user_id` = uid)  ORDER BY `date_added` DESC; 

    Then you’d wind up with a result set like

    user_id | date_added | type     5   | 2009-01-03 | photo     5   | 2008-12-07 | video     5   | 2008-11-19 | comment 

    and so on. (actually you can leave user_id out of the SELECT if you want, of course)

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

Sidebar

Related Questions

I have a MySql database with three tables I need to combine in a
I have two mysql tables: USER and ROSTER. Previously, I've been able to query
I have two mysql tables, courses and student_courses, I need to get unique course_names
I have 2 mysql tables one has a list of user ids that are
I have MYSQL tables as follows user TABLE {id INT} profile TABLE {user_id INT,
I have two MySQL tables: User (with an auto-incremented pk id) and WaitingUser which
i have two mysql tables which are linked together and i need to show
I have large MySQL tables with hundreds of thousands of rows. I need to
I have two mysql tables, one containing details on cars and one containing all
i have two mysql tables tableA colA1 colA2 1 whatever 2 whatever 3 whatever

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.