使用 php/mysql 创建数据网格 - 联合查询?
我正在尝试从多个表中创建一个可用性网格来呈现信息,如下所示:
------------------------------------------------------
venue | 22/11 | 23/11 | 24/11 | 25/11 | 26/11 |
------------------------------------------------------
Some venue | £24 | £25 | £32 | N/A | £65 |
------------------------------------------------------
Some venue | £20 | N/A | £22 | £34 | £43 |
-------------------------------------------------------
对于每个场地,我可以使用如下查询为选定的日期段创建一个记录集:
SELECT temp_date.queryDate, availability.venueId
FROM temp_date
LEFT JOIN availability
ON temp_date.queryDate = availability.date
AND availability.venueId = 7
GROUP BY temp_date.queryDate
ORDER BY temp_date.queryDate;
这里的关键是我可以返回 null任何不可用的日期的值,同时将我的记录集保留为正确的天数(即一个工作周 5 个条目)。
为了将其他每个场馆添加到此列表中,我正在考虑简单地使用 UNION 查询将数据集组合成一个数据集,如下所示:
(SELECT temp_date.queryDate, availability.venueId
FROM temp_date
LEFT JOIN availability
ON temp_date.queryDate = availability.date
AND availability.venueId = 7
GROUP BY temp_date.queryDate
ORDER BY temp_date.queryDate)
UNION
(SELECT temp_date.queryDate, availability.venueId
FROM temp_date
LEFT JOIN availability
ON temp_date.queryDate = availability.date
AND availability.venueId = 8
GROUP BY temp_date.queryDate
ORDER BY temp_date.queryDate);
我对此的担忧是,可以查询 10 个不同的场馆,那么查询可能需要相当长的时间跑过去。
所以我的问题如下:UNION 查询是我唯一的选择吗?或者我可以使用一些替代的 JOIN 来确保我获得所有正在查询的场地的整周结果。
希望这一点很清楚:-S
编辑:
为了表明我所追求的内容,下面是所需记录集和当前查询的屏幕截图:
按照建议使用 IN() :
I'm attempting to create a grid of availability from a number of tables to present information as follows:
------------------------------------------------------
venue | 22/11 | 23/11 | 24/11 | 25/11 | 26/11 |
------------------------------------------------------
Some venue | £24 | £25 | £32 | N/A | £65 |
------------------------------------------------------
Some venue | £20 | N/A | £22 | £34 | £43 |
-------------------------------------------------------
For each venue I can create a recordset for the selected date period using a query such as:
SELECT temp_date.queryDate, availability.venueId
FROM temp_date
LEFT JOIN availability
ON temp_date.queryDate = availability.date
AND availability.venueId = 7
GROUP BY temp_date.queryDate
ORDER BY temp_date.queryDate;
The key here being that I can return a null value for any dates that are unavailable whilst keeping my recordset to the correct number of days (ie. 5 entries for a working week).
To add each of the other venues to this list I was considering simply using a UNION query to combine the datasets into one like so:
(SELECT temp_date.queryDate, availability.venueId
FROM temp_date
LEFT JOIN availability
ON temp_date.queryDate = availability.date
AND availability.venueId = 7
GROUP BY temp_date.queryDate
ORDER BY temp_date.queryDate)
UNION
(SELECT temp_date.queryDate, availability.venueId
FROM temp_date
LEFT JOIN availability
ON temp_date.queryDate = availability.date
AND availability.venueId = 8
GROUP BY temp_date.queryDate
ORDER BY temp_date.queryDate);
My concern with this is with say 10 different venues that could be queried then the query could take quite a long time to run through.
So my question is as follows: Is a UNION query my only option here or is there some alternative JOIN I can use to ensure that I get a full weeks results for all the venues being queried.
Hopefully this is clear :-S
EDIT:
To indicate what I'm after below are screenshots of the recordset required and current query:
Using IN() as suggested:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
另请注意,
GROUP BY
不是处理重复项的正确方法:事实上,您的查询在除MySQL
之外的任何其他系统中都会失败,甚至在MySQL 中也是如此。
并启用ANSI
GROUP BY
行为。(date, id)
是否可能在availability
中重复?如果是,应该选择这些重复项中的哪一个?Also note that
GROUP BY
is not a correct method to handle duplicates here: in fact, your query would wail in any other system exceptMySQL
, and even inMySQL
withANSI
GROUP BY
behavior enabled.Are duplicates on
(date, id)
possible inavailability
? If yes, which of these duplicates should be selected?为什么不使用 IN() ?
Why not use IN() ?
是的,当然,正如 Webnet 所描述的,您可以使用 IN 语句来选择您的场地 ID。
yes,definitely, as Webnet describe you can use IN statement for selecting your venueID.