简体   繁体   English

MySQL选择只有值的列

[英]MySQL Select Columns with value only

I found a similar question but not quite the answer that I need.我发现了一个类似的问题,但不是我需要的答案。 That's why I'm asking again.这就是为什么我再次问。 Thank you for understanding.谢谢你的理解。

I have a table that has 100+ columns.我有一个有 100 多列的表。

After researching I found this:经过研究,我发现了这一点:

Select * from your_table 
WHERE col1 and col2 and col3 and col4 and col5 IS NOT NULL

My question is, is there any way that I can filter out all the columns that has no data wihout listing them all like the code above?我的问题是,有什么方法可以过滤掉所有没有数据的列,而不像上面的代码那样将它们全部列出?

A way of doing this is to use the fact that a concat of columns where any of the columns is null results in a null result.这样做的一种方法是利用这样一个事实,即任何列为空的列的连接都会导致空结果。 MD5 hashing the columns can then be tested - you still have to specify all the appropriate columns but that is trivial.然后可以测试对列进行 MD5 散列 - 您仍然必须指定所有适当的列,但这很简单。

DROP TABLE IF EXISTS T;

CREATE TABLE T
(ID INT, COL1 INT,COL2 VARCHAR(2));
INSERT INTO T VALUES
(1,NULL,NULL),
(2,1,NULL),
(3,NULL,'AA'),
(4,1,'BB');


SELECT ID,COL1,COL2,concat(col1,col2) ccol,MD5(CONCAT(COL1,COL2)) hsh
FROM T
;

+------+------+------+------+----------------------------------+
| ID   | COL1 | COL2 | ccol | hsh                              |
+------+------+------+------+----------------------------------+
|    1 | NULL | NULL | NULL | NULL                             |
|    2 |    1 | NULL | NULL | NULL                             |
|    3 | NULL | AA   | NULL | NULL                             |
|    4 |    1 | BB   | 1BB  | b2161dc77696ff2fd06d9f814b443923 |
+------+------+------+------+----------------------------------+
4 rows in set (0.001 sec)

SELECT ID,COL1,COL2,MD5(CONCAT(COL1,COL2))
FROM T
WHERE MD5(CONCAT(COL1,COL2)) IS NOT NULL;

+------+------+------+----------------------------------+
| ID   | COL1 | COL2 | MD5(CONCAT(COL1,COL2))           |
+------+------+------+----------------------------------+
|    4 |    1 | BB   | b2161dc77696ff2fd06d9f814b443923 |
+------+------+------+----------------------------------+
1 row in set (0.009 sec)

If you know the columns you wish to exclude you can build a sql statement from information_schema.columns如果您知道要排除的列,则可以从 information_schema.columns 构建 sql 语句

select concat('select *', ' from t where md5(concat(', (select group_Concat(column_name) from information_schema.columns where table_schema = 'sandbox' and table_name = 't' and column_name <> 'id'), ')) is not null;' ); select concat('select *', ' from t where md5(concat(', (select group_Concat(column_name) from information_schema.columns where table_schema = 'sandbox' and table_name = 't' and column_name <> 'id'), ' )) 不为空;' );

Copy,paste and execute this or复制、粘贴并执行此或

set @sql = (
select concat('select *',
' from t where md5(concat(',
(select group_Concat(column_name) 
                    from information_schema.columns 
                    where table_schema = 'sandbox' and table_name = 't' and column_name <> 'id'),
                    ')) is not null;'
            ));
        
prepare sqlstmt from @sql;
execute sqlstmt;
deallocate prepare sqlstmt;

This code uses the following provider.此代码使用以下提供程序。

Imports MySql.Data.MySqlClient

First I got the column names for the fields in the table.首先,我得到了表中字段的列名。 I only selected fields that are nullable to begin with.我只选择了可以为空的字段。

Next I built the Select string by looping through the column names and adding the phrases to a StringBuilder .接下来,我通过循环列名并将短语添加到StringBuilder来构建 Select 字符串。 (more efficient when building a string of over 10 pieces) (构建超过 10 件的字符串时效率更高)

Finally retrieve the records that meet your requirements.最后检索符合您要求的记录。

Private Function GetNullableColumnNames() As DataTable
    Dim MyTableSchema As New DataTable
    Using cn As New MySqlConnection(My.Settings.StudentConnection),
            cmd As New MySqlCommand("SELECT COLUMN_NAME
                                    FROM INFORMATION_SCHEMA.COLUMNS
                                    WHERE table_name = 'students'
                                    AND table_schema = 'student'
                                    AND IS_NULLABLE = 'YES';", cn)
        cn.Open()
        MyTableSchema.Load(cmd.ExecuteReader)
    End Using
    Return MyTableSchema
End Function

Private Function GetSelectString(dt As DataTable) As String
    Dim sb As New StringBuilder
    sb.Append("Select * From students Where 1 = 1")
    For Each row As DataRow In dt.Rows
        sb.Append($" And {row(0)} IS NOT NULL")
    Next
    sb.Append(";"
    Debug.Print(sb.ToString)
    Return sb.ToString
End Function

Private Function GetRecords(sql As String) As DataTable
    Dim dt As New DataTable
    Using cn As New MySqlConnection(My.Settings.StudentConnection),
            cmd As New MySqlCommand(sql, cn)
        cn.Open()
        dt.Load(cmd.ExecuteReader)
    End Using
    Return dt
End Function

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim dt = GetNullableColumnNames()
    Dim sql = GetSelectString(dt)
    Dim dt2 = GetRecords(sql)
    DataGridView1.DataSource = dt2
End Sub

you can do this:你可以这样做:

Select * from table_name 
WHERE NULL NOT IN (column1, column2, ...);

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

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