且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在sql表中进行AND操作?

更新时间:2023-02-14 22:35:06

create table #bitoprt(id int identity(1,1),bag bit,tapee bit,band bit,knife bit,guitar bit,watch bit,umbrella bit,hammer bit)
insert into #bitoprt
values
(0, 1,  1,  0,  1,  1,  1,    0),
    (   0,  1,  1,  0,  1,  1,  0,    0),
    (   0,  1,  1,  0,  0,  0,  0,    0)


select (bt.bag & bt1.bag)bag,(bt.band & bt1.band)band,(bt.guitar & bt1.guitar)guitar
,(bt.hammer&bt1.hammer)hammer,(bt.knife&bt1.knife)knife
,(bt.tapee &bt1.tapee)tapee,(bt.umbrella&bt1.umbrella)umbrella,(bt.watch&bt1.watch)watch
from #bitoprt bt
join #bitoprt bt1 on bt.id = bt1.id+1
where bt.id in(1,2)


Hi,

Try below sql

select min(bag),min(Tapee),min(Band),min(shoes),min(knife),min(guitar),min(watch),min(umbrella),min(hammer)
 from [dbo].[transcation] where id in(1,2)



Answer is

0	0	1	1	0	1	1	0	0


here is how to do it in SQL Server

& (Bitwise AND) (Transact-SQL)[^]