使用MySQL(和PHP)搜索可用性吗?

使用MySQL(和PHP)搜索可用性吗?,第1张

概述我有两个MySQL(MyIsAm)表,分别代表出租单位和预订:> LettingUnits(ID,名称等)> LettingUnitBookings(ID,F_LU_ID,开始,结束)其中F_LU_ID是该单元的外键.搜索特定时段内可用单位的最佳方法是什么?搜索通过开始,结束和持续时间.>开始=最早开始预定>结束=预订的最后结束&gt

我有两个MySQL(MyIsAm)表,分别代表出租单位和预订:

> LettingUnits(ID,名称等)
> LettingUnitBookings(ID,F_LU_ID,开始,结束)

其中F_LU_ID是该单元的外键.

搜索特定时段内可用单位的最佳方法是什么?搜索通过开始,结束和持续时间.

>开始=最早开始预定
>结束=预订的最后结束
>持续时间=预订持续时间

我想知道是否有可能在MysqL中执行此 *** 作,但是如果不能,那么最好的方法是在PHP中执行此 *** 作.

在回答下面的答案时,我觉得一个例子将有助于解释问题.

一个LettingUnit:

>(123,“ Foo Cottage”)

一些LettingUnitBookings:

>(400,123,01/01/09,05/01/09)-5天预订
>(401,10/01/09,20/01/09)-10天预订
>(402,25/01/09,30/01/09)-5天预订

如果我们搜索:

>开始= 01/01/09
>结束= 01/02/2009
>持续时间= 5(天)

然后,我们要显示该单元.因为在搜索范围内可以预订5天.

如果持续时间为10,则该单元将不会显示,因为在搜索范围内没有连续10天的未预订天数.

最佳答案这是一个可行的解决方案:

SELECT t.*,DATEDIFF(t.LatestAvailable,t.EarlIEstAvailable) AS LengthAvailableFROM    (SELECT u.*,COALESCE(b1.End,@StartOfWindow) AS EarlIEstAvailable,COALESCE(b2.Start,@EndOfWindow) AS LatestAvailable    FROM LettingUnits u    left OUTER JOIN LettingUnitBookings b1      ON (u.ID = b1.F_LU_ID AND b1.End BETWEEN @StartOfWindow AND @EndOfWindow)    left OUTER JOIN LettingUnitBookings b2      ON (u.ID = b2.F_LU_ID AND b2.Start BETWEEN @StartOfWindow AND @EndOfWindow          AND b2.Start >= b1.End) -- edit: new term    ) AS tleft OUTER JOIN LettingUnitBookings x  ON (t.ID = x.F_LU_ID AND x.Start < t.LatestAvailable AND x.End > t.EarlIEstAvailable)WHERE x.ID IS NulL AND DATEDIFF(t.LatestAvailable,t.EarlIEstAvailable) >= @windowsize;

输出为:

+-----+-------------+-------------------+-----------------+-----------------+| ID  | name        | EarlIEstAvailable | LatestAvailable | LengthAvailable |+-----+-------------+-------------------+-----------------+-----------------+| 123 | Foo Cottage | 2009-01-05        | 2009-01-10      |               5 || 123 | Foo Cottage | 2009-01-20        | 2009-01-25      |               5 || 456 | bar Cottage | 2009-01-20        | 2009-01-31      |              11 |+-----+-------------+-------------------+-----------------+-----------------+

用EXPLAIN对此进行分析表明,它很好地利用了索引:

+----+-------------+------------+--------+---------------+---------+---------+-------+------+-------------------------+| ID | select_type | table      | type   | possible_keys | key     | key_len | ref   | rows | Extra                   |+----+-------------+------------+--------+---------------+---------+---------+-------+------+-------------------------+|  1 | PRIMARY     | <derived2> | ALL    | NulL          | NulL    | NulL    | NulL  |    9 | Using where             ||  1 | PRIMARY     | x          | ref    | F_LU_ID       | F_LU_ID | 8       | t.ID  |    2 | Using where; Not exists ||  2 | DERIVED     | u          | system | NulL          | NulL    | NulL    | NulL  |    1 |                         ||  2 | DERIVED     | b1         | ref    | F_LU_ID       | F_LU_ID | 8       | const |    0 |                         ||  2 | DERIVED     | b2         | ref    | F_LU_ID       | F_LU_ID | 8       | const |    0 |                         |+----+-------------+------------+--------+---------------+---------+---------+-------+------+-------------------------+

与@martin clayton的given解决方案的EXPLAIN报告进行比较:

+----+--------------+---------------------+--------+---------------+---------+---------+------+------+---------------------------------+| ID | select_type  | table               | type   | possible_keys | key     | key_len | ref  | rows | Extra                           |+----+--------------+---------------------+--------+---------------+---------+---------+------+------+---------------------------------+|  1 | PRIMARY      | lu                  | system | PRIMARY,ID    | NulL    | NulL    | NulL |    1 |                                 ||  1 | PRIMARY      | <derived2>          | ALL    | NulL          | NulL    | NulL    | NulL |    4 | Using where                     ||  2 | DERIVED      | <derived3>          | ALL    | NulL          | NulL    | NulL    | NulL |    4 | Using temporary; Using filesort ||  2 | DERIVED      | <derived5>          | ALL    | NulL          | NulL    | NulL    | NulL |    4 | Using where; Using join buffer  ||  5 | DERIVED      | LettingUnitBookings | ALL    | NulL          | NulL    | NulL    | NulL |    3 |                                 ||  6 | UNION        | LettingUnitBookings | index  | NulL          | F_LU_ID | 8       | NulL |    3 | Using index                     || NulL | UNION RESulT | <union5,6>          | ALL    | NulL          | NulL    | NulL    | NulL | NulL |                                 ||  3 | DERIVED      | LettingUnitBookings | ALL    | NulL          | NulL    | NulL    | NulL |    3 |                                 ||  4 | UNION        | LettingUnitBookings | index  | NulL          | F_LU_ID | 8       | NulL |    3 | Using index                     || NulL | UNION RESulT | <union3,4>          | ALL    | NulL          | NulL    | NulL    | NulL | NulL |                                 |+----+--------------+---------------------+--------+---------------+---------+---------+------+------+---------------------------------+

通常,您要避免强制使用文件排序或使用临时的优化计划,因为这些计划会降低性能.使用GROUP BY的查询几乎可以肯定会导致这种优化,至少在MysqL中如此. 总结

以上是内存溢出为你收集整理的使用MySQL(和PHP)搜索可用性吗? 全部内容,希望文章能够帮你解决使用MySQL(和PHP)搜索可用性吗? 所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1165484.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存