0

I have 2 tables

Table1

 ID . Name . Position  
= = = = = = = = = = = =  
10 . Mike . Analyst  
20 . Anna . HR  
30 . Mark . Accountant 

Table2

 Deal ID . Status  
= = = = = = = = = = = =  
10 . . . . .  Active  
19 . . . . . New  
20 . . . . . New  

I want to add a new Calculated Column in Table1 with this logic :

If ID found in Table2 then return Position, ELSE return "NONE"
so the output table should be like this

Outout

ID . Name . Position . . . . **NewCol**  
= = = = = = = = = = = = = = = = =  
10 . Mike . Analyst . . . . . **Analyst**  
20 . Anna . HR . . . . . . . . **HR**  
30 . Mark . Accountant. . **NONE**  
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
  • There are so many solution available on SO? – Gajendra K Chauhan Nov 05 '15 at 06:40
  • 1
    Possible duplicate of [Retrieving the last record in each group](http://stackoverflow.com/questions/1313120/retrieving-the-last-record-in-each-group) – Gajendra K Chauhan Nov 05 '15 at 06:40
  • I have tried this ALTER TABLE Table1 ADD newcolumn AS Case When Table1.ID in Table2.Deal ID then Table1.position else 'None' end - But its not working – Maged Zaki Nov 05 '15 at 07:06
  • Sorry, I'm new to SQL coding - any answer will help. and what is SO ? – Maged Zaki Nov 05 '15 at 07:09
  • I'm not sure you can accomplish what you need with a generated column, see `Generated column expressions must adhere to the following rules.` in [13.1.14 CREATE TABLE Syntax](http://dev.mysql.com/doc/refman/5.7/en/create-table.html#create-table-generated-columns). One option is to use a base column (nongenerated) and a trigger. – wchiquito Nov 05 '15 at 10:02
  • I have updated my Question with table examples .. thank you – Maged Zaki Nov 05 '15 at 15:43

1 Answers1

0

There are two ways to accomplish that.

Query Based Result

If you just want to display the information every time you need it, the simplest, cleanest and efficient way is just to perform a SELECT within the tables. In addition, the data will allways be updated, because the query runs over the actual table state.

So, the query would look like this:

SELECT T1.*, IF(T2.ID IS NULL, 'NONE', T1.Position) As NewPos
FROM Table1 T1 LEFT OUTER JOIN Table2 T2
ON (T1.ID = T2.ID)

This query will show the position if found on Table2 and, if not, it will display NULL value, so it may be useful for your needs.


Database Modification and Data Inserting

The other way, is to alter the Table1 structure to add the column Position. The existence of the column is referred to the table architecture, it is not value dependant, so you can't alter a Table and adding the column based on row values.

So, the first step to do is to alter the table adding the column, something like this:

ALTER TABLE table1 ADD COLUMN Position VARCHAR(50);

The second step is to fill the data. For this, you will have to perform an UPDATE to fill the data you have just created, something like this.

UPDATE Table1 T1 LEFT OUTER JOIN Table2 T2
ON (T1.ID = T2.ID)
SET T1.Position = IF(T2.ID IS NULL, 'NONE', T1.Position);

This query will update the rows which also exists in Table2 referenced by ID and will put its position.

The problem of this way it's that if you perform this and after, you add rows to Table1 and Table2, the information will not be updated and you'll have to do the UPDATE query every certain time, which is database and time cost.

So, if the application is not too big, in this case is better to use just the SELECT query, which involves less cost, performance and database changes.

  • Thank you for your time, But this answer will not give me the needed output table above, I have updated my question with examples. – Maged Zaki Nov 05 '15 at 15:42
  • Hi @MagedZaki, I've updated the answer. Now, instead of NULL, it shows the 'NONE' String. Does it do the trick for you? – David de los Santos Boix Nov 05 '15 at 15:46
  • A computed column is added to table and whenever record is fetched is calculated again or its value persist which means it will be calculated once. What Question is asking is how to add that specific column. Your query returns more than one value and your update doesn't fit in definition of `computed column` – AaA Nov 14 '20 at 15:31