[英]IF statement SQL query within the SELECT in MySQL
I have the below stored procedure which works fine. 我有下面的存储过程,可以正常工作。
CREATE DEFINER=`root`@`localhost` PROCEDURE `TxCountByDatePerod`(IN `fromdate` DATE, IN `todate` DATE, IN `department` VARCHAR(20), IN `status` VARCHAR(10), IN `ipg` VARCHAR(20))
SELECT d.dept_name,
s.service_name,
COUNT(*) AS all_row_count
FROM department AS d
INNER JOIN service s ON s.dept_id=d.dept_id
INNER JOIN transaction_detail t ON s.dept_id=t.dept_id
AND s.service_id=t.service_id
WHERE t.tx_begin_date BETWEEN fromdate AND todate
AND (t.dept_id = department
OR department ='null')
AND (t.tx_status = status
OR status ='null')
AND (t.pg_name = ipg
OR ipg ='null')
GROUP BY t.dept_id,
t.service_id
Above SP is written in MySQL to get the number of transactions for a given time duration, provided by a particular service of a particular department. 上面的SP用MySQL编写,以获取由特定部门的特定服务提供的给定持续时间内的事务数。 A transactions can be 3 different transaction statuses(
tx_status
coulmn) which are completed(1), failed(-1), uncompleted(0). 一个事务可以是3个不同的事务状态(
tx_status
),它们分别是已完成(1),失败(-1),未完成(0)。
What I'm trying to do is get the data for the report below. 我想做的是获取下面报告的数据。
The right now the problem is that all the status are grouped into one row. 现在的问题是所有状态都分组为一行。 I tried the below SQL to get the data as needed but it gives out an error that the syntax is not correct.
我尝试了下面的SQL以根据需要获取数据,但是它给出了语法不正确的错误。
CREATE DEFINER=`root`@`localhost` PROCEDURE `TxCountByDatePerod`(IN `fromdate` DATE, IN `todate` DATE, IN `department` VARCHAR(20), IN `status` VARCHAR(10), IN `ipg` VARCHAR(20))
SELECT d.dept_name,
s.service_name,
COUNT(*) AS all_row_count,
IF(t.tx_status = 1,
SELECT COUNT(*)
FROM transaction_detail
WHERE tx_status = 1) AS successful_tx_count
FROM department AS d
INNER JOIN service s ON s.dept_id=d.dept_id
INNER JOIN transaction_detail t ON s.dept_id=t.dept_id
AND s.service_id=t.service_id
WHERE t.tx_begin_date BETWEEN fromdate AND todate
AND (t.dept_id = department
OR department ='null')
AND (t.tx_status = status
OR status ='null')
AND (t.pg_name = ipg
OR ipg ='null')
GROUP BY t.dept_id,
t.service_id
Please see the added line below. 请参见下面添加的行。
IF(t.tx_status = 1, SELECT COUNT(*) FROM transaction_detail WHERE tx_status = 1) AS successful_tx_count is added to the SP.
How can I fix this? 我怎样才能解决这个问题?
the correct syntax for using an IF statement with a select is like this 将IF语句与select配合使用的正确语法如下所示
CREATE DEFINER=`root`@`localhost` PROCEDURE `TxCountByDatePerod`(IN `fromdate` DATE, IN `todate` DATE, IN `department` VARCHAR(20), IN `status` VARCHAR(10), IN `ipg` VARCHAR(20))
SELECT d.dept_name,
s.service_name,
COUNT(*) AS all_row_count,
IF(t.tx_status = 1,
(SELECT COUNT(*) FROM transaction_detail WHERE tx_status = 1),
0
) AS successful_tx_count
FROM department AS d
INNER JOIN service s ON s.dept_id=d.dept_id
INNER JOIN transaction_detail t ON s.dept_id=t.dept_id
AND s.service_id=t.service_id
WHERE t.tx_begin_date BETWEEN fromdate AND todate
AND (t.dept_id = department
OR department ='null')
AND (t.tx_status = status
OR status ='null')
AND (t.pg_name = ipg
OR ipg ='null')
GROUP BY t.dept_id,
t.service_id
NOTE: 注意:
I just put in a default 0 value since you are doing a count but you can change that 我只是输入默认的0值,因为您正在进行计数,但是您可以更改该值
IF(t.tx_status = 1
,(SELECT COUNT(*) FROM transaction_detail WHERE tx_status = 1)
,0 -- # -- this can be anything.. you could put in NULL or whatever else if you want
) AS successful_tx_count
you can also use a user defined variable and plug that in like so 您还可以使用用户定义的变量并将其插入
SET @a := (SELECT COUNT(*) FROM transaction_detail WHERE tx_status = 1);
and then plug the @a variable in your if statement like so 然后像这样在@if语句中插入@a变量
if(t.tx_status = 1, @a, 0)
See this ARTICLE for more details on the if function 有关if函数的更多详细信息,请参见此文章
This is your select
statement: 这是您的
select
语句:
SELECT d.dept_name,
s.service_name,
COUNT(*) AS all_row_count,
IF(t.tx_status = 1,
SELECT COUNT(*)
FROM transaction_detail
WHERE tx_status = 1) AS successful_tx_count
Why are you using a subquery here? 为什么在这里使用子查询? I suspect that you want the "successful" count to match the same conditions (from the
where
clause) as the count(*)
. 我怀疑您希望“成功”计数匹配与
count(*)
相同的条件(来自where
子句count(*)
。 So, just use conditional aggregation: 因此,只需使用条件聚合:
SELECT d.dept_name,
s.service_name,
COUNT(*) AS all_row_count,
SUM(t.tx_status = 1) AS successful_tx_count
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.