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
Release: 2024-12-15 21:49:14
Original
265 people have browsed it

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
Copy after login

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<>''
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template