查找空座位问题
空座位:
一个餐厅1000个座位,用1000行,一行一个整数、一个字符(描述位置的地址)来存储数据。要求管理这个餐厅的座位,方便查找出空座位。并且按照起始和结束座位号分为几个片区。即空座位号的起始和结束分为多少个空座位片区。 解惑1:在座位号加上正负号,正号代表空座位,负号代表已经入座。 update id=-id where id= @id 要根据座位号查找出输入哪个片区时候,可以用绝对值差select ABS(id) from seat 。结束营业时候,重置座位号set id=ABS(id) 。 该方法虽然简单,但是在一列中存在了2个属性, 解惑2: 再建立一个表,存放已入坐的,已入坐的在源表中删除对应表。 解惑3: 加入两列无用的行id为0和1001,座位标记界限。 createtableseat(idintnotnullprimarykey,areachar(1)) insertintoseatvalues(100,'A') SELECT*FROMseat 查找一片空座位中的最后一个空座位, selectidfromseatwhere(id+1)notin(selectidfromseat)andid<1001 找到一片空座位中的第一个位置: selectidfromseatwhere(id-1)notin(selectidfromseat)andid>0 现在使用两个视图把 createviewfirst_seat(id)asselectidfromseatwhere(id-1)notin(selectidfromseat)andid>0 createviewlast_seat(id)asselectidfromseatwhere(id+1)notin(selectidfromseat)andid<1001 找出空座位片区: selectf1.idasstart,l1.idasfinishfromfirst_seatasf1,last_seatl1wherel1.id=(selectmin(l2.id)fromlast_seatl2wheref1.id<=l2.id) 优质内容筛选与推荐>>