查询问题?

sunfor 2011-10-09 05:10:37
数据如下:
code name type
101 金丝柚 半成品
102 黄云香 半成品
103 白枫 半成品
201 水曲柳 成品
201-7 不织布 成品
203 泰柚 成品
248-1 加工珍珠 NULL
301 花梨 普通木片
302 泰柚 普通木片
303 人造泰柚 普通木片
401 白板 夹板
402 面板 夹板
想查TYPE不等于“半成品”及“成品”的商品,用
select * from goods where type_m<>'半成品' or type_m<>'成品'

select * from goods where type_m<>'半成品' and type_m<>'成品'

为何都查到数据里都有“半成品' 及”成品“出现?跟没加WHERE一样!
...全文
125 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
pengxuan 2011-10-10
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 ssp2009 的回复:]
引用 11 楼 pengxuan 的回复:
SQL code

select * from tb where [type]<>'半成品' and [type]<>'成品'


为什么这样的查询结果中没有type为NULL的那一行
用or和不等于加在一起是否定之否定之否定
[/Quote]
还是没看明白
快溜 2011-10-10
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 pengxuan 的回复:]
SQL code

select * from tb where [type]<>'半成品' and [type]<>'成品'


为什么这样的查询结果中没有type为NULL的那一行
[/Quote]用or和不等于加在一起是否定之否定之否定
feihongluori 2011-10-10
  • 打赏
  • 举报
回复
use students3;
go
if object_id('tb3')is not null
drop table tb3;
go
create table tb3
(
code varchar(5),
name varchar(20),
type varchar(20)
)
insert into tb3
select '101','金丝柚','半成品' union all
select '102','黄云香','半成品' union all
select '103','白枫','半成品' union all
select '201','水曲柳','成品' union all
select '201-7','不织布','成品' union all
select '203','泰柚','成品' union all
select '248-1', '加工珍珠', NULL union all
select '301' ,'花梨' ,'普通木片' union all
select '302' ,'泰柚' ,'普通木片' union all
select '303' ,'人造泰柚', '普通木片' union all
select '401', '白板', '夹板' union all
select '402' ,'面板' ,'夹板'
select * from tb3 where type<>'半成品'and type<>'成品'
/*code name type
----- -------------------- --------------------
301 花梨 普通木片
302 泰柚 普通木片
303 人造泰柚 普通木片
401 白板 夹板
402 面板 夹板
*/
没有问题啊
lnmhfeng 2011-10-09
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 qianjin036a 的回复:]

SQL code
create table [tb]([code] varchar(5),[name] varchar(8),[type] varchar(8))
insert [tb]
select '101','金丝柚','半成品' union all
select '102','黄云香','半成品' union all
select '103','白枫','半成品' union all
……
[/Quote]
-晴天 2011-10-09
  • 打赏
  • 举报
回复
create table [tb]([code] varchar(5),[name] varchar(8),[type] varchar(8))
insert [tb]
select '101','金丝柚','半成品' union all
select '102','黄云香','半成品' union all
select '103','白枫','半成品' union all
select '201','水曲柳','成品' union all
select '201-7','不织布','成品' union all
select '203','泰柚','成品' union all
select '248-1','加工珍珠',null union all
select '301','花梨','普通木片' union all
select '302','泰柚','普通木片' union all
select '303','人造泰柚','普通木片' union all
select '401','白板','夹板' union all
select '402','面板','夹板'
go
select * from tb where [type]<>'半成品' and [type]<>'成品' or [type] is null
/*
code name type
----- -------- --------
248-1 加工珍珠 NULL
301 花梨 普通木片
302 泰柚 普通木片
303 人造泰柚 普通木片
401 白板 夹板
402 面板 夹板

(6 行受影响)

*/
go
drop table tb
pengxuan 2011-10-09
  • 打赏
  • 举报
回复

select * from tb where [type]<>'半成品' and [type]<>'成品'

为什么这样的查询结果中没有type为NULL的那一行
iymmgd 2011-10-09
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 geniuswjt 的回复:]

长相问题
[/Quote]
这个回答牛
geniuswjt 2011-10-09
  • 打赏
  • 举报
回复
长相问题
快溜 2011-10-09
  • 打赏
  • 举报
回复
没有问题
快溜 2011-10-09
  • 打赏
  • 举报
回复
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([code] varchar(5),[name] varchar(8),[type] varchar(8))
insert [tb]
select '101','金丝柚','半成品' union all
select '102','黄云香','半成品' union all
select '103','白枫','半成品' union all
select '201','水曲柳','成品' union all
select '201-7','不织布','成品' union all
select '203','泰柚','成品' union all
select '248-1','加工珍珠',null union all
select '301','花梨','普通木片' union all
select '302','泰柚','普通木片' union all
select '303','人造泰柚','普通木片' union all
select '401','白板','夹板' union all
select '402','面板','夹板'

select * from tb where [type]<>'半成品' and [type]<>'成品'

/*
code name type
----- -------- --------
301 花梨 普通木片
302 泰柚 普通木片
303 人造泰柚 普通木片
401 白板 夹板
402 面板 夹板

(5 行受影响)
sunfor 2011-10-09
  • 打赏
  • 举报
回复
type 是varchar
为何用<>不行!
要用not !
--小F-- 2011-10-09
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2011-10-09 17:15:53
-- Verstion:
-- Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (Intel X86)
-- Apr 22 2011 11:57:00
-- Copyright (c) Microsoft Corporation
-- Enterprise Evaluation Edition on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([code] varchar(5),[name] varchar(8),[type] varchar(8))
insert [tb]
select '101','金丝柚','半成品' union all
select '102','黄云香','半成品' union all
select '103','白枫','半成品' union all
select '201','水曲柳','成品' union all
select '201-7','不织布','成品' union all
select '203','泰柚','成品' union all
select '248-1','加工珍珠',null union all
select '301','花梨','普通木片' union all
select '302','泰柚','普通木片' union all
select '303','人造泰柚','普通木片' union all
select '401','白板','夹板' union all
select '402','面板','夹板'
--------------开始查询--------------------------
select * from tb t where not exists(select 1 from tb where (type='成品' or type='半成品') and code=t.code and name=t.name)
----------------结果----------------------------
/*code name type
----- -------- --------
248-1 加工珍珠 NULL
301 花梨 普通木片
302 泰柚 普通木片
303 人造泰柚 普通木片
401 白板 夹板
402 面板 夹板

(6 行受影响)
*/
NBDBA 2011-10-09
  • 打赏
  • 举报
回复
select * from goods where not (type='半成品' or type='成品')

--小F-- 2011-10-09
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2011-10-09 17:15:53
-- Verstion:
-- Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (Intel X86)
-- Apr 22 2011 11:57:00
-- Copyright (c) Microsoft Corporation
-- Enterprise Evaluation Edition on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([code] varchar(5),[name] varchar(8),[type] varchar(8))
insert [tb]
select '101','金丝柚','半成品' union all
select '102','黄云香','半成品' union all
select '103','白枫','半成品' union all
select '201','水曲柳','成品' union all
select '201-7','不织布','成品' union all
select '203','泰柚','成品' union all
select '248-1','加工珍珠',null union all
select '301','花梨','普通木片' union all
select '302','泰柚','普通木片' union all
select '303','人造泰柚','普通木片' union all
select '401','白板','夹板' union all
select '402','面板','夹板'
--------------开始查询--------------------------
select * from tb where type not in ('半成品','成品')
----------------结果----------------------------
/* code name type
----- -------- --------
301 花梨 普通木片
302 泰柚 普通木片
303 人造泰柚 普通木片
401 白板 夹板
402 面板 夹板

(5 行受影响)

*/
快溜 2011-10-09
  • 打赏
  • 举报
回复
type_m什么类型,如果是nvarchar,试试type_m<>N'半成品'
sunfor 2011-10-09
  • 打赏
  • 举报
回复
更正
数据如下:
code name type
101 金丝柚 半成品
102 黄云香 半成品
103 白枫 半成品
201 水曲柳 成品
201-7 不织布 成品
203 泰柚 成品
248-1 加工珍珠 NULL
301 花梨 普通木片
302 泰柚 普通木片
303 人造泰柚 普通木片
401 白板 夹板
402 面板 夹板
想查TYPE不等于“半成品”及“成品”的商品,用
select * from goods where type<>'半成品' or type<>'成品'

select * from goods where type<>'半成品' and type<>'成品'

为何都查到数据里都有“半成品' 及”成品“出现?跟没加WHERE一样!

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧