SQL statement condition query

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.