I have two tables (I'll list only fields that I want to search)
MySQL version: 5.0.96-community
Table 1: Clients (860 rows)
ID_CLIENT (varcahar 10)
ClientName (text)
Table 2: Details (22380 rows)
ID_CLIENT (varchar 10)
Details (varchar 1000)
The Details
table can have multiple rows from the same client.
I need to search into those two tables and retrive the ids of clients that match a search value.
If i have a search value "blue" it has to match CLientName
(ex the Blue
berries Corp), or the Details
in the second table (ex "it has a blue
logo)
The result should be a list of client id's that match the criteria.
If I make a query for one table, it takes a decent time
#0.002 sec
select a.`ID_CLIENT` from clienti a
where
a.`ClientName` LIKE '%blue%'
#0.1 sec
SELECT b.`ID_CLIENT` FROM Details b
WHERE b.`Details` LIKE '%blue%'
GROUP BY b.`GUID_CLIENT`
But if I try to join those two queries it takes ages.
My questions(s)
What's the best way of doing what I need here, to get a list of ID-s based on the search result from both tables
What to change to improve search performance in the Details table, I know that %..% is not fast, but I need partial matches too.
Edit (based on the answers)
#~0.2 sec
(SELECT a.`ID_CLIENT` FROM `clienti` a where a.`ClientName` like '%blue%')
union
(SELECT distinct b.`ID_CLIENT` FROM `Details` b where b.`Details` like '%blue%')
It returns a list of IDs from both tables filtred by the search value.
Edit 2: final query
And with that list of ids I can filter the client table, to get only the clients that are in boths tables based on their id
select cl.`ID_CLIENT`, `ClientName`, `OtherField`
from clients cl
join
((SELECT a.`ID_CLIENT` FROM `clients` a where a.`ClientName` like '%blue%')
union
(SELECT distinct b.`ID_Client` FROM `Details` b where b.`Detail` like '%blue%' )) rez
on cl.`ID_CLIENT` = rez.`ID_CLIENT`