天天看點

mysql 多元數組_多元數組查詢mysql

mysql 多元數組_多元數組查詢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. 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.

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:

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. a new one the day 29 with a percent 0.00 and the other two days 0.15. i am tring but i cant assign the day 29 in both arrays.

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.

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.

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.