0

很抱歉,很长的帖子......

我有这个表格,用户输入一个startdate和一个enddate结果将被发送到一个 PHP 文件,该文件正在生成这样的结果变量:
PHP

if(isset($_POST['user_selected_dates'])) {
    $invoice_startdate = $_POST['start_date'];
    $invoice_enddate = $_POST['end_date'];
}

然后是这个讨厌的 MySQL 查询......

SELECT

  s.student_socialnr, s.student_lastname, s.student_firstname,
  cs.city_name,
  c.customer_name,
  scp.cpl_startdate, scp.cpl_enddate, scp.cpl_coursename, scp.cpl_pricevalue,
  SUM(scpe.scpe_days) AS total_days, scp.cpl_pricevalue * SUM(scpe.scpe_days) AS      total_invoice

 FROM studentcourseplan scp
   INNER JOIN students s ON s.student_id = scp.student_id
   INNER JOIN studentcourseplanelements scpe ON scpe.scpe_cpl_id = scp.cpl_id
   INNER JOIN cityselections cs ON cs.city_id = s.student_city_id
   INNER JOIN customers c ON c.customer_id = s.student_customer_id
 WHERE scp.cpl_startdate BETWEEN '$invoice_startdate' AND '$invoice_enddate'

 GROUP BY scp.cpl_id ORDER BY s.student_lastname ASC

这将在表中输出类似这样的内容(使用 PHP mysql_fetch_array):

+------------------+------------------+-------------------+---------------+---------------+---------------+----------------+----------------+------------+---------------+
| student_socialnr | student_lastname | student_firstname | customer_name | cpl_startdate | cpl_enddate   | cpl_coursename | cpl_pricevalue | total_days | total_invoice |
+------------------+------------------+-------------------+---------------+---------------+---------------+----------------+----------------+------------+---------------+
| 000000-0000      | Doe              | John              | A name        | 2012-06-01    | 2012-06-30    | A coursename   | 620            | 40         | 24800         |
+------------------+------------------+-------------------+---------------+---------------+---------------+----------------+----------------+------------+---------------+
| 000000-0000      | Clerksson        | Lisa              | A name        | 2012-01-05    | 2012-07-30    | A coursename   | 950            | 10         | 9500          |
+------------------+------------------+-------------------+---------------+---------------+---------------+----------------+----------------+------------+---------------+
| 000000-0000      | Derpson          | Derp              | A name        | 2012-04-01    | 2012-04-05    | A coursename   | 620            | 40         | 24800         |
+------------------+------------------+-------------------+---------------+---------------+---------------+----------------+----------------+------------+---------------+

现在我正在寻找一个可以执行以下操作的函数:为和

之间的天数创建一个值。如果该值较小,则列中结果的值进行计算:列* [新值的结果]并将其输出到每一行(但前提是它小于数据库中的值...)也许它可以在-query 中完成吗?我真的很想得到一些帮助。这是我第一个使用 PHP 和 MySQL 的网站,请多多包涵!谢谢。$invoice_startdate$invoice_enddate

total_days

price_value

MySQL



4

2 回答 2

0

我通过在查询中添加以下代码自己解决了这个问题:

CASE WHEN SUM(scpe.scpe_days) > DATEDIFF('$invoice_enddate', '$invoice_startdate') THEN
 (scp.cpl_pricevalue * DATEDIFF('$invoice_enddate', '$invoice_startdate')) ELSE SUM(scpe.scpe_days) * scp.cpl_pricevalue
END AS to_invoice
于 2012-06-16T19:07:45.363 回答
0

我相信您正在考虑使用 MySQL IF() 和 TO_DAYS() 函数的组合。所以是这样的:

SELECT IF( TO_DAYS(DATE_DIFF('$invoice_enddate', '$invoice_startdate')) < total_days, price_value * ... ) as total_invoice
于 2012-06-16T16:58:03.303 回答