Make MySQL ignore "The" from the start of a LIKE condition?
I have a list of company names, and want to display all the names beginning with a certain character. d
for example.
So the query is
SELECT * FROM company WHERE name LIKE 'd%'
But I also want the companies that start with "The" so
SELECT * FROM company WHERE name LIKE 'd%' OR name LIKE 'The d%'
But now the tricky bit when we get to 't' I want to remove all those that start with 'The' while keeping others that begin with a 't' (and also keeping anything like 'The Truffle House' where the word after 'The' begins wi开发者_运维百科th a 't').
Any ideas? Would like to keep the logic in MySQL...
SELECT *
FROM company
WHERE (name LIKE '?%' AND NOT name LIKE 'The %')
OR name LIKE 'The ?%'
Try:
SELECT * FROM company WHERE name regexp '^(The)d'
Thanks for the answers I've just opted for this
SELECT *
FROM company
WHERE (name LIKE '?%' AND NOT name LIKE 'The %')
OR name LIKE 'The ?%'
for t
I've just done a long list of NOT LIKE
so
AND (company_name LIKE 'The%t' OR company_name LIKE 't%') AND company_name NOT LIKE 'The a%' AND company_name NOT LIKE 'The b%'
etc....
精彩评论