[英]MySQL 1241 error
I have 2 independent queries which are calculating the total IN/OUT of payments for a given festival. 我有2个独立的查询,用于计算给定节日的付款总收入。 The IN/OUT is given by a ENUM value (see below). IN / OUT由一个ENUM值给出(见下文)。
If I run the queries independently it gives the correct output of SUM based on the selected payment_type IN or OUT. 如果我独立运行查询,它将根据所选的payment_type IN或OUT提供正确的SUM输出。 My problem is when I try to combine them in one query to have 2 separate columns for IN/OUT like below. 我的问题是,当我尝试将它们合并到一个查询中时,有2个单独的IN / OUT列,如下所示。
I have error in MySQL "Operand should contain 1 column(s)". 我在MySQL中出现错误“操作数应包含1列”。 After I've done some research I believe is the subqueries are wrong but I'm not quite sure how to solve it. 经过一些研究后,我相信子查询是错误的,但我不确定如何解决。
Thanks in advance for any help... 预先感谢您的任何帮助...
TOTAL IN 总计
SELECT
SUM(`payment`.`pmt_amount`) AS `TOTAL IN`
, `payment`.`pmt_type`
, `festival`.`id_festival`
FROM
payment
INNER JOIN festival
ON (`payment`.`id_festival` = `festival`.`id_festival`)
WHERE (`payment`.`pmt_type` LIKE '%IN'
AND `festival`.`id_festival` = 1);
And TOTAL OUT 和总计
SELECT
SUM(`payment`.`pmt_amount`) AS `TOTAL OUT`
, `payment`.`pmt_type`
, `festival`.`id_festival`
FROM
payment
INNER JOIN festival
ON (`payment`.`id_festival` = `festival`.`id_festival`)
WHERE (`payment`.`pmt_type` LIKE '%OUT'
AND `festival`.`id_festival` = 1);
Combined 综合
SELECT
festival.id_festival,
payment.pmt_amount,
payment.pmt_type,
(SELECT
payment.pmt_type,
SUM(payment.pmt_amount) AS `TOTAL OUT`
FROM payment
WHERE payment.pmt_type LIKE '%OUT'),
(SELECT
payment.pmt_type,
SUM(payment.pmt_amount) AS `TOTAL IN`
FROM payment
WHERE payment.pmt_type LIKE '%IN')
FROM payment
INNER JOIN festival
ON payment.pmt_amount = festival.id_festival
WHERE festival.id_festival = 1
Your subquery must return only one column
but you query is returning two column
which is wrong try below mention query: 您的子查询only one column
返回only one column
但您的查询返回的是two column
,请尝试以下提及的查询:
SELECT
festival.id_festival,
payment.pmt_amount,
payment.pmt_type,
(SELECT
SUM(payment.pmt_amount) AS `TOTAL OUT`
FROM payment
WHERE payment.pmt_type LIKE '%OUT') AS `TOTAL OUT`,
(SELECT
SUM(payment.pmt_amount) AS `TOTAL IN`
FROM payment
WHERE payment.pmt_type LIKE '%IN') AS `TOTAL IN`
FROM payment
INNER JOIN festival
ON payment.pmt_amount = festival.id_festival
WHERE festival.id_festival = 1
You probably just want to use conditional aggregation here: 您可能只想在此处使用条件聚合:
SELECT
f.id_festival,
SUM(CASE WHEN p.pmt_type = 'Payment IN' THEN p.pmt_amount ELSE 0 END) AS `TOTAL IN`,
SUM(CASE WHEN p.pmt_type = 'Payment OUT' THEN p.pmt_amount ELSE 0 END) AS `TOTAL OUT`
FROM festival f
INNER JOIN payment p
ON p.id_festival = f.id_festival
WHERE f.id_festival = 1
GROUP BY
f.id_festival;
Note that your query is only looking at one festival, but the correct way to express this is via GROUP BY
even if we only want to retain a single group in the output. 请注意,您的查询仅查看一个节日,但是表达这一点的正确方法是通过GROUP BY
即使我们只想在输出中保留一个组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.