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.
Any help with building this query will be deeply appreciated.
Yup, that’s easily done with joins. Check out the MySQL documentation for the syntax. There are also a plethora of join tutorials out there for beginners.
What I am trying to achieve is the followong. If the serarch term exist in posts, I want to show the post, with the date and who post it. If the search term exist in the user name or e-mail, I would like to display those users.
So if someone search for lorem ipsum…I want the posts that contain lorem ipsum to be displayed. However, if there is user named Lorem, I want both , the post and the user. First: search results: posts and then the posts that contain search term. When done with posts, on same page I want : search results: people and there the data of all users that have Lorem in the user name or in their emails
Hopefully this clarify everything. Just didn’t know how to proceed. Ok, going with 2 queries…