使用 php/mysql 创建数据网格 - 联合查询?

发布于 2024-10-03 12:58:13 字数 1762 浏览 3 评论 0原文

我正在尝试从多个表中创建一个可用性网格来呈现信息,如下所示:

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

编辑:

为了表明我所追求的内容,下面是所需记录集和当前查询的屏幕截图:

alt text

alt text

按照建议使用 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:

alt text

alt text

Using IN() as suggested:
alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

幸福%小乖 2024-10-10 12:58:13
SELECT  td.query_date, a.venueId
FROM    temp_date td
CROSS JOIN
        (
        SELECT  7 AS id
        UNION ALL
        SELECT  8 AS id
        ) ids
LEFT JOIN
        availability a
ON      a.date = td.query_date
        AND a.id = ids.id
ORDER BY
        ids.id, td.query_date

另请注意,GROUP BY 不是处理重复项的正确方法:事实上,您的查询在除 MySQL 之外的任何其他系统中都会失败,甚至在 MySQL 中也是如此。 并启用 ANSI GROUP BY 行为。

(date, id) 是否可能在 availability 中重复?如果是,应该选择这些重复项中的哪一个?

SELECT  td.query_date, a.venueId
FROM    temp_date td
CROSS JOIN
        (
        SELECT  7 AS id
        UNION ALL
        SELECT  8 AS id
        ) ids
LEFT JOIN
        availability a
ON      a.date = td.query_date
        AND a.id = ids.id
ORDER BY
        ids.id, td.query_date

Also note that GROUP BY is not a correct method to handle duplicates here: in fact, your query would wail in any other system except MySQL, and even in MySQL with ANSI GROUP BY behavior enabled.

Are duplicates on (date, id) possible in availability? If yes, which of these duplicates should be selected?

佼人 2024-10-10 12:58:13

为什么不使用 IN() ?

SELECT temp_date.queryDate, availability.venueId
FROM temp_date
LEFT JOIN availability
ON temp_date.queryDate = availability.date
AND availability.venueId IN(7, 8)
GROUP BY temp_date.queryDate
ORDER BY temp_date.queryDate

Why not use IN() ?

SELECT temp_date.queryDate, availability.venueId
FROM temp_date
LEFT JOIN availability
ON temp_date.queryDate = availability.date
AND availability.venueId IN(7, 8)
GROUP BY temp_date.queryDate
ORDER BY temp_date.queryDate
旧情别恋 2024-10-10 12:58:13

是的,当然,正如 Webnet 所描述的,您可以使用 IN 语句来选择您的场地 ID。

SELECT temp_date.queryDate, availability.venueId

FROM temp_date
LEFT JOIN availability
ON temp_date.queryDate = availability.date
AND availability.venueId IN(7, 8)
GROUP BY temp_date.queryDate
ORDER BY temp_date.queryDate

yes,definitely, as Webnet describe you can use IN statement for selecting your venueID.

SELECT temp_date.queryDate, availability.venueId

FROM temp_date
LEFT JOIN availability
ON temp_date.queryDate = availability.date
AND availability.venueId IN(7, 8)
GROUP BY temp_date.queryDate
ORDER BY temp_date.queryDate
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文