JonasE Posted March 5, 2007 Share Posted March 5, 2007 Hello, I currently have two tables: documents with fields: id title vartitles with fields: id doc_id vartitle Each record can have one primary title (record.title) and several vartitles (referenced by record_id) Here's my problem: I want to search these two tables for matches to titles. After this, I want to return all the documents matching the the title (i.e. matching vartitle and title fields) regardless of whether match for the title was found in the documents table, or in the vartitles table. I know that I can use UNION ALL to return for instance documents.id and vartitles.record_id fields, but how do I select all records matching either of those? Do i use a subquery of some sort? Thank you, Jonas Link to comment https://forums.phpfreaks.com/topic/41349-solved-multiple-select-advice/ Share on other sites More sharing options...
artacus Posted March 5, 2007 Share Posted March 5, 2007 Use a left join and an OR in your where clause. Link to comment https://forums.phpfreaks.com/topic/41349-solved-multiple-select-advice/#findComment-200360 Share on other sites More sharing options...
JonasE Posted March 5, 2007 Author Share Posted March 5, 2007 Thanks for the reply Artacus. Do I need to do a UNION at all? Could you provide an example? Thanks so much. Link to comment https://forums.phpfreaks.com/topic/41349-solved-multiple-select-advice/#findComment-200363 Share on other sites More sharing options...
artacus Posted March 5, 2007 Share Posted March 5, 2007 No union. I'm not sure if you've got a full text index or not. Use match() against() if you do. SELECT d.* FROM documents AS d LEFT JOIN vartitles AS v ON d.id = v.doc_id WHERE d.title LIKE '%search%' OR v.vartitle LIKE '%search%' GROUP BY d.id Link to comment https://forums.phpfreaks.com/topic/41349-solved-multiple-select-advice/#findComment-200378 Share on other sites More sharing options...
JonasE Posted March 6, 2007 Author Share Posted March 6, 2007 Thanks so much Artacus. I'll give that a go! Link to comment https://forums.phpfreaks.com/topic/41349-solved-multiple-select-advice/#findComment-200390 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.