我有三个表:学生,兴趣和interest_lookup 学生们的的cols student_id数据和名称 利益具有的cols interest_id和INTEREST_NAME interest_lookup拥有的cols student_id数据和interest_id如何将查询表的行放入查询的列中?
要了解你感兴趣的学生有我做
select interests.interest_name from `students`
inner join `interest_lookup`
on interest_lookup.student_id = students.student_id
inner join `interests`
on interests.interest_id = interest_lookup.interest_id
我想要做的就是一个结果集像
student_id | students.name | interest_a | interest_b | ...
其中列名'interest_a'是interests.name中的值并且 interest_列是0或1,使得当 在给定 student_id和interest_id中存在关于interest_lookup中的记录时,该值为1不。
兴趣表中的每个条目都必须显示为列名。
我可以使用子查询(这是超级慢)或通过一堆连接做到这一点,但这两个都确实要求我首先从兴趣中选择所有记录并写出动态查询。
我可以想象的唯一方法是使用关键技术,但它也需要事先知道'interest_name'的所有值 – a1ex07