Php 如何从嵌套联接查询中进行选择

Php 如何从嵌套联接查询中进行选择,php,mysql,Php,Mysql,我有这两个查询,我想将它们连接在一起,以便让用户根据第二个查询过滤第一个查询 第一个问题: 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 <

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

第一个问题:

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 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

我试过了,但不是像你那样。。。我在大括号前按型号、型号、价格、颜色说明写了订单。。。您这样写是有效的是的,您必须在where子句之后编写order by。如果我没问题,就把它标记为已回答。但我还有另一个评论。。。为什么它没有检索rent.startdate>“$to”或rent.enddate<“$from”它只是给我查询的第一个和最后一个结果我不明白,你期望的结果是什么?
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