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
If the tables were both identical schema, you could
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…
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.