我有这两个查询,我想将它们合并在一起,以便用户根据第二个查询过滤第一个查询

第一个查询:

SELECT t1 . *
FROM car t1
left outer JOIN rent ON t1.carId = rent.carId
WHERE NOT
EXISTS (

SELECT NULL
FROM rent t2
WHERE t1.carId = t2.carId

)
OR not (
rent.startdate > '$to'
OR rent.enddate < '$from'
)


第二个查询:

SELECT carId,model,fuelconsumption,type,picture,color,price,region FROM  car WHERE model ='$m' OR type='$t' OR price='$p' OR color = '$c' ORDER BY model,type,price,color DESC


这两个查询分别给出正确的答案

最佳答案

我不确定要了解您的请求,但是如果您想合并两个查询,则应该这样做:

select t1.*
from car t1
left outer join rent on t1.carId = rent.carId
where
(
  not exists
  (
    select null
    from rent t2
    where t1.carId = t2.carId
  )
  or not
  (
    rent.startdate > '$to'
    or rent.enddate < '$from'
  )
)
and
(
  model ='$m'
  OR type='$t'
  OR price='$p'
  OR color = '$c'
)
order by model,type,price,color desc

关于php - 如何从嵌套联接查询中选择,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23948194/

10-10 03:34