【问题标题】:How to find result with multiple where condition如何找到具有多个where条件的结果
【发布时间】:2016-05-19 12:02:05
【问题描述】:

下面是我的产品实体表,

我想从 user_id 不应该等于 12, 36,43 的表中获取数据

我怎样才能为此编写一个选择查询,

当前 sql 查询 -

select * from catalog_product_entity where entity_id = 12345;

我想要没有 user_id 12、36、43 的结果

desc catalog_product_entity;

+------------------+----------------------+------+-----+---------------------+----------------+
| Field            | Type                 | Null | Key | Default             | Extra          |
+------------------+----------------------+------+-----+---------------------+----------------+
| entity_id        | int(10) unsigned     | NO   | PRI | NULL                | auto_increment |
| entity_type_id   | smallint(8) unsigned | NO   | MUL | 0                   |                |
| attribute_set_id | smallint(5) unsigned | NO   | MUL | 0                   |                |
| type_id          | varchar(32)          | NO   |     | simple              |                |
| sku              | varchar(64)          | YES  | MUL | NULL                |                |
| created_at       | datetime             | NO   |     | 0000-00-00 00:00:00 |                |
| updated_at       | datetime             | NO   |     | 0000-00-00 00:00:00 |                |
| has_options      | smallint(1)          | NO   |     | 0                   |                |
| required_options | tinyint(1) unsigned  | NO   |     | 0                   |                |
| user_id          | mediumint(11)        | NO   |     | NULL                |                |
+------------------+----------------------+------+-----+---------------------+----------------+

从 catalog_product_entity 中选择不同的 user_id;

+---------+
| user_id |
+---------+
|       8 |
|       4 |
|       9 |
|       1 |
|      12 |
|      10 |
|      13 |
|      15 |
|       7 |
|      33 |
|      34 |
|      35 |
|      36 |
|      43 |
|      38 |
|      49 |
|      39 |
+---------+

【问题讨论】:

  • 提示,使用not in...

标签: mysql


【解决方案1】:

试试这个查询:

select * from catalog_product_entity 
   where user_id not in (12,36,43) 
   and entity_id = 12345;

【讨论】:

    【解决方案2】:

    使用WHERE ... NOT IN:

    SELECT * FROM catalog_product_entity
    WHERE entity_id = 12345 AND
          user_id NOT IN (12, 36, 43)
    

    【讨论】:

      【解决方案3】:

      试试这个

      select * from catalog_product_entity where user_id not in (12,36,43) and entity_id = 12345 order by desc
      

      【讨论】:

      • 虽然此代码可能会回答问题,但提供有关 why 和/或 如何 回答问题的额外上下文将显着改善其长期价值。请edit你的答案添加一些解释。
      【解决方案4】:

      没有:

      select * from catalog_product_entity where entity_id = 12345 and user_id not in (12, 36, 43);

      http://www.w3schools.com/sql/sql_in.asp

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-17
        • 2013-06-28
        相关资源
        最近更新 更多