日期:2014-05-18  浏览次数:20948 次

JSP问题,高手赐教
有两个表user,主要字段有:userid     username   carid  
            表car,主要字段有:carid   carname   free
界面是一个下拉菜单   现在就是想能不能通过SQL语句来实现下拉菜单里显示的车必须free=ture并且之前没被别的司机绑定过..就这样   。如果只是(select   *   from   car   where   free=true)   那被绑定过的车还是会显示出来


------解决方案--------------------
既然车绑定了 那你就得 update 下表car 的free 字段..使得它不能再被其他人绑定.
然后你select 的时候根据free状态去判断...
------解决方案--------------------
如果是oracle的话,可以这样写:
select * from car where free=true and carid not in (select carid from user where)
------解决方案--------------------
select * from car where free=true and carid=null
------解决方案--------------------
如果是mysql可以用
select * from user left join car on user.free=car.free where(car.free=true)
采用左或右连接方式。
------解决方案--------------------
加个锁

select * from car with(UPDLOCK,READPAST) where free=true
------解决方案--------------------
select c.* from car c left join user u on c.carid <> u.carid where c.free=true
------解决方案--------------------
我的思路就是
table里所有的纪录 - car已经被绑的纪录 = 你要的结果
------解决方案--------------------
select c.carid,c.carname
from user u,car c
where c.free=true
and u.carid!=c.carid
------解决方案--------------------
create table [user]
(
userid int not null primary key,
username varchar(10),
carid int
)

create table car
(
carid int not null primary key,
carname varchar(10),
free bit
)
go

alter table [user] add constraint FK_CARID foreign key(carid) references car(carid)
go

insert into car values(1, 'car1 ',0);
insert into car values(2, 'car2 ',0);
insert into car values(3, 'car3 ',1);
insert into car values(4, 'car4 ',0);
insert into car values(5, 'car5 ',1);
insert into [user] values(1, 'user1 ',1);
insert into [user] values(2, 'user2 ',2);
go

select * from [user];
select * from car;

****************************

select distinct car.carid,car.carname from car,[user] where car.carid <> [user].carid and car.free = 1;