开发者

Creating a grid of data with php/mysql - union queries?

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.queryDat开发者_如何学运维e = 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:

Creating a grid of data with php/mysql - union queries?

Creating a grid of data with php/mysql - union queries?

Using IN() as suggested:

Creating a grid of data with php/mysql - union queries?


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?


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜