我创建了这个选择查询来获取一些行...它正在工作。但我的问题是我需要从查询中删除重复的行...
这是我的疑问:
SELECT tcs.tutor_id, tcs.category_id, tcs.subject_id, s.subjects, t.tutor_name, t.tutor_code
FROM tutor_category_subject as tcs
INNER JOIN subject AS s ON tcs.subject_id = s.subject_id
INNER JOIN tutors AS t ON tcs.tutor_id = t.tutor_id
WHERE s.subjects LIKE '%business%';
这是它的输出:
+----------+-------------+------------+------------------+-------------------+-----------+
| tutor_id | category_id | subject_id | subjects | tutor_name | tutor_code|
+----------+-------------+------------+------------------+-------------------+-----------+
| 1 | 6 | 37 | Business Studies | Tharanga Nuwan | 1250 |
| 3 | 6 | 37 | Business Studies | Kumara | 1252 |
| 15 | 4 | 11 | Business & Accou | Tharanga Nuwan | 1264 |
| 15 | 6 | 37 | Business Studies | Tharanga Nuwan | 1264 |
| 16 | 5 | 11 | Business & Accou | Kasun Kalhara | 1265 |
| 16 | 6 | 37 | Business Studies | Kasun Kalhara | 1265 |
在这里,您可以看到导师ID在我的查询中重复。我需要选择所有科目到一行中以逗号分隔的导师。
例如:(商业与会计研究,商业研究)就像这样..
所以有人能告诉我在我的选择查询中需要做什么吗?
谢谢。
这是我期待的结果
+----------+-------------+------------+-------------------------------------------------+-----------------------+------------+
| tutor_id | category_id | subject_id | subjects | tutor_name | tutor_code |
+----------+-------------+------------+-------------------------------------------------+-----------------------+------------+
| 16 | 5 | 11 | Business & Accounting Studies, Business Studies | Kasun Kalhara | 1265 |
| 3 | 6 | 37 | Business Studies | Kumara | 1252 |
| 1 | 6 | 37 | Business Studies, Business & Accounting Studies | Tharanga Nuwan Kumara | 1250 |
+----------+-------------+------------+-------------------------------------------------+-----------------------+------------+
答案 0 :(得分:2)
为了让主题在一行中,您应该创建一个函数,该函数将返回逗号分隔的字符串,其中包含每个教师的主题。可能你需要传递一个参数tutor_id(也可能是一个带有你需要添加的“like子句”的字符串)。然后在你的选择中你会有这样的东西:
SELECT tcs.tutor_id, tcs.category_id, t.tutor_name, t.tutor_code, function_name(tcs.tutor_id)
FROM tutor_category_subject as tcs
INNER JOIN tutors AS t ON tcs.tutor_id = t.tutor_id;
答案 1 :(得分:0)
尝试:
SELECT DISTINCT tcs.tutor_id, tcs.category_id, tcs.subject_id, s.subjects, t.tutor_name, t.tutor_code
FROM tutor_category_subject as tcs
INNER JOIN subject AS s ON tcs.subject_id = s.subject_id
INNER JOIN tutors AS t ON tcs.tutor_id = t.tutor_id
WHERE s.subjects LIKE '%business%';
或:
SELECT DISTINCTROW tcs.tutor_id, tcs.category_id, tcs.subject_id, s.subjects, t.tutor_name, t.tutor_code
FROM tutor_category_subject as tcs
INNER JOIN subject AS s ON tcs.subject_id = s.subject_id
INNER JOIN tutors AS t ON tcs.tutor_id = t.tutor_id
WHERE s.subjects LIKE '%business%';
答案 2 :(得分:0)
SELECT tcs.tutor_id, tcs.category_id, tcs.subject_id, GROUP_CONCAT(s.subjects) AS subjects, t.tutor_name, t.tutor_code
FROM tutor_category_subject as tcs
INNER JOIN subject AS s ON tcs.subject_id = s.subject_id
INNER JOIN tutors AS t ON tcs.tutor_id = t.tutor_id
WHERE s.subjects LIKE '%business%'
GROUP BY t.tutor_name;
答案 3 :(得分:0)
立即尝试,与group by:
分开SELECT DISTINCT tcs.tutor_id, tcs.category_id, tcs.subject_id, group_by(s.subjects), t.tutor_name, t.tutor_code
FROM tutor_category_subject as tcs
INNER JOIN subject AS s ON tcs.subject_id = s.subject_id
INNER JOIN tutors AS t ON tcs.tutor_id = t.tutor_id
WHERE s.subjects LIKE '%business%'
GROUP BY tcs.tutor_id;