sqlphppgadmin

Printing the same column twice


How can I print the 'ID' column twice. I've already got it in the start of the table, and also want it to also print out as the last column again for easy viewing purposes.

ALTER TABLE EMPLOYEES
ADD ID int;

'ID already exists'

Also, when I update 'ID' I want both columns to be updated at once.

Thank you


Solution

  • If you want to have another copy of a column, you can always use it in the SELECT statement for more than once

    SELECT ID, ID
    FROM EMPLOYEES
    

    If you insist in add it to the table structure, you have to change the name

    ALTER TABLE EMPLOYEES
    ADD ID2 int;
    

    To update them both, you have to specify that in the update statement

    UPDATE EMPLOYEES
    SET ID = /*Whatever*/
    ,ID2 = /*Whatever*/
    

    OR

    you can create a trigger to update ID2 every time ID is updated