Dave
Dave

Reputation: 687

Mysql query for blank data

Can I run a query that would perform the following?

Table: users
Columns: username, email

If username is blank/empty, update username with email in that row

Upvotes: 1

Views: 1054

Answers (4)

gvitchev
gvitchev

Reputation: 19

This should do it:

UPDATE users
SET username = email
WHERE username = ""
OR username is NULL;

Upvotes: 1

DevelopmentIsMyPassion
DevelopmentIsMyPassion

Reputation: 3591

Update users set username = email where username is null  or username = ''

Upvotes: 1

Dipesh Parmar
Dipesh Parmar

Reputation: 27364

UPDATE
    users
SET
    users.username = users.email
WHERE users.username IS NULL OR LENGTH(users.username)=0;

Upvotes: 0

John Woo
John Woo

Reputation: 263723

Assuming by BLANK/EMPTY means NULL or nothing,

if you mean you only want to project or SELECT, use COALESCE

SELECT COALESCE(username, email) username,
       email
FROM   users

but if you want to permanently update the field,

UPDATE users
SET    username =  email
WHERE  userName IS NULL

Upvotes: 5

Related Questions