MYSQL query to exclude column with null value in the result

aaa28 :

The scenario is I have two tables that I wanted to query using a search function using PHP. There are two tables, table_a and table_b. In table_a it has id, name, middle_name, surname, case_no columns and table_b is similar. Data in table_b are all imported from another source, while table_a data was inserted manually by the users. There were instances that there is a duplicate data from table_a and table_b. What I want is to remove the duplicate data from 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.

+----------+----------+----------+     +----------+----------+----------+
| 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. 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. If possible I only want to only show the result that has no null value.

Badrinath :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=16538&siteId=1