petenaylor Posted October 26, 2010 Share Posted October 26, 2010 Hi all How do I modify the below code to search multiple tables in mySQL database? $query = "select * from store_items where description like \"%$trimmed%\" or title like \"%$trimmed%\" or dimensions like \"%$trimmed%\" order by id ASC"; $numresults=mysql_query($query); $numrows=mysql_num_rows($numresults); It is for a search function and I need it to search another table called 'about_text' and 'faq_text' Thanks Pete Quote Link to comment Share on other sites More sharing options...
vichu.1985 Posted October 26, 2010 Share Posted October 26, 2010 Dude, Need to know whether these three tables are related to each other. If its related tables then we'l b using joins. If these are not related, need to know the field names in each tables to write query with union functionality........ Quote Link to comment Share on other sites More sharing options...
petenaylor Posted October 26, 2010 Author Share Posted October 26, 2010 Hi there No they are not related. It's basically a search function so that I can search multiple tables and output the results to a page.. Thanks Pete Quote Link to comment Share on other sites More sharing options...
vichu.1985 Posted October 26, 2010 Share Posted October 26, 2010 IN that case we need to use union. Like, select * from store_items where description like \"%$trimmed%\" or title like \"%$trimmed%\" or dimensions like \"%$trimmed%\" order by id ASC union select * from about_text where description like \"%$trimmed%\" or title like \"%$trimmed%\" or dimensions like \"%$trimmed%\" order by id ASC union select * from faq_text where description like \"%$trimmed%\" or title like \"%$trimmed%\" or dimensions like \"%$trimmed%\" order by id ASC here the condition is first query's fields = 2nd query's fields = 3rd queries field Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.