Home > Database > Mysql Tutorial > How to Select Only Columns with Non-Empty Values in MySQL?

How to Select Only Columns with Non-Empty Values in MySQL?

Susan Sarandon
Release: 2024-12-23 22:13:11
Original
670 people have browsed it

How to Select Only Columns with Non-Empty Values in MySQL?

MySQL: Selecting Columns with Non-Empty Values

In MySQL, you can select specific columns only if they contain non-empty values. This allows you to filter out any rows where those columns are null or empty.

Example Query

Consider the following query:

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

Here, the goal is to select rows where:

  • phone starts with '813'
  • phone2 contains a non-empty value

Solution

To select columns only where non-empty values exist, you can compare the value of the column with an empty string.

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

Explanation

  • phone like '813%': Filters rows where phone starts with '813'.
  • phone2<>'': Checks if phone2 is not equal to an empty string, effectively filtering out empty values.

Note: Null values are interpreted as false in MySQL. This means that rows with phone2 set to null will not be included in the results.

The above is the detailed content of How to Select Only Columns with Non-Empty Values 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