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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:47:41+00:00 2026-06-01T02:47:41+00:00

Stack: I’m trying to turn my website’s user profile into more of a feed

  • 0

Stack:

I’m trying to turn my website’s user profile into more of a feed style page. Currently the profile shows some user stats at the top, then the last 10 comments a user submitted (ordered by timestamp descending), then the last 10 posts they have submitted.

What I want to do instead, is have the last 20 “actions” (either comment or post submission) listed in order of the timestamp (so the comments and submissions will be merged together in the list instead of having 2 seperate lists). You know, like a “feed.”

The issue is that the comments are pulled from a comments table, and the submissions are pulled from a “submissions” table.

I’ve solved it in a pretty inefficient way by using a union query to select “comment_id/submission_id”, “field the identifies record as a comment or a submission”, “timestamp” from both tables.

This gives me a result that tells my the entity id, as well as defines the entity as a comment or a post, by which I can then shoot off another query in a while mysql_fetch_array statement to get the full comment or submission data.

This just seems really dirty to me, since I’m basically querying the tables, finding the rows/records that I need (but ignoring the actual data I need since the different table’s columns don’t match up as I believe to be necessary for a union query), then going back for the data I ignored the first time with individual queries in a while statement…

Is there a better way to do this that I don’t know of?

Additional notes:


Example sql I’m currently using to build the initial result I spoke of above:

select comment_id, datatype, timestamp from comments where userid=3
union all
select content_id, datatype, timestamp from submissions where userid=3
ORDER BY timestamp DESC

Returns a result like this:

commentid  datatype      timestamp 
5201       post          2012-03-27 20:30:40
43761      comment       2012-03-26 21:00:19
43759      comment       2012-03-26 20:59:47
5033       post          2012-03-26 20:57:36
43755      comment       2012-03-26 20:54:57
43745      comment       2012-03-26 16:32:24

Pseudocode I can then use to print out the information onto the profile page:

while ($content_array = mysql_fetch_array($abovequery)){

Individual select query fetching the full row by from either comment table or submission table by id depending on $content_array[‘datatype’];

echo out relevant data from individual select query onto profile screen in a pretty way;

}

Surely this can be done better?

  • 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-01T02:47:43+00:00Added an answer on June 1, 2026 at 2:47 am

    If you are unable to coerce the columns of the two full queries in such a way that it can all be returned in the UNION then you could join the union result to the comments and submissions tables.

    SELECT records.datatype, comments.*, submissions.*
    FROM (
        (
            SELECT comment_id, datatype
            FROM comments
            WHERE userid=3
            ORDER BY timestamp DESC
            LIMIT 20
        ) UNION ALL (
            SELECT content_id, datatype
            FROM submissions
            WHERE userid=3
            ORDER BY timestamp DESC
            LIMIT 20
        )
        ORDER BY timestamp DESC
        LIMIT 20
    ) AS records
    LEFT JOIN comments
        ON records.comment_id = comments.comment_id
        AND records.datatype = 'comment'
    LEFT JOIN submissions
        ON records.comment_id = submissions.content_id
        AND records.datatype = 'post'
    

    Alternatively, you could run the two innermost selects with all required fields and then order the final result in PHP.

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

Sidebar

Related Questions

Hi Stack Overflow community. I'm developing a wordpress website for Satan. First of all,
Somewhere here on Stack Overflow, I had found the following code some day which
I am trying to understand how to use SyndicationItem to display feed which is
Stack Overflowers: I have a search function on my company's website (based on .NET
Greetings Stack Overflow community. I'm trying to make a function that can pop all
I stack with this, I'm trying to create application using TabLayout. I have 3
Stack Overflowers: I have been racking my brain trying to get an List(of T)
My stack is the uWSGI 1.2.2, bottle, and gevent 1.0b2. I am trying to
Hail Stack! I'm having a hard time trying to figure out why my treeview
Hello Stack Overflow Experts, i have need of your expertice: I am trying to

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.