vince
vince

Reputation: 172

MYSQL on how to insert value into a specific column table

This is my table name mam_zip

Columns are id, zipcode, cityname, countryname, statename, stateabbr, category

For example:

Pseudo code:

if statename = 'New York' then insert 'Regional' in category
if statename = 'California' then insert 'National' in category

Is there a shorter way to insert values in SQL so that I will not manually inserting values in column table category because this has a lot of records

Upvotes: 1

Views: 492

Answers (3)

Abhishek
Abhishek

Reputation: 529

Try this query that would conditionally populates your category column.

INSERT INTO mam_zip (<otherColumns....>,category)
SELECT <OtherColumns>,
        CASE 
        WHEN statename = 'New York'
            THEN 'Regional'
        WHEN statename = 'California'
            THEN 'National'
        END

Upvotes: 0

Fahmi
Fahmi

Reputation: 37473

You can try below using case when

demo

you need to insert your values for other columns

insert into mam_zip(id, zipcode, cityname, countryname, statename, stateabbr, category)

values(id,zipcode,cityname,countryname,statename,stateabbr, 
case when statename='New York' then 'Regional'
when statename='California' then 'Natioanal' else statename end)

Upvotes: 0

Shalvin
Shalvin

Reputation: 178

Simply use an insert query like so

insert into  mam_zip (category) values (regional)

or simply do an update

update mam_zip
where statename = 'New York' 
set catergory = 'Regional' 

Cheers

Upvotes: 2

Related Questions