简体   繁体   English

多维数组查询mysql

[英]multidimensional array query mysql

I am working in a hotel reservation system, and the rates are per night. 我正在酒店预订系统中,价格为每晚。 My database has two tables, one called stock and the other promotions. 我的数据库有两个表,一个表叫股票,另一个表促销。 The first contain one row for day (365 days), and the last one only the days that have a percent of discount. 第一行包含一天(365天)的一行,最后一行仅包含折扣百分比的天。 Sometimes, some days have two or more different percent of discount. 有时,有些日子会有两个或更多不同的折扣百分比。 In this case 30 and 31 of january have 0.10 and 0.15. 在这种情况下,一月的30和31具有0.10和0.15。

Table stock 表库存

date         | rate
--------------------------
2017-01-29     500
2017-01-30     500
2017-01-31     500

Table promotions 餐桌促销

date         | percent
--------------------------
2017-01-30     0.10
2017-01-31     0.10
2017-01-30     0.15
2017-01-31     0.15

while a mysql query (SELECT s.date, s.rate, IFNULL(p.percent, 0) FROM stock s LEFT JOIN promotions p ON s.date = p.date WHERE s.date as date BETWEEN '2017-01-29' AND '2017-01-31') i obtain this result: 而mysql查询(SELECT s.date,s.rate,IFNULL(p.percent,0)从股票s LEFT JOIN促销中p ON s.date = p.date s.date作为'2017-01-29之间的日期'AND'2017-01-31')我得到以下结果:

date         | rate       | percent
-----------------------------------
2017-01-29     500          0
2017-01-30     500          0.10
2017-01-31     500          0.10
2017-01-30     500          0.15
2017-01-31     500          0.15

i need to create a multidimensional array, that contain one array for each percent of discount, and these arrays contain the three days, the day 29 with a percent 0.00 and the other two days 0.10. 我需要创建一个多维数组,每个折扣百分比包含一个数组,这些数组包含三天,第29天的百分比为0.00,其他两天的值为0.10。 a new one the day 29 with a percent 0.00 and the other two days 0.15. 新的一天29的百分比为0.00,另两天的百分比为0.15。 i am tring but i cant assign the day 29 in both arrays. 我正在练习,但我不能在两个阵列中分配第29天。

Array
(
    [1] => Array
        (
            [0] => Array
                (
                    [date] => 2017-01-29
                    [rate]=>500
                    [percent] => 0.00
                )

            [1] => Array
                (
                    [date] => 2017-01-30
                    [rate]=>500
                    [percent] => 0.10
                )

            [2] => Array
                (
                    [date] => 2017-01-31
                    [rate]=>500
                    [percent] => 0.10
                )
        )

    [2] => Array
        (
            [0] => Array
                (
                    [date] => 2017-01-29
                    [rate]=>500
                    [percent] => 0.00
                )

            [1] => Array
                (
                    [date] => 2017-01-30
                    [rate]=>500
                    [percent] => 0.15
                )

            [2] => Array
                (
                    [date] => 2017-01-31
                    [rate]=>500
                    [percent] => 0.15

                )
        )
)

At its core, this feels like a database problem. 从本质上讲,这就像数据库问题。

Set the percent column as NOT NULL and set 0 as the DEFAULT. 将百分比列设置为NOT NULL,并将0设置为DEFAULT。

ALTER TABLE promotions CHANGE COLUMN percent [maintain-its-type] NOT NULL DEFAULT '0';

Then it is a matter of (if I understand the problem) plucking the MAX() and MIN() percent values for each date. 然后(如果我理解这个问题)就可以选择每个日期的MAX()和MIN()百分比值。

SELECT s.date, s.rate, MAX(p.percent) AS maxperc, MIN(p.percent) AS minperc
    FROM stock s
    LEFT JOIN promotions p ON s.date = p.date
    WHERE s.date BETWEEN '2017-01-29' AND '2017-01-31'
    GROUP BY s.date,s.rate;

...I haven't test that, so it may need some fiddling with. ...我还没有测试,所以可能需要摆弄一些。

Then as you loop through your result set, you can declare the two separate subarrays and build your complete array. 然后,当您遍历结果集时,可以声明两个单独的子数组并构建完整的数组。

$array=[];
$i=0;
while($row=mysqli_fetch_assoc($result)){
    $array[0][$i]["date"]=$row["date"];
    $array[0][$i]["rate"]=$row["rate"];
    $array[0][$i]["perc"]=$row["minperc"];
    $array[1][$i]["date"]=$row["date"];
    $array[1][$i]["rate"]=$row["rate"];
    $array[1][$i]["perc"]=$row["maxperc"];
    ++$i;
}

By this point, I've made too many assumptions about your purpose/usage. 至此,我对您的目的/用途做出了太多假设。 Basically, set zero as the percent default, query for highest and lowest percent for each date-rate pair (if zero, then zero will appear as highest and lowest value). 基本上,将零设置为默认百分比,查询每个日期率对的最高和最低百分比(如果为零,则零将显示为最高和最低值)。 Do whatever you wish with the result set. 对结果集做任何您想做的事情。

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

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