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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:00:03+00:00 2026-06-05T22:00:03+00:00

I have the following problem: I have a search box in which user can

  • 0

I have the following problem:

I have a search box in which user can search either for a post, username or e-mail.

Posts are in my posts table, and users are located in my users table. Tables have the following structure:

Posts

post_id
user_id
post
post_date
visible

Users

userid
username
email
firstname
lastname

Can I get the results from both tables with one query? Not sure how that will benefit me (if at all), just asking.

Then I should display the results on same page, but posts first, and after the users. As you can see, posts will have different data to display and users will display different data.

I know that group by clausule exist, so I would like to group the results,first to get posts and then users.

Any help with building this query will be deeply appreciated.

Regards,Zoreli

  • 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-05T22:00:06+00:00Added an answer on June 5, 2026 at 10:00 pm

    If the tables were both identical schema, you could

    SELECT fields from posts where (yadda = 1)
    UNION 
    SELECT fields from users where (yadda = 1)
    

    However, this isnt possible because your tables have different schema and data types
    (ie post_date is presumably a datetime, whereas the corresponding field in users table is firstname, which is a string)

    You could in theory work around this, by having dummy fields which are only conditionally populated for posts, or for users…

    SELECT 'P' as result_type, post_id, user_id, post_date, visible, NULL as username, NULL as email, NULL as firstname, NULL as lastname from posts where (yadda = 1)
    UNION ALL
    SELECT 'U', NULL, userid, NULL, NULL, username, email, firstname, lastname from users where (yadda = 1)
    

    But this is a pretty crappy way of doing things. You’re transferring more data, and then have to code logic to check what sort of row it is, and interrogate the relevant fields.

    So the simple answer is to just issue two separate queries.

    The only time I’d recommend the above is if you have some complex db-based sort order, which means you have to merge the posts records with the user records. From your question tho, this sounds unlikely.

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

Sidebar

Related Questions

I have the following problem. I have a table with a few hundred thousand
Hello i have the following problem. I have a search page lets call it
I have the following problem: in a database table are Sales recorded. One of
I am trying to search questions which have a given tag. How can you
I have the following problem. My sitemap's content is shown in GOOGLE search results.
I'm trying to use the search text box (which I made by following this
I have the following function which applies watermark text to any text box on
I have the following problem: We have several different databases on which our program
Again in problem Actually I have following jsp code in which I have few
I have following problem: I have built a tabbar application with 4 tabs. I

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.