简体   繁体   English

Mysql select 具有相同值列的行

[英]Mysql select row with same value column

I have a student mark table with 4 question answers marked as 1,-1, and 0 means correct, incorrect, and not attended respectively.我有一个学生成绩表,有 4 个问题答案分别标记为 1、-1 和 0 表示正确、不正确和未参加。 I am adding a table structure below.我在下面添加一个表结构。 What I am looking for a MySQL query to update another column as selected or not selected based on how much 1 they got.我正在寻找一个 MySQL 查询来根据他们得到多少 1 将另一列更新为选中或未选中。 So if my condition is a student which got at least 1 got will select then I can write UPDATE table SET selected = 1 WHERE total_mark > 0 (atleast one correct will get a mark of greater than 0).因此,如果我的情况是一名学生至少获得了 1 个 got will select,那么我可以编写UPDATE table SET selected = 1 WHERE total_mark > 0 (至少一个正确的分数将大于 0)。

No, I need at least 2 selected or 3 selected how will I write an update query.不,我需要至少选择 2 个或 3 个,我将如何编写更新查询。

在此处输入图像描述

The best approach is to improve your relational model, by creating a USER_ANSWERS table, then you can simply count the answers having score = 1. Then you are not dependant of the number of questions you have in a survey.最好的方法是通过创建一个 USER_ANSWERS 表来改进您的关系 model,然后您可以简单地计算分数 = 1 的答案。这样您就不会依赖于调查中的问题数量。

If its not something you want to do, this query does the trick:如果这不是您想做的事情,则此查询可以解决问题:

UPDATE TABLE SET selected = 1 WHERE GREATEST(0,m1) + GREATEST(0,m2) + GREATEST(0,m3) + GREATEST(0,m4) > 0

If you want at least 2 1 s for the column selected to be 1 , then:如果您希望selected列的至少 2 11 ,则:

UPDATE tablename 
SET selected = (m1 = 1) + (m2 = 1) + (m3 = 1) + (m4 = 1) >= 2

Each of the boolean expressions mx = 1 evaluates to 1 or 0 for true or false . boolean 表达式mx = 1中的每一个对于truefalse的计算结果为10

See the demo .请参阅演示
Results:结果:

> id | name  | m1 | m2 | m3 | m4 | selected
> -: | :---- | -: | -: | -: | -: | -------:
>  1 | user1 |  1 |  1 |  1 |  1 |        1
>  2 | user2 |  1 | -1 | -1 | -1 |        0
>  3 | user3 |  1 |  1 |  0 |  0 |        1

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

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