-1

我创建了这个选择查询来获取一些行......它的工作。但我的问题是我需要从查询中删除重复的行...

这是我的查询:

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 |
+----------+-------------+------------+-------------------------------------------------+-----------------------+------------+
4

4 回答 4

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;
于 2012-10-31T10:54:56.187 回答
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%';
于 2012-10-31T10:45:21.113 回答
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;
于 2012-10-31T10:54:57.253 回答
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;
于 2012-10-31T11:31:20.623 回答