SQL statement condition query has four keywords: Keyword 1, keyword 2, keyword 3, keyword 4,
I want to retrieve data that contains "Keyword 1" or "keyword 2" or "keyword 3" but does not contain "keyword 4" in a database field. how can I write an SQL statement?
Reply to discussion (solution)
Select * from table where (a field like '% keyword 1%' or a field like '% keyword 2%' or a field like '% keyword 3% ') and a field not like '% keyword 4%'
Upstairs positive solution
Positive solution on the second floor. Excellent writing
So much or we can consider using regular expressions.
SELECT * FROM tbl_name WHERE field REGEXP 'keyword 1 | keyword 2 | keyword 3 'and field not regexp 'keyword 4'
SELECT * FROM tbl_name WHERE field REGEXP 'keyword 1 | keyword 2 | keyword 3 'and field not regexp 'keyword 4'
Learned
SELECT * FROM tbl_name WHERE field REGEXP 'keyword 1 | keyword 2 | keyword 3 'and field not regexp 'keyword 4'
This method searches for duplicate data.
Select * from table where (a field like '% keyword 1%' or a field like '% keyword 2%' or a field like '% keyword 3% ') and a field not like '% keyword 4%'
This method searches for duplicate data.
Contains "Keyword 1"OrContains "keyword 2"Or"Keyword 3"
This is your requirement. it is normal to have repeated requests.
Contains "Keyword 1"OrContains "keyword 2"Or"Keyword 3"
This is your requirement. it is normal to have repeated requests.
Contains "Keyword 1"OrContains "keyword 2"Or"Keyword 3"
This is your requirement. it is normal to have repeated requests.
What should I do if I don't have repeated data?
Thank you for your advice.
Try distinct