sql:选择两列值所在的行

时间:2018-09-12 03:51:42

标签: sql

如何选择不在company = c和leg = 2处的行 没有特定的数据库功能

+---------+-----+
| company | leg |
+---------+-----+
| c       |   1 |
| b       |   2 |
| c       |   2 |
| d       |   1 |
+---------+-----+

并获得:

+---------+-----+
| company | leg |
+---------+-----+
| a       |   1 |
| b       |   2 |
| d       |   1 |
+---------+-----+

这是典型的错误方式:

SELECT *
FROM   mytable
WHERE  company <> 'c' AND leg <> 2

4 个答案:

答案 0 :(得分:1)

您可以简单地将这两个条件放在where子句中:

SELECT *
FROM   mytable
WHERE  company <> 'c' OR leg <> 2

答案 1 :(得分:0)

您可以尝试-

select company, leg
from table-name
where company != 'c'
and leg != 2

答案 2 :(得分:0)

您可以尝试一下

 select company,leg from mytable where company !='c' OR leg !=2;

结果:

    +---------+-----+
    | company | leg |
    +---------+-----+
    | c       |   1 |
    | b       |   2 |
    | d       |   1 |
    +---------+-----+

答案 3 :(得分:-1)

尝试一下:

SELECT * FROM TABLE_NAME WHERE COMPANY !='c' AND LEG !='2';