Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update a column in MySQL table if only the values are empty or NULL

Tags:

sql

mysql

joomla

I had previously applied this query...which works perfectly and was answered by one of the fellow members of this forum

UPDATE
    jos_jbjobs_jobseeker a
    INNER JOIN jos_users b ON a.email = b.email
SET
    a.user_id = b.id

Now i want to use the same query adding one more condition... i.e

Set a.user_id = b.id only if a.user_id is empty ,,

can i apply this :

if a.user_id = '' SET a.user_id = b.id ;

?

like image 483
ricardo Avatar asked Apr 21 '12 20:04

ricardo


3 Answers

UPDATE
    jos_jbjobs_jobseeker a
    INNER JOIN jos_users b ON a.email = b.email
SET
    a.user_id = b.id
WHERE a.id IS NULL OR LENGTH(a.id)=0;
like image 67
Teja Avatar answered Oct 12 '22 21:10

Teja


Use this

UPDATE
    jos_jbjobs_jobseeker a
    INNER JOIN jos_users b ON a.email = b.email
SET
    a.user_id = b.id
WHERE a.id ='';

If id have null values too then use this-

UPDATE
    jos_jbjobs_jobseeker a
    INNER JOIN jos_users b ON a.email = b.email
SET
    a.user_id = b.id
WHERE a.id is null or a.id ='';
like image 44
Ankit Sharma Avatar answered Oct 12 '22 23:10

Ankit Sharma


Try this code SQL native it's work for me very well :

UPDATE table 
SET field = 'New value'
WHERE field 
IS NULL
OR field = ''

Update just NULL value or EMPTY.

like image 25
MISSIRIA Avatar answered Oct 12 '22 21:10

MISSIRIA