简体   繁体   English

如何执行MYSQL条件选择语句

[英]How to do a MYSQL conditional select statement

Background 背景

I'm faced with the following problem, relating to three tables 我面临以下与三张桌子有关的问题

class_sectors table contains three categories of classes class_sectors表包含三类类别

classes table contains a list of classes students can attend classes表格包含学生可以参加的班级列表

class_choices contains the first, second and third class choice of the student, for each sector. class_choices包含每个部门学生的第一,第二和第三等选择。 So for sector 1 Student_A has class_1 as first choihce, class_3 as second choice and class_10 as third choice for example, then for sector 2 he has another three choices, etc... 例如,对于扇区1,Student_A将class_1作为第一选择,将class_3作为第二选择,将class_10作为第三选择,然后对于扇区2,他具有另外三个选择,依此类推。

The class_choices table has these columns: class_choices表具有以下列:

kp_choice_id | kf_personID | kf_sectorID | kf_classID | preference | assigned

I think the column names are self explanatory. 我认为列名是不言自明的。 preference is either 1, 2 or 3. And assigned is a boolean set to 1 once we have reviewed a student's choices and assigned them to a class. preference是1,2或3,而assigned是一个布尔值设置为1,一旦我们审查了学生的选择和他们分到一个班。

Problem : 问题

Writing an sql query that tells the students what class they are assigned to for each sector. 编写一个sql查询,告诉学生他们为每个部门分配的班级。 If their class hasn't been assigned, it should default to show their first preference. 如果尚未分配他们的班级,则应默认显示他们的第一个首选项。

I have actually got this to work, but using two (very bloated??) sql queries as follows: 我实际上已经做到这一点,但使用两个(非常(肿的?)SQL查询,如下所示:

$choices = $db -> Q("SELECT
    *, concat_ws(':', `kf_personID`, `kf_sectorID`) AS `concatids`
FROM
    `class_choices`
WHERE
    (`assigned` = '1')
GROUP BY
    `concatids`
ORDER BY
    `kf_personIDID` ASC,
    `kf_sectorID` ASC;");

$choices2 = $db -> Q("SELECT
    *, concat_ws(':', `kf_personID`, `kf_sectorID`) AS `concatids`
FROM
    `class_choices`
WHERE
    `preference` = '1'
GROUP BY
    `concatids`
HAVING
    `concatids` NOT IN (".iimplode($choices).")
ORDER BY
    `kf_personID` ASC,
    `kf_sectorID` ASC;");

if(is_array($choices2)){
    $choices = array_merge($choices,$choices2);
}

Now $choices does have what I want. 现在$choices确实有我想要的东西。

But I'm sure there is a way to simplify this, merge the two SQL queries, and so it's a bit more lightweight. 但是我敢肯定,有一种方法可以简化此过程,合并两个SQL查询,因此它要轻一些。

Is there some kind of conditional SQL query that can do this??? 有某种可以执行此操作的条件SQL查询吗???

Your solution uses two steps to enable you to filter the data as needed. 您的解决方案使用两个步骤来使您能够根据需要过滤数据。 Since you are generating a report, this is a pretty good approach even if it looks a bit more verbose than you might like. 由于您正在生成报告,因此即使看起来比您希望的更为冗长,这也是一种非常好的方法。

The advantage of this approach is that it is much easier to debug and maintain, a big plus. 这种方法的优点是调试和维护容易得多,这是一大优点。

To improve the situation, you need to consider the data structure itself. 为了改善这种情况,您需要考虑数据结构本身。 When I look at the class_choices table, I see the following fields: kf_classID, preference, assigned which contain the key information. 当我查看class_choices表时,我看到以下字段: kf_classID, preference, assigned ,其中包含键信息。

For each class, the assigned field is either 0 (default) or 1 (when the class preference is assigned for the student). 对于每个班级, assigned字段为0(默认值)或1(当为学生分配班级偏好时)。 By default, the class with preference = 1 is the assigned one since you display it in the report when assigned=0 for all the student's class choices in a particular sector. 默认情况下, preference = 1preference = 1的班级是已分配的班级,因为当您为特定部门中所有学生的班级选择assigned=0时,您将其显示在报告中。

The data model could be improved by imposing a business rule as follows: 可以通过施加如下业务规则来改进数据模型:
For preference=1 set the default value assigned=1 . 对于preference=1设置assigned=1的默认值assigned=1 When the class selection process takes place, and if the student gets assigned the 2nd or 3rd choice, then preference 1 is unassigned and the alternate choice assigned. 在进行班级选择过程时,如果为学生分配了第二或第三选择,则未分配首选项1,并且分配了替代选择。

This means a bit more code in the application but it makes the reporting a bit easier. 这意味着应用程序中的代码会更多,但是使报告更加容易。

The source of the difficulty is that the assignment process does not explicitly assign the 1st preference. 困难的根源在于分配过程没有明确分配第一偏好。 It only updates assigned if the student cannot get the 1st choice. 仅当学生无法获得第一选择时,它才会更新assigned的内容。

In summary, your SQL is good and the improvements come from taking another look at the data model. 总而言之,您的SQL很不错,而改进又来自于另一种数据模型。

Hope this helps, and good luck with the work! 希望这会有所帮助,并祝您工作顺利!

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

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