Home >Database >Mysql Tutorial >How Can I Select Only Non-Empty Columns in MySQL?

How Can I Select Only Non-Empty Columns in MySQL?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-15 21:49:14270browse

How Can I Select Only Non-Empty Columns in MySQL?

Selecting Non-Empty Columns in MySQL

In MySQL, retrieving columns only where a value exists can enhance data analysis and ensure accuracy. The article delves into a successful method to accomplish this task.

Consider the scenario where phone numbers stored in the phone and phone2 columns need filtering. The goal is to select rows where phone starts with '813' and phone2 contains a value.

The SQL query you provided:

select phone, phone2
from jewishyellow.users
where phone like '813%'
and phone2

fails because MySQL interprets an empty phone2 column as a logical false value. To remedy this, compare phone2 with an empty string using the following query:

select phone, phone2 
from jewishyellow.users 
where phone like '813%' and phone2<>''

By utilizing this approach, MySQL treats all non-empty phone2 values as true, resulting in the successful selection of desired rows.

The above is the detailed content of How Can I Select Only Non-Empty Columns in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn