简体   繁体   English

如何从 MySQL 中的一行中的列中计算值

[英]How to count value from column in a row in MySQL

How to count all the columns in a row from a table?如何计算表格中一行中的所有列? I'm having trouble combining of queries, I really need help.我在组合查询时遇到问题,我真的需要帮助。 Someone help me.谁来帮帮我。

My Table looks like this;我的表看起来像这样;

jhsf2 jhsf2

+-----------------------------------------+
|Stud_ID|M1|T1|W1|TH1|F1|M2|T2|W2|TH2|F2| 
|1022234|  |P |P |P  |P |A |P |P | P | P|
|1045978|  |P |P |P  |P |P |P |P | P | P|
+---------------------------------------+

And I want the result like this if I have the query to do that.如果我有查询要这样做,我想要这样的结果。 I want to count from M1 to F2 that have a value not Null and it's something like this.我想从 M1 数到 F2 的值不是 Null ,它是这样的。

jhsf2 jhsf2

+-----------------------------------------------+
|Stud_ID|Count(M1,T1,W1,TH1,F1,M2,T2,W2,TH2,F2) |
|1022234|                  9                    |
+-----------------------------------------------+

OR I want to use WHERE query in mysql, i don't know the query but it's just like this.或者我想在 mysql 中使用 WHERE 查询,我不知道查询,但就是这样。

Select Stud_ID,M1,T1,W1,TH1,F1,M2,T2,W2,TH2 from jhsf2 
WHERE Stud_ID = "131418100008" 
and M1,T1,W1,TH1,F1,M2,T2,W2,TH2 T2, = "P"


+-----------------------------------------------+
|Stud_ID|Count(M1,T1,W1,TH1,F1,M2,T2,W2,TH2,F)  |
|1022234|                  8                    |
+-----------------------------------------------+

In MySQL, you can count the values by adding up booleans:在 MySQL 中,您可以通过将布尔值相加来计算值:

select stud_id,
       ( (m1 = 'P') + (m2 = 'P') + . . .
       ) as cnt
from jhsf2;

Note: Having multiple columns that contain essentially an array is not a good data structure in SQL.注意:多列本质上包含一个数组在 SQL 中并不是一个好的数据结构。 Normally, you would want one row per stud_id and column value.通常,您希望每个stud_id和列值stud_id一行。

You can try this by checking the value not null and count of asterisk (all)您可以通过检查非空值和星号计数来尝试此操作(全部)

        Select count(*) from     
         jhsf2 WHERE Stud_ID !=null and M1!=null and ..
         .......

..........rest all columns which u want to check that it's values are fine or not. ..........休息所有你想检查它的值是否正确的列。

I am not clever enough to create a select to do the job but you can do it in code.我不够聪明,无法创建一个选择来完成这项工作,但您可以在代码中完成。 I have guessed the data type of Student ID as Long.我猜学生 ID 的数据类型为 Long。 Adjust the code if it is something else.如果是其他内容,请调整代码。

Private Sub GetCount()
        Dim dictStudents As New Dictionary(Of Long, Integer)
        Dim itgNonNullCount As Integer = 0
        Dim strQuery As String = "Select * from jhsf2"
        Using cn As New MySqlConnection("your connection string")
            Using cmd As New MySqlCommand With {
                    .Connection = cn,
                    .CommandType = CommandType.Text,
                    .CommandText = strQuery}
                cn.Open()
                Using dr As MySqlDataReader = cmd.ExecuteReader
                    If dr.HasRows Then
                        While dr.Read
                            'Loop through each column
                            For i As Integer = 1 To 10 'number of columns -1
                                If dr.GetValue(i) IsNot DBNull.Value Then
                                    itgNonNullCount += 1
                                End If
                            Next
                            'Add the student ID and count to the dictionary
                            dictStudents.Add(dr.GetInt64(0), itgNonNullCount)
                            'reset the count back to zero for the next record
                            itgNonNullCount = 0
                        End While
                    End If
                End Using
            End Using
        End Using
    End Sub

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

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