简体   繁体   English

MYSQL查询以排除结果中具有空值的列

[英]MYSQL query to exclude column with null value in the result

The scenario is I have two tables that I wanted to query using a search function using PHP.场景是我有两个表,我想使用 PHP 的搜索功能查询这些表。 There are two tables, table_a and table_b.有两个表,table_a 和 table_b。 In table_a it has id, name, middle_name, surname, case_no columns and table_b is similar.在 table_a 中有 id、name、middle_name、surname、case_no 列和 table_b 类似。 Data in table_b are all imported from another source, while table_a data was inserted manually by the users. table_b 中的数据都是从其他来源导入的,而 table_a 中的数据是用户手动插入的。 There were instances that there is a duplicate data from table_a and table_b.有实例表明 table_a 和 table_b 中存在重复数据。 What I want is to remove the duplicate data from table_b.我想要的是从 table_b 中删除重复数据。 Let say that in table_a there is a column which have null value, what I want is to exclude that from the result.假设在 table_a 中有一个具有空值的列,我想要的是从结果中排除它。

+----------+----------+----------+     +----------+----------+----------+
| name     | surname  | case_num |     | name     | surname  | case_num |
+----------+----------+----------+     +----------+----------+----------+
|  john    |  wick    |  1434    |     |  john    |  wick    |  null    |
+----------+----------+----------+     +----------+----------+----------+
      table_a                                  table_b

What I did now is create a view and combine the two together using Union.我现在所做的是创建一个视图并使用 Union 将两者结合在一起。 It will now look like this.它现在看起来像这样。

+----------+----------+----------+     
| name     | surname  | case_num |     
+----------+----------+----------+     
|  john    |  wick    |  1434    |     
+----------+----------+----------+ 
|  john    | wick     |  null    |
+----------+----------+----------+

Now what I want is to query this table and to show only the result that has no null value which is现在我想要的是查询这个表并只显示没有空值的结果

+----------+----------+----------+     
|  john    |  wick    |  1434    |     
+----------+----------+----------+

And if it is possible to automatically delete the row that has null value which is如果可以自动删除具有空值的行

+----------+----------+----------+     
|  john    |  wick    |  null    |     
+----------+----------+----------+

But the first problem is excluding the row that has null value from the result.但第一个问题是从结果中排除具有空值的行。 Here is my query.这是我的查询。

SELECT * FROM created_view_table WHERE name LIKE ? OR surname LIKE ? OR case_num LIKE ? AND 
case_num <> null;

SELECT * FROM created_view_table WHERE name LIKE ? OR surname LIKE ? OR case_num LIKE ? AND 
case_num IS NOT null;

Whenever I search for '%john%' it always show both row , the one with the column that has null value and the one that has not.每当我搜索 '%john%' 时,它总是同时显示 row ,具有空值的列和没有空值的列。 If possible I only want to only show the result that has no null value.如果可能,我只想显示没有空值的结果。

You are using or clause so you need to bracket out the queries with or and try to do below您正在使用条款,所以你需要支架从与查询,并尝试下面做

select * from created_view_table WHERE (name LIKE ? OR 
surname LIKE ? OR case_num LIKE ?) AND  case_num is not null;

This will solve your issue.这将解决您的问题。

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

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