一个查询的问题,忘大家帮帮忙

tonyyf000 2006-11-18 03:43:35
我在一张表中有两个状态,想根据这两个状态来查询表的信息
比如根据一个状态的查询是这样 select * from table where type=0
如果要根据两个状态该怎么查
希望大家帮帮忙 谢谢了
...全文
172 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
sgucxc0 2006-11-18
  • 打赏
  • 举报
回复
上面编辑有错.

--如果状态值只有0,1,那么用bit最好
declare @ta table(ID int identity(1,1),Name varchar(10),Type1 bit,Type2 bit)

insert into @ta select 'tom',1,1 union all select 'Jack',1,0 union all select 'tony',0,1
union all select 'lucy',1,1 union all select 'haha',1,0

select * from @ta
--测试数据
/*
1 tom 1 1
2 Jack 1 0
3 tony 0 1
4 lucy 1 1
5 haha 1 0
*/

select * from @ta where type1 in('1') and type2 in('1')
/*--测试结果
1 tom 1 1
4 lucy 1 1
*/
sgucxc0 2006-11-18
  • 打赏
  • 举报
回复
--如果状态值只有0,1,那么用bit最好
declare @ta table(ID int identity(1,1),Name varchar(10),Type1 bit,Type2 bit)

insert into @ta select 'tom',1,1 union all select 'Jack',1,0 union all select 'tony',0,1
union all select 'lucy',1,1 union all select 'haha',1,0

select * from @ta
--测试数据
/*

select * from @ta where type1 in('1') and type2 in('1')
/*--测试结果
1 tom 1 1
4 lucy 1 1
*/
1 tom 1 1
2 Jack 1 0
3 tony 0 1
4 lucy 1 1
5 haha 1 0
*/
jackeyabc 2006-11-18
  • 打赏
  • 举报
回复
declare @t table(id int,name varchar(6),type1 int,type2 int)
insert @t select 1,'tom',1,1
insert @t select 2,'tom',1,0
insert @t select 3,'tom',0,1
insert @t select 4,'tom',1,1
insert @t select 5,'tom',1,0
select * from @t where type1='1' and type2='1'

id name type1 type2
1 tom 1 1
4 lucy 1 1
  • 打赏
  • 举报
回复
select * from tablename where type1=1 and type2=1
  • 打赏
  • 举报
回复
问题好像不是很清楚 "如果要根据两个状态该怎么查" 这句话 意思是不是说查找 type=0 并且存在 type <>0 的 相关行 假如是这样 你相关行应该要给出
tonyyf000 2006-11-18
  • 打赏
  • 举报
回复
比如数据是这样
ID Name Type1 Type2
1 tom 1 1
2 Jack 1 0
3 tony 0 1
4 lucy 1 1
5 haha 1 0
我要根据type1和type2的状态查出的结果是
ID Name Type1 Type2
1 tom 1 1
4 lucy 1 1
sgucxc0 2006-11-18
  • 打赏
  • 举报
回复
如果有两个状态,一个为0,另一个为!0了

select * from table where type not in('0')

mugua604 2006-11-18
  • 打赏
  • 举报
回复
列出数据给大家吧
hhhdyj 2006-11-18
  • 打赏
  • 举报
回复
两个状态是说type字段有两个值吗?
十豆三 2006-11-18
  • 打赏
  • 举报
回复
如:
select * from table where type=0 or type=1

27,581

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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