简体   繁体   English

Mysql显示#1222-使用的SELECT语句具有不同数量的列

[英]Mysql shows #1222 - The used SELECT statements have a different number of columns

There has two tables (1)Sales and (2) sales items. 有两个表(1)销售和(2)销售项目。 sale_item table is as follows: sale_item表如下:

id|sale_id|product|quantity|real_unit_price| 
prod_type|food_sgst|liquor_sgst|food_cgst|liquor_cgst|food_igst|liquor_igst|item_sc
1 | 10000|XX1|02|100.00|0|7.25|0.00|7.25|0.00|0.00|0.00|4.0
2 | 10000|XX2|03|100.00|0|7.25|0.00|7.25|0.00|0.00|0.00|4.0
3 | 10000|XX3|02|100.00|0|7.25|0.00|7.25|0.00|0.00|0.00|4.0
4 | 10000|XX4|07|100.00|0|7.25|0.00|7.25|0.00|0.00|0.00|4.0
5 | 10001|XXX|02|100.00|0|7.25|0.00|7.25|0.00|0.00|0.00|4.0
6 | 10002|XX4|02|100.00|0|7.25|0.00|7.25|0.00|0.00|0.00|4.0 
7 | 10002|XX5|02|100.00|0|7.25|0.00|7.25|0.00|0.00|0.00|4.0
8 | 10002|XX5|02|100.00|0|7.25|0.00|7.25|0.00|0.00|0.00|4.0

sale table is as follows: 销售表如下:

id|date|customer_name|total_discount
10000|2019-02-19|YYYY|20
10001|2019-02-19|YYYY|10
10002|2019-02-19|YYYY|20

In the above example you have selling 4 items in a bill (sale id 10000 ) and put discount on overall bill. 在上面的示例中,您在一个账单中出售了4个项目(销售ID 10000),并对总账单进行了折扣。 All items will store in sales items table and bill details will stored in sales table.I have written a SQL query. 所有项目将存储在销售项目表中,而帐单明细将存储在销售表中。我编写了一个SQL查询。 If I use the following sql query: 如果我使用以下sql查询:

SELECT sales.id as sale_id, DATE_FORMAT(sales.date, '%e' ) AS date,SUM( (line.quantity)*(line.real_unit_price) ) AS amt, SUM(line.food_sgst+line.liquor_sgst) AS sgst, SUM(line.food_cgst+line.liquor_cgst) AS cgst, SUM(line.food_igst+line.liquor_igst) AS igst, SUM(line.item_sc) AS i_sc, SUM( DISTINCT( total_discount) ) AS discount, SUM(((line.quantity)*(line.real_unit_price))+(line.food_sgst+line.liquor_sgst)+(line.food_cgst+line.liquor_cgst)+(line.food_igst+line.liquor_igst)+line.item_sc) AS total FROM sale_items as line LEFT JOIN sales as sales ON line.sale_id = sales.id WHERE created_by = 17 AND DATE_FORMAT(date, '%Y-%m') = '2019-02' GROUP BY DATE_FORMAT( date, '%e')

Then the problem is that same discount amount in different sales id it did not return proper value. 那么问题是,不同销售标识中的相同折扣金额未返回正确的值。 Suppose there has 4 sales id. 假设有4个销售ID。 100,101,102 and 103 and the discounts are $10, $10,$15 and $10. 100,101,102和103,折扣分别为$ 10,$ 10,$ 15和$ 10。 So total discount is $45. 因此总折扣为$ 45。 But it shows only $25. 但它仅显示$ 25。 To avoid this problem write the query is as follows: 为避免此问题,请编写查询,如下所示:

select * from (
                (SELECT sales.id as sale_id, DATE_FORMAT(sales.date, '%e' ) AS date,SUM( (line.quantity)*(line.real_unit_price) ) AS amt, SUM(line.food_sgst+line.liquor_sgst) AS sgst, SUM(line.food_cgst+line.liquor_cgst) AS cgst, SUM(line.food_igst+line.liquor_igst) AS igst, SUM(line.item_sc) AS i_sc, SUM(((line.quantity)*(line.real_unit_price))+(line.food_sgst+line.liquor_sgst)+(line.food_cgst+line.liquor_cgst)+(line.food_igst+line.liquor_igst)+line.item_sc) AS total FROM sale_items as line LEFT JOIN sales as sales ON line.sale_id = sales.id WHERE created_by = 17 AND DATE_FORMAT(date, '%Y-%m') = '2019-02' GROUP BY DATE_FORMAT( date, '%e')
               )union all(
                   SELECT SUM(a.total_discount) as total_discount from sales a where DATE_FORMAT(a.date, '%Y-%m' ) = '2019-02' GROUP BY DATE_FORMAT(a.date, '%e' ) order by a.id)
               )as salesall 

It shows the error 显示错误

1222 - The used SELECT statements have a different number of columns. 1222-使用的SELECT语句具有不同数量的列。

Please help me to solve this problem. 请帮我解决这个问题。

you should have same number of columns for UNION/UNION ALL 您应该为UNION/UNION ALL使用相同数量的列

SELECT sales.id as sale_id, DATE_FORMAT(sales.date, '%e' ) AS date,SUM( (line.quantity)*(line.real_unit_price) ) AS amt, SUM(line.food_sgst+line.liquor_sgst) AS sgst, SUM(line.food_cgst+line.liquor_cgst) AS cgst, 
SUM(line.food_igst+line.liquor_igst) AS igst, 
SUM(line.item_sc) AS i_sc, SUM( DISTINCT( total_discount) ) AS discount, SUM(((line.quantity)*(line.real_unit_price))+(line.food_sgst+line.liquor_sgst)+(line.food_cgst+line.liquor_cgst)+(line.food_igst+line.liquor_igst)+line.item_sc) AS total 
FROM sale_items as line LEFT JOIN sales as sales ON line.sale_id = sales.id WHERE created_by = 17 AND DATE_FORMAT(date, '%Y-%m') = '2019-02' 
GROUP BY DATE_FORMAT( date, '%e')
union all
SELECT null,null,null,null,null,null,null,null,SUM(a.total_discount) as total_discount 
from sales a where DATE_FORMAT(a.date, '%Y-%m' ) = '2019-02' 
GROUP BY DATE_FORMAT(a.date, '%e' ) order by a.id

Given your sample data and your query I guess you you want 1 row returned per day 根据您的样本数据和查询,我想您希望每天返回1行

drop table if exists t,t1;
create table t
(id int,sale_id int,product varchar(3),quantity int,real_unit_price decimal(10,2), 
prod_type int, food_sgst decimal(10,2),liquor_sgst decimal(10,2),food_cgst decimal(10,2),liquor_cgst decimal(10,2),
food_igst decimal(10,2),liquor_igst decimal(10,2),item_sc decimal(10,2));
insert into t values
(1 , 10000,'XX1',02,100.00,0,7.25,0.00,7.25,0.00,0.00,0.00,4.0),
(2 , 10000,'XX2',03,100.00,0,7.25,0.00,7.25,0.00,0.00,0.00,4.0),
(3 , 10000,'XX3',02,100.00,0,7.25,0.00,7.25,0.00,0.00,0.00,4.0),
(4 , 10000,'XX4',07,100.00,0,7.25,0.00,7.25,0.00,0.00,0.00,4.0),
(5 , 10001,'XXX',02,100.00,0,7.25,0.00,7.25,0.00,0.00,0.00,4.0),
(6 , 10002,'XX4',02,100.00,0,7.25,0.00,7.25,0.00,0.00,0.00,4.0),
(7 , 10002,'XX5',02,100.00,0,7.25,0.00,7.25,0.00,0.00,0.00,4.0),
(8 , 10002,'XX5',02,100.00,0,7.25,0.00,7.25,0.00,0.00,0.00,4.0);


create table t1
(id int,date date,customer_name varchar(4),total_discount int);
insert into t1 values
(10000,'2019-02-19','YYYY',20),
(10001,'2019-02-19','YYYY',10),
(10002,'2019-02-19','YYYY',20);

In which case aggregate the 2 tables separately and join the results 在这种情况下,将两个表分别汇总并合并结果

select b.*,a.total_discount
from
(
select DATE_FORMAT(sales.date, '%e' ) dt, sum(total_discount) total_discount
from t1 sales
group by DATE_FORMAT(sales.date, '%e' )
) a
left join
(
select DATE_FORMAT(sales.date, '%e' ) AS date,
        SUM( (line.quantity)*(line.real_unit_price) ) AS amt, 
        SUM(line.food_sgst+line.liquor_sgst) AS sgst, 
        SUM(line.food_cgst+line.liquor_cgst) AS cgst, 
        SUM(line.food_igst+line.liquor_igst) AS igst, 
        SUM(line.item_sc) AS i_sc, 
        SUM(((line.quantity)*(line.real_unit_price))+(line.food_sgst+line.liquor_sgst)+(line.food_cgst+line.liquor_cgst)+(line.food_igst+line.liquor_igst)+line.item_sc) AS total 
FROM t as line 
left join t1 as sales on line.sale_id = sales.id
group by DATE_FORMAT(sales.date, '%e' )
) b
on a.dt = b.date;

+------+---------+-------+-------+------+-------+---------+----------------+
| date | amt     | sgst  | cgst  | igst | i_sc  | total   | total_discount |
+------+---------+-------+-------+------+-------+---------+----------------+
| 19   | 2200.00 | 58.00 | 58.00 | 0.00 | 32.00 | 2348.00 |             50 |
+------+---------+-------+-------+------+-------+---------+----------------+
1 row in set (0.00 sec)

Aggregating by row looks dangerous unless you only have 1 months worth of data. 除非您只有1个月的数据量,否则按行进行聚合看起来很危险。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 #1222 - 使用的 SELECT 语句具有不同的列数 - #1222 - The used SELECT statements have a different number of columns 在 mysql 中调用过程时出错:错误代码:1222。使用的 SELECT 语句具有不同的列数 - error when I call procedure in mysql:Error Code: 1222. The used SELECT statements have a different number of columns 错误代码:1222。使用的 SELECT 语句具有不同的列数 - Error Code: 1222. The used SELECT statements have a different number of columns MYSQL 使用的 SELECT 语句有不同的列数 - MYSQL The used SELECT statements have a different number of columns 使用的选择语句具有不同数量的mysql列 - the used select statements have a different number of columns mysql MySQL错误:使用的SELECT语句的列数不同 - MySQL Error: The used SELECT statements have a different number of columns MySQL:联接,使用的SELECT语句的列数不同 - MySQL: joins, The used SELECT statements have a different number of columns 使用的SELECT语句具有不同的列数 - The used SELECT statements have a different number of columns 使用的 SELECT 语句在 codeigniter 中具有不同的列数 - The used SELECT statements have a different number of columns in codeigniter 触发器更新错误 - 使用的SELECT语句具有不同的列数 - Trigger updation error - The used SELECT statements have a different number of columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM