求MYSQL汇总语句

求MYSQL汇总语句,第1张

select 商品名称,

sum(数量) as 总量,sum(金额) as 总金额,

sum(case 线号 when '1号线' then 数量 else 0 end) as 1号线数量,

sum(case 线号 when '2号线' then 数量 else 0 end) as 2号线数量,

sum(case 线号 when '3号线' then 数量 else 0 end) as 3号线数量,

sum(case 线号 when '4号线' then 数量 else 0 end) as 4号线数量,

sum(case 线号 when '1号线' then 金额 else 0 end) as 1号线金额,

sum(case 线号 when '2号线' then 金额 else 0 end) as 2号线金额,

sum(case 线号 when '3号线' then 金额 else 0 end) as 3号线金额,

sum(case 线号 when '4号线' then 金额 else 0 end) as 4号线金额

from

(

select 商品名称,线号,sum(数量) as 数量,sum(金额) as 金额

from Table1

group by 商品名称,线号

) T1

group by 商品名称

使用UNION

select DEPT_NAME, USER_NAME, PHONE, CONTENT, SEND_TIME from sms3

UNION ALL

select DEPT_NAME,SENDER_NAME,MOBIL_NO ,CONTENT,DIA_TIME from diary

在每个select 语句中可以使用where 条件指定数据

两个表的对应列类型最好一致

SELECT ...

UNION [ALL | DISTINCT] SELECT ...

[UNION [ALL | DISTINCT] SELECT ...]

UNION is used to combine the result from multiple SELECT statements into a single result set.

The column names from the first SELECT statement are used as the column names for the results returned. Selected columns listed in corresponding positions of each SELECT statement should have the same data type. (For example, the first column selected by the first statement should have the same type as the first column selected by the other statements.)

使用方法步骤如下:

1、工具需求

(1)navicat for mysql

2、先下载安装软件,按照正确的软件安装步骤进行安装,安装过程之间注意一下插件的自定义选择。

(1)连接参数填写:连接名可以任意填写,主需要方便区分即可,接着在主机名或IP填写服务器的主机名或者服务器IP地址,

(2)然后端口默认是3306  如果修改了其他端口,需要对应,接着填写用户名的密码,如图。

3、下面设置完后,点击下方的“连接”,接着出现“连接成功”的d窗,意味着连接成功了。

(1)添加删除数据库:如果需要添加删除数据库的话,可以选择需要 *** 作的数据库点击 *** 作即可,如图。

(2)如何使用命令行:,对于很多 *** 作还是SQL命令会更加方便,接着进入sql命令行界面,点击“工具”--“命令列界面”。

(3)可以在右下方空白区域进入了SQL命令行界面, *** 作方法和普通进入mysql命令行界面一样使用命令。

4、其他功能:面对之前数据库已经导出的sql文件。直接通过选择需要导入的数据库,效果如图所示。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/8550665.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-17
下一篇2023-04-17

发表评论

登录后才能评论

评论列表(0条)

    保存