【发布时间】:2023-03-26 03:39:01
【问题描述】:
每个学生有多个支付时间段,我想获取未支付费用的时间段,或者MySQL语言 - 获取不在另一个表中的行。
在这里,我在MySQL 中使用嵌套的GROUP_CONCAT,并在LEFT JOIN 中使用IS NULL
表格student - 学生名单
id | ttl | cls | sec
===============================
1 | Rohit | 1 | 1
2 | Karuna | 2 | 0
表cls - 类列表
id | ttl
===========
1 | One
2 | Two
表格sec - 部分列表
id | ttl
===========
1 | A
2 | B
表格fee_tm - 费用时间段列表
id | ttl
===========
1 | Jan
2 | Feb
表格std_fee - 分配给学生的费用期限列表
id | s_id| f_id| fee| f_tm
====================================
1 | 1 | 4 | 100| 1
根据表结构和表中的行,我期望使用我的 MySQL 代码跟随输出。
//(student.id-student.cls-student.sec-student rest of the fee.time(Month1,Month2..))
1-Rohit-One-A-Feb,
2-Karuna-Two-John,Feb
但是我得到的(我想申请 NULL 和 LEFT JOIN 仅限收费时间,所以可以获取剩余的收费时间,但这里适用到整个结果)
2-Karuna-Two-
MySQL 代码
SELECT
GROUP_CONCAT(DISTINCT CONCAT(student.id,'-',student.ttl,'-',cls.ttl,'-',
COALESCE(sec.ttl,''),
COALESCE(CONCAT('-',fee_tm.ttl),''))
ORDER BY student.id) AS stdt
FROM
student
JOIN
cls ON cls.id=student.cls
LEFT JOIN
sec ON sec.id=student.sec
LEFT JOIN
std_fee ON std_fee.s_id = student.id
LEFT JOIN
fee_tm ON fee_tm.id = std_fee.f_tm
WHERE
std_fee.f_tm IS NUll
【问题讨论】:
-
尝试将
WHERE std_fee.f_tm IS NULL移至ON子句。
标签: mysql sql group-by left-join group-concat