-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 がクエリで重複していることがわかります。そして、カンマで区切られた1行でチューターのすべての科目を選択する必要があります。

例: (Business & Accounting Studies, Business Studies) このように..

では、選択クエリで何をする必要があるか教えてもらえますか??

ありがとうございました。

これが私の期待する結果です

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

科目を 1 行に表示するには、チューターごとの科目を含むカンマ区切りの文字列を返す関数を作成する必要があります。おそらく、パラメーターとして 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

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 に答える
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

試す:

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 に答える