interviz Posted February 21, 2007 Share Posted February 21, 2007 I am having a problem with a search script I have written to search a database. Each database record has its main record in the table Defect_Details, and has a number of records associated with the record in Defect_Details in a table called Defect_Audit I am trying to write a search facility that will search the either the title of a defect in the Defect_Details table, or both the Defect_Title field in Defect_Details and the Defect_Info field in the Defect_Audit table The tables are linked by a field called Defect_ID, which is the primary key on the details table, and a secondary key on the audit table Have the switch statement working to differentiate between the type of search, and have the sql working for the single table query select * from defect_detail where (Defect_Title like '%$searchtext%') but I am really struggling with the SQL syntax for the more complex search across both tables I roughly want to do select distinct * from defect_detail where ((Defect_Title like '%$searchtext%') or (defect_audit.Defect_Info like '%$searchtext%')) suspect I may need some kind of join in here, but have not managed to get it working despite pulling my hair out for a good few hours Any suggestions greatly appreciated Quote Link to comment https://forums.phpfreaks.com/topic/39481-multiple-tables-in-where-clause/ Share on other sites More sharing options...
fenway Posted February 21, 2007 Share Posted February 21, 2007 Actually, you'll need a union -- write each query separately, and then wrap them in parens, and add a UNION ALL. Quote Link to comment https://forums.phpfreaks.com/topic/39481-multiple-tables-in-where-clause/#findComment-190544 Share on other sites More sharing options...
interviz Posted February 21, 2007 Author Share Posted February 21, 2007 Thanks - that did help. The query as it stood did not work with the * because the structure of the two tables is different, so it threw an error. If I used the common field Defect_ID it did quite happily return the correct numbers and the search is working - its just not populating the fields apart from the defect_id anymore. Would the best way of dealing with this problem be to put the Defect_ID values in an array and then step through it, pulling in the details from each record as I go? Quote Link to comment https://forums.phpfreaks.com/topic/39481-multiple-tables-in-where-clause/#findComment-190558 Share on other sites More sharing options...
fenway Posted February 21, 2007 Share Posted February 21, 2007 Actually, you can "merge" the two tables in a number of ways -- you can just ask for the matching fields back, or mark the ones from each table/query different and figure it out later. Quote Link to comment https://forums.phpfreaks.com/topic/39481-multiple-tables-in-where-clause/#findComment-190757 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.