简体   繁体   English

根据另一个表中的数据从表中选择行

[英]select rows from table based on data from another table

Mentor table
------------
name (varchar)
contact (int)
english (boolean)
french (boolean)
german (boolean)


Student table
-------------
name (varchar)
contact (int)
english (boolean)
french (boolean)
german (boolean)


I want to match mentor with student based on the languages, such that for example: 我想根据语言将导师与学生进行匹配,例如:

if mentor1 knows english and french, he will be matched with all students that know at least english or french. 如果mentor1会英语和法语,那么它将与所有至少会英语或法语的学生相匹配。

mentor1 (english, french)
-------------------------
studentA (english); 
studentB (english, french); 
studentC (english, german);
studentD (english, french, german)  


if mentor2 know german only, he will be matched with all students that know at least german. 如果mentor2仅懂德语,他将与所有至少懂德语的学生相匹配。 the students matched can know more than just german. 匹配的学生不仅仅知道德语。

mentor2 (german)
----------------
studentC (english, german)
studentD (english, french, german)



normally i will just use a bunch of if then else to piece together a sql string but i am using gridview to display the data so i am not sure what can i do. 通常我只会使用一堆if then else将sql字符串拼凑在一起,但是我正在使用gridview来显示数据,所以我不确定该怎么办。

sample codes and tutorials are alway welcome. 始终欢迎示例代码和教程。


edit: forgot to mention that the mentor table will also have columns such as name and contact . 编辑:忘记提及mentor表还将具有诸如namecontact列。 so the output on the gridview should be 1 row per mentor . 所以gridview的输出应该是每个mentor 1行。

Select
   m.MentorName
   , m.Language
   , s.StudentName
from Mentor as m
inner join Student as s
on (m.English = 1 and m.English = s.English)
    or (m.french = 1 and m.French = s.French)
    or (m.German = 1 and m.German = s.German);

This would be easier if you had your tables structured without field for each language but a record instead 如果您将表结构化为每种语言都没有字段,而是用一条记录来代替,则会更容易

Table: Mentor(MentorName, Language)
Rows:
Mentor1 | English
Mentor2 | Englisn
Mentor2 | French

Do the same for Students and then the query is: 对学生执行相同的操作,然后查询为:

Select
   m.MentorName
   , m.Language
   , s.StudentName

from Mentor as m
inner join Student as s
on m.Language = s.Language

The benefit here is if you add another language, it is purely data entry and no need to change table structure or your code, but that is not always an option. 这样做的好处是,如果您添加另一种语言,那纯粹是数据输入,无需更改表结构或代码,但这并不总是一种选择。

SELECT  m.*, s.name
FROM    dbo.Mentor m
JOIN    dbo.Student s
ON EXISTS 
(
    SELECT  x.LanguageID
    FROM    
    (
        SELECT 1 AS LanguageID WHERE s.english = 1 UNION ALL 
        SELECT 2 AS LanguageID WHERE s.french = 1 UNION ALL 
        SELECT 3 AS LanguageID WHERE s.german = 1
    ) x
    INTERSECT
    SELECT  y.LanguageID
    FROM    
    (
        SELECT 1 AS LanguageID WHERE m.english = 1 UNION ALL 
        SELECT 2 AS LanguageID WHERE m.french = 1 UNION ALL 
        SELECT 3 AS LanguageID WHERE m.german = 1
    ) y
)
ORDER BY m.name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM