Friday, January 1, 2021

UPDATE & UPDATE JOIN USE IN MYSQL

 

The UPDATE Statement is used for change data in table . 

Solve : 

select * from employee

# id,  first_name, last_name ,  phone          email
    '1',       'A',              'K',         '94XXXX',   'A@gmail.com'
    '3',       'D',              'L',         '96XXXX',   'b@gmail.com'
 

Now I want to update phone no  

update employee set phone='9643939373' where id=1 ;

select * from employee

# id,  first_name, last_name ,   phone                 email
    '1',       'A',              'K',         '9643939373',   'A@gmail.com'
    '3',       'D',              'L',         '96XXXX',          'b@gmail.com'

update employee set phone='9999373737' where id=3 ;

select * from employee

# id,  first_name, last_name ,   phone                 email
    '1',       'A',              'K',         '9643939373'    'A@gmail.com'
    '3',       'D',              'L',         '9999373737'    'b@gmail.com'

We are finally update phone no in  employee table.

Now we are update multiple column  first_name & last_name.

 update employee set first_name='Tom',last_name='Petter' where id=3 ;

select * from employee

# id,  first_name, last_name ,   phone                 email
    '1',       'A',              'K',         '9643939373'    'A@gmail.com'
    '3',       'Tom',          Petter,   '9999373737'    'b@gmail.com'

Now we are use  update join 

add new column 

alter table employee add dept_id int  ;

# id,  first_name, last_name ,   phone                 email                 dept_id
    '1',       'A',              'K',         '9643939373'    'A@gmail.com'      Null
    '3',       'Tom',          Petter,   '9999373737'    'b@gmail.com'       Null

select * from dept ;

#ID  emp_id Dept_Name

  1        1           IT

  2       2           A/C

update  employee e inner join dept t  on  e.ID=t.ID   set e.dept_id=t.ID where e.id=1 ;

# id,  first_name, last_name ,   phone                 email                 dept_id
    '1',       'A',              'K',         '9643939373'    'A@gmail.com'      1
    '3',       'Tom',       Petter,   '9999373737'    'b@gmail.com'       Null 

update  employee e inner join dept t  on  e.ID=t.ID   set e.dept_id=t.ID where e.id=3

# id,  first_name, last_name ,   phone                 email                 dept_id
    '1',       'A',              'K',         '9643939373'    'A@gmail.com'      1
    '3',       'Tom',       Petter,   '9999373737'    'b@gmail.com'       3 

Real world example

update  test.1day_daily_summary  inner join STATION s
on s.STATION_NAME=a.location 
set a.datestationid=concat(date_format(a.datadate, '%Y%m%d'),a.Station_id)
where s.STATION_NAME=a.location and a.tehsil=s.TEHSIL and a.district=s.DISTRICT  and a.Station_id=s.STATION_ID;



No comments:

Post a Comment

How to Find the Third highest salary in mysql

  set @b:=0; set @b1:=3;   # #select distinct salary,@b:=@b+1 as Row_no from emp select  * from emp where salary=( select salary from ( sele...