【问题标题】:Sql Returning null from second selectSql从第二次选择返回null
【发布时间】:2013-05-19 12:27:59
【问题描述】:

我有这样的查询

select * from Cars where 
car_id!=(Select car_id from reservation where reservation_date between '2013-05-13' and '2013-05-15')

如果在这两个日期之间没有任何内容但它不起作用,我想使用 car_id=''。

【问题讨论】:

  • 你在说什么 SQL 引擎?试试NVL( subquery, '' )

标签: sql select null


【解决方案1】:

首先检查这是否返回正确的值

Select car_id 
   from reservation 
   where reservation_date 
   between '2013-05-13' and '2013-05-15'

试试这个:

select * 
from Cars 
where car_id
not in 
(
   Select car_id 
     from reservation 
     where reservation_date between '2013-05-13' and '2013-05-15'
)

【讨论】:

  • @BarışSaçıkara 。 . .如果这个答案对你有用,那么你应该接受它。
【解决方案2】:

按照 draxxxeus 的回答使用“不在”会起作用,但是一旦您获得大量数据,您的查询就会很慢。一种不太直观但在逻辑上等效的方法是这样的:

where car_id in 
(select car_id
from cars
except 
select car_id
from reservation
etc
)

如果您的数据库支持该语法,它将比使用“not in”运行得更快。对于某些数据库,例如 Oracle,您必须使用单词“minus”而不是“except”。

【讨论】:

  • 。 . not in 的性能取决于数据库。即使在通常表现不佳的数据库上,使用正确的索引通常也非常有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-03
  • 1970-01-01
  • 1970-01-01
  • 2021-07-05
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
相关资源
最近更新 更多