SQL statement Condition query problem

Source: Internet
Author: User
Tags repetition
There are four key words: keyword 1, keyword 2, keyword 3, keyword 4,
I'm going to go out. A field in the database contains "keyword 1" or "Keyword 2" or "Keyword 3" but does not contain "keyword 4" data, how to write 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

Second floor positive solution. It's a great writing.

So much or you can think of regular.

SELECT * from tbl_name WHERE field REGEXP ' keywords 1| keywords 2| keyword 3 ' and  field not REGEXP ' keyword 4 '

SELECT * from tbl_name WHERE field REGEXP ' keywords 1| keywords 2| keyword 3 ' and  field not REGEXP ' keyword 4 '


Study the

SELECT * from tbl_name WHERE field REGEXP ' keywords 1| keywords 2| keyword 3 ' and  field not REGEXP ' keyword 4 '



This method will search 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 will search for duplicate data

Contains "Keyword 1" or "Keyword 2" or "Keyword 3"

That's what your needs are, repetition is normal.

Contains "Keyword 1" or "Keyword 2" or "Keyword 3"

That's what your needs are, repetition is normal.



Contains "Keyword 1" or "Keyword 2" or "Keyword 3"

That's what your needs are, repetition is normal.




I don't seem to have duplicate data, what should I do?
Thanks for pointing.

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.