Select all except some rows

2020-08-27 03:14发布

let's say i have table with this structure:

  id      model       color
------  --------    ----------
   1      Ford           yellow
   2      Ford           green
   3      Ford           red
   4      Ford           yellow
   5      Subaru         yellow
   6      Subaru         red

I need to make a query, which returns me every car in list, except for yellow ford's. Can somebody help?

标签: sql
5条回答
我想做一个坏孩纸
2楼-- · 2020-08-27 04:00

If you want to deselect some rows dynamically, you may use it:

SELECT * FROM `table_1` WHERE id NOT IN (SELECT id FROM `table_2` WHERE column_name='value')

NOTE: Here, id is a column_name, which both tables have in common.

Good luck. :)

查看更多
劳资没心,怎么记你
3楼-- · 2020-08-27 04:08

Hope this will help you.

    $this->db->where('model !=','FORD' AND 'color!=','yellow');
    $query= $this->db->get('table_name');
    $res = $query->result_array();
    return $res;
查看更多
Summer. ? 凉城
4楼-- · 2020-08-27 04:15
... WHERE NOT (model = 'Ford' AND color = 'yellow')
查看更多
姐就是有狂的资本
5楼-- · 2020-08-27 04:15

Use Color<> "yellow" and model <> "Ford" as your where clause

查看更多
做自己的国王
6楼-- · 2020-08-27 04:19

In addition to Foo Bah's answer,

if you have null column values like that;

NAME      COLOR

Ford      green               
Ford      red                 
Subaru    yellow              
Subaru    red                 
Subaru    NULL
Ford      NULL              

you need to use ISNULL() function to bring null column values

... WHERE  NOT (ISNULL(NAME,'') = 'Ford' AND ISNULL(COLOR,'') = 'yellow')
查看更多
登录 后发表回答