1

私は今ちょっと立ち往生しており、助けていただければ幸いです。「月」と「価格」の2つの列を持つMysqlテーブルがあります。つまり、1月>> 5(ユーロ)です。2月 >> 7 (ユーロ) など。ウェブサイトの予約フォームで、ユーザーは到着日と出発日を選択できます。ユーザーが 2013 年 1 月 2013 日から 2013 年 12 月 4 日までの間に月がある日付を選択した場合、その間の完全な月 (02 と 03) をどのように計算できますか?

これは、到着月と出発月の日数を計算するために必要なものです。

 ...
 $query="SELECT * FROM pricecal";
 $result=mysql_query($query);
 $num=mysql_numrows($result);

 $i=0;

 while ($i < $num)
  {

$month=mysql_result($result,$i,"month");
$price=mysql_result($result,$i,"price");

$i++;

$a = $_POST['dropToDay'];
$b = $_POST['dropToMonth'];
$c = $_POST['dropToYear'];
$d = $_POST['dropOffDay'];
$e = $_POST['dropOffMonth'];
$f = $_POST['dropOffYear'];

$days_to = (date('t',mktime(0, 0, 0, $a,(date($b) +1), date($c))) - $a +1);
$off_month_dif = (date('d-m-Y',mktime(0, 0, 0, $d,(date($e)), date($f))) - $d);
$days_off = (date('d-m-Y',mktime(0, 0, 0, $d,(date($e)), date($f))) - $off_month_dif);
$days = (date($f.$e.$d) - date($c.$b.$a));
$days_all = ($days+1);

if($b==$e AND $b==$month)
 {
  $b = $month;
  $pricing = $days_all*$price;
  echo 'Price for '.$days_all.' days is '.$pricing.' EUR.';
 }

if($b == $month AND $b != $e)
 {
  $pricing1 = $days_to*$price;
 }
if($e == $month AND $b != $e)
 {
  $pricing = $days_off*$price;
  $pricings_all = $pricing+$pricing1;
  echo 'Price for booking time: '.$pricings_all.' EUR';
 }
 .....
4

3 に答える 3

0

大声で仮定するだけです。これはあなたが探しているものですか?この方法で何か?

SQLFIDDLE MYSQL DEMO

select months, price
from demo, (select @i:=-1) r
where months = 
  date_format(
    Date_Add(Date '2013.01.13',
             interval @i:=@i+1 month),'%b')
having @i <= period_diff(date_format
(Date '2013-04-12', '%Y%m'),
date_format(Date '2013-01-20', '%Y%m'))
;

| MONTHS | PRICE |
------------------
|    Jan |     5 |
|    Feb |     7 |
|    Mar |     9 |
|    Apr |    10 |
于 2013-02-02T18:41:37.717 に答える
0

月と価格の配列を作成します (数値の月を格納していると想定しています)。

$price = array();
$query = "SELECT month, price FROM pricecal";
$result = mysql_query($query) or die(mysql_error()); 
while($row = mysql_fetch_assoc($result)) {
    $price[$result["month"]] = $result["price"];
}
// array(
// 1 => 5,
// 2 => 7,
// ...
// )

次に、2 つのタイムスタンプを作成し、それらをループして合計します。

$ts1 = mktime(0, 0, 0, $_POST['dropToMonth'],  $_POST['dropToDay'],  $_POST['dropToYear']);
$ts2 = mktime(0, 0, 0, $_POST['dropOffMonth'], $_POST['dropOffDay'], $_POST['dropOffYear']);

for($ts = $ts1, $sum = 0; $ts <= $ts2; $ts = strtotime("+1 day", $ts)) {
    $sum += $price[date("n", $ts)];
}
于 2013-02-02T21:28:40.950 に答える
0

これにより、1 つのクエリで 2 つの日付間の合計料金が取得されます。最も内側のサブクエリは少し恐ろしく見えますが、範囲の間に毎日の行を作成するだけです。次に、月ごとにグループ化します。

デモ: http://sqlfiddle.com/#!2/53b3b/1

出力:

447

クエリ:

SET @beginDate = '2013-01-20';
SET @endDate   = '2013-04-12';

SELECT
    SUM( days.days * rate.price ) AS fee

FROM
    rate

    INNER JOIN
    ( SELECT 
        MONTH( a.Date ) AS month,
        COUNT( * )      AS days

    FROM (
        select @endDate - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY as Date
        from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
        cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
        cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
        ) a

    WHERE 
        a.Date BETWEEN @beginDate AND @endDate 

    GROUP BY
        MONTH( a.Date )

    ) AS days
    ON rate.month = days.month

スキーマ:

CREATE TABLE 
    rate 
    (
    month INT, 
    price INT
    );

INSERT INTO 
    rate
    ( 
    month, 
    price
    )

VALUES
    ( 1, 5 ),
    ( 2, 7 ),
    ( 3, 5 ),
    ( 4, 3 );
于 2013-02-02T21:59:42.233 に答える