简体   繁体   English

PHP MySQL:浏览每一行,并计算有多少列满足条件

[英]PHP MySQL: Go through each row, and count how many columns meet criteria

I've got a table similar to: 我有一张类似的桌子:

|       | column1 | column2 | column3 | column4 |
|-------|---------|---------|---------|---------|
| user1 | 100     | 49      | 3       | 1,980   |
| user2 | 7       | 5       | 51      | 500     |
| user3 | 1       | 65      | 44      | 37      |

I want to go through all the rows, and for each of those rows, I want to select the count() relating to how many columns meet a criteria ( value < 100 , for example) 我想遍历所有行,对于这些行中的每一行,我都想选择与符合条件的列count()有关的count() (例如, value < 100

So the result of each row calculating value < 100 would be: 因此,每行计算value < 100将是:

user1: 2
user2: 3
user3: 4

Does anyone have an idea how to go about this? 有谁知道如何解决这个问题? I can't seem to find any answers for a question this specific. 对于这个特定的问题,我似乎找不到任何答案。

Thanks! 谢谢!

I totally forgot about just doing a simple, hardcoded query. 我完全忘了只做一个简单的硬编码查询。

This solution works for me: 此解决方案适用于我:

SELECT user,              (column1 < 100) +
                          (column2 < 100) +
                          (column3 < 100) +
                          (column4 < 100)
AS total FROM table;

Thanks to the commenters - I had overlooked this simple solution. 感谢评论者-我忽略了这个简单的解决方案。

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

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