【发布时间】:2018-08-07 12:59:00
【问题描述】:
我很难查询。
如果我运行这个(仅限第一胎):
SELECT subject.name as subject, GROUP_CONCAT(evaluations_mark.note) as mark
FROM evaluations_mark
LEFT JOIN subject ON evaluations_mark.subject_id = subject.subject_id
LEFT JOIN user ON evaluations_mark.user_id = user.user_id
WHERE evaluations_mark.number_exam = 1 AND evaluations_mark.user_id = 28265
AND subject.print_visible='1'
AND subject.number_exam = '4'
GROUP BY evaluations_mark.subject_id
我得到这个结果很好:
+--------------------+-----+
| Lengua | SA |
| Matematica | SA |
| Ciencias Sociales | MSA |
| Ciencias Naturales | MSA |
| Italiano | MSA |
| Ingles | SA |<--RIGHT!
+--------------------+-----+
如果我运行其他查询:
SELECT subject.name as subject, GROUP_CONCAT(evaluations_mark.note) as mark
FROM evaluations_mark
LEFT JOIN subject ON evaluations_mark.subject_id = subject.subject_id
LEFT JOIN user ON evaluations_mark.user_id = user.user_id
WHERE evaluations_mark.number_exam = 2 AND evaluations_mark.user_id = 28265
AND subject.print_visible='1'
AND subject.number_exam = '4'
GROUP BY evaluations_mark.subject_id
我得到的结果也很好(仅限第二学期):
+--------------------+-----+
| Lengua | SA |
| Matematica | SA |
| Ciencias Sociales | SA |
| Ciencias Naturales | MSA |
| Italiano | MSA |
| Ingles | MSA |<--RIGHT!
+--------------------+-----+
现在,问题终于来了。如果我运行这个该死的查询:
SELECT subject.name as subject, GROUP_CONCAT(evaluations_mark.note) as mark
FROM evaluations_mark
LEFT JOIN subject ON evaluations_mark.subject_id = subject.subject_id
LEFT JOIN user ON evaluations_mark.user_id = user.user_id
WHERE evaluations_mark.number_exam IN (1,2) AND evaluations_mark.user_id = 28265
AND subject.print_visible='1'
AND subject.number_exam = '4'
GROUP BY evaluations_mark.subject_id
我得到了 1 条记录倒置(两个 bimester):
+--------------------+-----+-----+
| Lengua | SA | SA |
| Matematica | SA | SA |
| Ciencias Sociales | MSA | SA |
| Ciencias Naturales | MSA | MSA |
| Italiano | MSA | MSA |
| Ingles | MSA | SA |******* <<--WRONG! IT'S INVERTED!
+--------------------+-----+-----+
第一栏:主题 第 2 栏:第 1 次妊娠(第一个结果) 第 3 列:第 2 次孕期(第二个结果)
到底为什么会发生这种情况?提前致谢!
EDIT2:将 3 张图片替换为文字。
EDIT3:这里是表结构
TABLE `evaluations_mark` (
`mark_id` int(13) NOT NULL,
`note` varchar(225) NOT NULL,
`comments` text NOT NULL,
`subject_id` int(13) NOT NULL,
`course_id` int(13) NOT NULL,
`user_id` int(13) NOT NULL,
`evaluation_id` int(13) NOT NULL,
`number_exam` int(2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
TABLE `subject` (
`subject_id` int(6) NOT NULL,
`name` varchar(50) NOT NULL,
`course_id` int(6) NOT NULL,
`teacher_id` int(6) NOT NULL,
`load_type` varchar(20) NOT NULL,
`hours` int(11) NOT NULL,
`area_id` int(2) NOT NULL,
`active` tinyint(1) NOT NULL,
`number_exam` int(6) NOT NULL,
`parent` int(5) NOT NULL,
`print_visible` tinyint(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
编辑 4:从两个表中添加所需的内容
evaluations_mark 表:
mark_id note comments subject_id course_id 1 user_id evaluation_id number_exam
4363 SA 87 8 28265 0 1
4892 MSA 84 8 28265 0 2
1078 SA 85 8 28265 0 1
3646 MSA 88 8 28265 0 1
2634 MSA 89 8 28265 0 1
125 SA 80 8 28265 0 1
147 MSA 82 8 28265 0 1
1430 MSA 84 8 28265 0 1
169 MSA 83 8 28265 0 1
3753 SA 80 8 28265 0 2
3510 MSA 85 8 28265 0 2
191 SA 81 8 28265 0 1
3775 SA 81 8 28265 0 2
6858 MSA 86 8 28265 0 1
3279 MSA 83 8 28265 0 2
3797 SA 82 8 28265 0 2
主题表:
subject_id name course_id teacher_id load_type hours area_id active number_exam parent print_visible
80 Lengua y Literatura 8 0 1 4 2 0 4 0 1
81 Matemática 8 0 1 4 2 0 4 0 1
82 Ciencias Sociales 8 0 1 4 2 0 4 0 1
83 Ciencias Naturales 8 0 1 4 2 0 4 0 1
84 Italiano 8 0 2 5 2 0 4 0 1
85 Inglés 8 0 2 4 2 0 4 0 1
86 Expresion Plástica 8 0 2 3 2 0 2 0 1
87 Música 8 0 2 2 2 0 2 0 1
88 Informática 8 0 2 3 2 0 2 0 1
89 Educación Física 8 0 2 2 2 0 2 0 1
编辑 4:我标记了正确和错误。
【问题讨论】:
-
当您像这样将右侧表条件放在 WHERE 子句中时,那些 LEFT JOINS 会返回常规的 INNER JOIN 结果。将条件移至 ON 子句以获得真正的 LEFT JOIN 结果。
-
对不起..没有关注你..怎么样?可以举个例子吗?
-
第一张图1和2是一样的。第二,你说的倒置是什么意思?向我们展示您期望的结果。第三张图片没有说明问题是什么,因为还有另一行具有相同的值,你不称它为倒置
-
将表格和数据发布为文本READ THIS
-
向我们展示数据库架构、示例数据、当前和预期输出。请阅读How-to-Ask 这里是START 了解如何提高问题质量并获得更好答案的好地方。 How to create a Minimal, Complete, and Verifiable example 尝试在rextester.com 中创建样本
标签: sql where group-concat