Skip to content
Advertisement

SQL: Help creating a table based on another tables value

I am new to SQL and am currently doing a project on MSQL 2019.

I have one table with a list of codes mapping a “Loss ID” to a Reason.

enter image description here

Now I have another table that just gives you the number of the Loss ID but not the text version of it. enter image description here

How can I create a table that will take the number code and automatically change it to the text version of it?

Im not even sure what this type of scripting is called, so even pointers on what to google or look for would be very useful.

Advertisement

Answer

Rather than creating another table use a view.

Let’s call your tables tbl1 and tbl2. The view would be:

CREATE VIEW my_view AS SELECT tbl2.your_date_column, 
                              tbl2.LossID,
                              tbl1.reason
                       FROM tbl1 
                       INNER JOIN tbl2 on  tbl1.LossID=tbl2.LossID;

Lear more about VIEWS https://www.mysqltutorial.org/create-sql-views-mysql.aspx

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement