简体   繁体   中英

How to insert data from one column into another column in a different table

I have two tables LESSON & SPORT.

LESSONNO | SPORTNO | INSTRUCTORNO | DATE | PRICE |

SPORTNO | SPORT NAME | SPORTDESCRIPTION |

I need to add column SPORTNAME to the lesson table, and for its data to match the SPORTNO in lesson table as it does in sport table.

Thanks in advance!

Does this work for you:

ALTER TABLE LESSON ADD SPORTNAME VARCHAR(30); // or whatever type it is
UPDATE LESSON l JOIN SPORT s USING(SPORTNO) SET l.SPORTNAME = s.SPORTNAME;

?

This worked for me in SQL Server

 ALTER TABLE LESSON ADD SPORTNAME VARCHAR(50)

  UPDATE LESSON
  SET SPORTNAME = S.SPORTNAME
  FROM dbo.LESSON AS L
  INNER JOIN dbo.SPORT AS S
  ON L.SPORTNO = S.SPORTNO

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM