【发布时间】:2013-02-11 17:14:01
【问题描述】:
我有一个乐器清单和教师乐器清单。
我想获得带有 id 和名称的完整乐器列表。
然后检查 Teachers_instrument 表中的乐器,如果特定教师拥有该乐器,请在新列中添加 NULL 或 1 值。
然后我可以用它来遍历 Codeigniter 中的一些仪器复选框,当我需要从数据库中提取数据但我正在努力编写查询时,这似乎更有意义。
teaching_instrument_list
- id
- instrument_name
teachers_instruments
- id
- teacher_id
- teacher_instrument_id
SELECT
a.instrument,
a.id
FROM
teaching_instrument_list a
LEFT JOIN
(
SELECT teachers_instruments.teacher_instrument_id
FROM teachers_instruments
WHERE teacher_id = 170
) b ON a.id = b.teacher_instrument_id
我的查询如下所示:
instrument name id value
--------------- -- -----
woodwinds 1 if the teacher has this instrument, set 1
brass 2 0
strings 3 1
【问题讨论】:
-
teachers_instruments中的代理键有什么意义? -
抱歉,您的问题有点含糊。您是否要计算使用每种乐器的教师人数?然后 LEFT 加入您的
ti表并使用 COUNT(teacher_id) 进行计数。 -
我猜是不需要的。
-
我更新了帖子,希望对您有所帮助。
标签: php mysql codeigniter