mysql 一个表自连查询数据?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql 一个表自连查询数据?相关的知识,希望对你有一定的参考价值。
需要列显示查询出,1收入笔数,2最大收入说明及金额,3最小收入说明及金额,4支出笔数,5最大支出说明及金额,6最小支出说明及金额7本月结余,type值1为支出,2为收入
建表和视图语句:
DROP TABLE IF EXISTS `tab`;
CREATE TABLE `tab` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userid` int(11) NULL DEFAULT NULL,
`date` datetime NULL DEFAULT NULL,
`instructions` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`amount` decimal(18, 2) NULL DEFAULT NULL,
`type` tinyint(1) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = MyISAM AUTO_INCREMENT = 9 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
create view tab_v as
select
`tab`.`userid` AS `userid`,
date_format( `tab`.`date`, '%Y-%m' ) AS `ym`,
`tab`.`instructions` AS `instructions`,
`tab`.`amount` AS `amount`,
`tab`.`type` AS `type`
from
`tab`
查询语句:
select t0.userid 用户ID,
t0.ym 年月,
t1.cnt 本月收入笔数,
t2.instructions 本月最大收入项目,
t2.amount 本月最大收入金额,
t3.instructions 本月最小收入项目,
t3.amount 本月最小收入金额,
t4.cnt 本月支出笔数,
t5.instructions 本月最大支出项目,
t5.amount 本月最大支出金额,
t6.instructions 本月最小支出项目,
t6.amount 本月最小支出金额,
t7.cnt 本月结余
from (select distinct a.userid,
a.ym
from tab_v a) t0
left join (select a.userid,
a.ym,
count(1) cnt
from tab_v a
where a.type = 2
group by a.userid,
a.ym) t1
on t0.userid = t1.userid
and t0.ym = t1.ym
left join (select a.userid,
a.ym,
a.amount,
group_concat(b.instructions) instructions
from (select a.userid,
a.ym,
max(a.amount) amount
from tab_v a
where a.type = 2
group by a.userid,
a.ym) a,
tab_v b
where a.userid = b.userid
and a.ym = b.ym
and a.amount = b.amount
group by a.userid,
a.ym,
a.amount) t2
on t0.userid = t2.userid
and t0.ym = t2.ym
left join (select a.userid,
a.ym,
a.amount,
group_concat(b.instructions) instructions
from (select a.userid,
a.ym,
min(a.amount) amount
from tab_v a
where a.type = 2
group by a.userid,
a.ym) a,
tab_v b
where a.userid = b.userid
and a.ym = b.ym
and a.amount = b.amount
group by a.userid,
a.ym,
a.amount) t3
on t0.userid = t3.userid
and t0.ym = t3.ym
left join (select a.userid,
a.ym,
count(1) cnt
from tab_v a
where a.type = 1
group by a.userid,
a.ym) t4
on t0.userid = t4.userid
and t0.ym = t4.ym
left join (select a.userid,
a.ym,
a.amount,
group_concat(b.instructions) instructions
from (select a.userid,
a.ym,
max(a.amount) amount
from tab_v a
where a.type = 1
group by a.userid,
a.ym) a,
tab_v b
where a.userid = b.userid
and a.ym = b.ym
and a.amount = b.amount
group by a.userid,
a.ym,
a.amount) t5
on t0.userid = t5.userid
and t0.ym = t5.ym
left join (select a.userid,
a.ym,
a.amount,
group_concat(b.instructions) instructions
from (select a.userid,
a.ym,
min(a.amount) amount
from tab_v a
where a.type = 1
group by a.userid,
a.ym) a,
tab_v b
where a.userid = b.userid
and a.ym = b.ym
and a.amount = b.amount
group by a.userid,
a.ym,
a.amount) t6
on t0.userid = t6.userid
and t0.ym = t6.ym
left join (select a.userid,
a.ym,
sum(case
when type = 1 then
-1 * a.amount
when 2 then
a.amount
end) cnt
from tab_v a
group by a.userid,
a.ym) t7
on t0.userid = t7.userid
and t0.ym = t7.ym
只能做到这个效果了
参考技术A一,内连接查询
是指所有查询出的结果都是能够在连接的表中有对应记录的。
以t_employee(员工表)和t_dept(部门表)为例:
t_employee表中的记录如下:dept代表该员工所在的部门
t_dept表中记录如下:
参考技术B 具体步骤如下:
选中试卷的题目和答案部分,然后单击“插入-表格-文字转换成表格”按钮。如下图 1 处所示:
然后注意,本例的每一个选择题由五个段落组成。所以我们要强制把表格转换成五列。如下图 2 处所示:
然后Word试卷就会变成以下样子:
接下来就简单多了,只要选中表格,复制到Excel就好了。
天蓝色本地存储表自定义字段为空
【中文标题】天蓝色本地存储表自定义字段为空【英文标题】:azure local storage table custom fields are null 【发布时间】:2021-06-29 04:08:05 【问题描述】:我正在尝试创建一个通用插入方法,我从抽象 TableEntity 类生成一个类。
class STCompany : TableEntity
string Abbrev;
string Name;
string NavName;
public STCompany(
string partitionKey, string rowKey, DateTime timeStamp,
string abbrev, string name, string navName
)
PartitionKey = partitionKey;
RowKey = rowKey;
Timestamp = timeStamp;
Abbrev = abbrev;
Name = name;
NavName = navName;
尝试在这个静态类中创建通用插入方法(参见代码块底部的插入任务)
public static class lclStorage
private static string paramsConstring = __paramsConstring;
private static CloudStorageAccount storageAcc;
private static CloudTableClient tblclient;
private static CloudTable get_params_table (string table_name)
if(storageAcc == null) storageAcc = CloudStorageAccount.Parse(paramsConstring);
if(tblclient == null) tblclient = storageAcc.CreateCloudTableClient(new TableClientConfiguration());
return tblclient.GetTableReference(table_name);
public static class cloudTables
public static CloudTable AccessUsersShareholders = get_params_table("AccessUsersShareholders");
public static CloudTable CompanyPropertyMapping = get_params_table("CompanyPropertyMapping");
public static CloudTable STCompanies = get_params_table("STCompanies");
public static CloudTable STdepartments = get_params_table("STdepartments");
public static async Task<(int statusCode, string response, bool success)> Insert(CloudTable p_tbl, TableEntity te)
TableOperation insertOperation = TableOperation.InsertOrMerge(te);
TableResult result = await p_tbl.ExecuteAsync(insertOperation);
Console.WriteLine("Record Added");
return (
result.HttpStatusCode,
(result.HttpStatusCode == 204) ? "Record added" : "Failed to add record",
result.HttpStatusCode == 204);
然后在我的主程序中运行插入
var result = await lclStorage.Insert(
lclStorage.cloudTables.STCompanies,
new STCompany( "STCompanies", "TE3", DateTime.Now, "TE3", "test", "nav__test")
);
Console.WriteLine($"result.response __ result.success");
TableResult result
中的字段显示正确
但在我的 Azure 表中,字段 Abbrev、Name 和 NavName 设置为 null。
这是因为我在插入函数中将TableEntity
声明为类型而不是STCompany
,还是我做错了什么?
【问题讨论】:
【参考方案1】:所以 TableEntity 类中的变量Abbrev,Name,NavName
必须是公共的。
class STCompany : TableEntity
public string Abbrev get; set;
public string Name get; set;
public string NavName get; set;
public STCompany(
string partitionKey, string rowKey,
string abbrev, string name, string navName
)
PartitionKey = partitionKey;
RowKey = rowKey;
Abbrev = abbrev;
Name = name;
NavName = navName;
【讨论】:
以上是关于mysql 一个表自连查询数据?的主要内容,如果未能解决你的问题,请参考以下文章
由于选择性低(全为 NULL),MariaDB 不在 1 列自连接上使用索引