sql LIKE和NOT在单个查询中相等,不起作用

时间:2022-11-15 20:10:25

I have a table called table1 with two coloumns "id" and "name". The values are :

我有一个名为table1的表,有两个coloumns“id”和“name”。价值观是:

id       name
----------------
(A1001   abc)
(A1002   bcd)
(A1003   cde)
(A1004   def)

I'm trying this query:

我正在尝试这个查询:

select distinct id,name
 from table1
 where id like '%1%' or name like '%c%' and id <> 'A1002'

It is not working. I want to get all the id and names that are like '1' or 'c' but want to exclude the row whose id is 'A1002'.

它不起作用。我想得到所有的id和名称,如'1'或'c',但想要排除id为'A1002'的行。

4 个解决方案

#1


1  

Use parenthesis, and has higher precedence than or.

使用括号,优先级高于或。

select distinct id,name
 from table1
 where (id like '%1%' or name like '%c%') and id <> 'A1002'

#2


1  

Try some brackets...

试试一些括号......

SELECT id,name 
FROM table1 
WHERE (id LIKE '%1%' OR name LIKE '%c%') AND id <> 'A1002'

#3


0  

try removing the distinct keyword. The ids make the rows distinct anyway.

尝试删除distinct关键字。无论如何,ids使行不同。

#4


0  

select distinct id,name from table1 where (id like '%1%' or name like '%c%') and id <> 'A1002'

What about braces? you mixed the and and or

大括号怎么样?你混合和和或

#1


1  

Use parenthesis, and has higher precedence than or.

使用括号,优先级高于或。

select distinct id,name
 from table1
 where (id like '%1%' or name like '%c%') and id <> 'A1002'

#2


1  

Try some brackets...

试试一些括号......

SELECT id,name 
FROM table1 
WHERE (id LIKE '%1%' OR name LIKE '%c%') AND id <> 'A1002'

#3


0  

try removing the distinct keyword. The ids make the rows distinct anyway.

尝试删除distinct关键字。无论如何,ids使行不同。

#4


0  

select distinct id,name from table1 where (id like '%1%' or name like '%c%') and id <> 'A1002'

What about braces? you mixed the and and or

大括号怎么样?你混合和和或