mysql query with AND, OR and NOT

后端 未结 3 1303
鱼传尺愫
鱼传尺愫 2021-01-02 22:59

Lets say I have a table of articles with as many to many relationship with topics. Each topic assigned to an article has a type field which can contain 1 of 3 v

3条回答
  •  孤街浪徒
    2021-01-02 23:14

    The ALL and NOT parts are very simple, you just chain them with ANDs:

    SELECT X FROM Y WHERE a AND b AND c AND NOT d AND e AND NOT e.

    And the ORs go between:

    SELECT X FROM Y WHERE ((a AND b AND c) AND (d OR e OR f)) AND NOT g AND NOT h

    replace small numbers with comparisons and you're done. So if you want to do this in code, sort your conditions and then just chain them together as a String. Be careful to avoid SQL-Insertions.

提交回复
热议问题