简体   繁体   English

如何结合两个独立的mysql查询

[英]How to combine two separate mysql queries

What is the correct syntax for combining these mysql queries? 组合这些mysql查询的正确语法是什么?

$reserved = mysql_query("Select * from reservations where $timestamp >= ReservationStart AND $timestamp <= ReservationEnd");

and

$reserved2 = mysql_query("Select * from reservations where $ReservationEnd >= ReservationStart AND $ReservationEnd <= ReservationEnd");

I would like the reservation to meet both of these criteria but I am quite new to using mysql (and I know it's deprecated, but for the purpose of what i'm using it is easier), so i'm not sure how to put these two together. 我希望保留满足这两个条件,但是我对使用mysql很陌生(而且我知道它已经过时了,但是出于我使用它的目的,它比较容易),所以我不确定如何放置这两个在一起。

Combine the WHERE clauses with AND . WHERE子句与AND结合使用。 You can also use the BETWEEN operator instead of testing both >= and <= . 您也可以使用BETWEEN运算符,而不是同时测试>=<=

$reserved = mysql_query("Select * 
    from reservations 
    where $timestamp BETWEEN ReservationStart AND ReservationEnd
    AND $ReservationEnd BETWEEN ReservationStart AND ReservationEnd") or die(mysql_error());

I found the reason why the query wasn't working. 我找到了查询无法正常工作的原因。

It was simply because the syntax for the query was wrong, as I needed the inverted commas around the variables for it to work properly, like this: 这仅仅是因为查询的语法错误,因为我需要在变量周围使用反逗号以使其正常工作,如下所示:

$reserved = mysql_query("Select * from reservations where '$timestamp' BETWEEN ReservationStart AND ReservationEnd AND '$ReservationEnd' BETWEEN ReservationStart AND ReservationEnd

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM