查询总数减少百分比

时间:2017-11-14 08:25:04

标签: mysql sql

嘿伙计我有一些查询问题。我希望用百分比减少总数,但是这段代码给了我一些错误:

SELECT `id_sewa`,`nama_villa`,DATE_FORMAT(`tgl_transaksi`, '%d-%m-%Y'),`nama_tamu`,`cek_in`,`cek_out`, datediff(`cek_out`,`cek_in`) as rn,format(`harga`, 0),format(datediff(`cek_out`,`cek_in`) *`harga`, 0)as total,`ref`,
(
CASE
WHEN ref = "direct" THEN format(datediff(`cek_out`,`cek_in`) * `harga`, 0) - 10%
END) AS total_bersih

from sewa_villa
where tgl_transaksi BETWEEN '2017-11-12' and '2017-11-14';

我的期望:

<table border =1>
<tr>
<th>total</th>
<th>ref</th>
<th>total bersih</th>
</tr>
<tr>
<th>2,000,000</th>
<th>direct</th>
<th>1,800,000</th>
</table>

这个错误:

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') AS total_bersih
from sewa_villa where tgl_transaksi BETWEEN '2017-11-12' and ' at line 6

2 个答案:

答案 0 :(得分:1)

您无法直接在MySQL上使用百分比计算(使用UserViewModel之类的东西)!您必须使用我在the comments中提到的- 10%替换- 10%

* 0.9

SELECT `id_sewa`, `nama_villa`, DATE_FORMAT(`tgl_transaksi`, '%d-%m-%Y'), `nama_tamu`, `cek_in`, `cek_out`, DATEDIFF(`cek_out`,`cek_in`) AS rn, FORMAT(`harga`, 0), FORMAT(DATEDIFF(`cek_out`,`cek_in`) * `harga`, 0) AS total, `ref`, ( CASE WHEN ref = "direct" THEN FORMAT(DATEDIFF(`cek_out`, `cek_in`) * `harga`, 0) * 0.9 END) AS total_bersih FROM sewa_villa WHERE tgl_transaksi BETWEEN '2017-11-12' AND '2017-11-14'; 可以使用如下:

  • 通配符运算符(%
  • modulo operationcol_name LIKE '%test%')上。

在您的情况下:MySQL尝试计算模运算,但col_name % 5之后的数字丢失。这就是MySQL抛出错误的原因。

答案 1 :(得分:0)

问题是你的“-10%”:

SELECT `id_sewa`,`nama_villa`,DATE_FORMAT(`tgl_transaksi`, '%d-%m-%Y'),`nama_tamu`,`cek_in`,`cek_out`, datediff(`cek_out`,`cek_in`) as rn,format(`harga`, 0),format(datediff(`cek_out`,`cek_in`) *`harga`, 0)as total,`ref`,
(
CASE
WHEN ref = "direct" THEN format(datediff(`cek_out`,`cek_in`) * `harga`, 0) *0.9
END) AS total_bersih

from sewa_villa
where tgl_transaksi BETWEEN '2017-11-12' and '2017-11-14';

只是一个注意事项:你没有把任何其他东西放在这个/什么时候,你确定吗?