我有两张桌子tasks
和task_category
:
任务
+---------+----------------+-----------+
| ID |task_category_id| task_name |
+---------+----------------+-----------+
| 1 | 1 | task1 |
+---------+----------------+-----------+
| 2 | 1 | task2 |
+---------+----------------+-----------+
| 3 | 1 | task3 |
+---------+----------------+-----------+
| 4 | 2 | task4 |
+---------+----------------+-----------+
| 5 | 2 | task5 |
+---------+----------------+-----------+
| 6 | 3 | task6 |
+---------+----------------+-----------+
| ... | ... | ... |
+---------+----------------+-----------+
任务类别
+---------+----------------+
| ID |task_category |
+---------+----------------+
| 1 | category_1 |
+---------+----------------+
| 2 | category_2 |
+---------+----------------+
| 3 | category_3 |
+---------+----------------+
| 4 | category_4 |
+---------+----------------+
我需要像这样输出html表:
<table>
<tr>
<td>
category_1
</td>
</tr>
<tr>
<td>
task_1
</td>
</tr>
<tr>
<td>
task_2
</td>
</tr>
<tr>
<td>
task_3
</td>
</tr>
<tr>
<td>
category_2
</td>
</tr>
<tr>
<td>
task_4
</td>
</tr>
</table>
SELECT DISTINCT factor.* , cat.task_name as factors
FROM task_category as factor
LEFT JOIN task AS cat ON factor.id = cat.task_category_id
此 mysql 查询返回重复的类别条目...如何连接表以获得如提供的示例中的结果?