Skip to content
Advertisement

Is there a SQL syntax that will create new column by searching in 2 columns based on equal value of 3rd column in same table?

I have 1 table, with fields named: class_code, name1, class_code1, name2, class_code2. I would like to search the value of class_code, and display it as class_code and name only, where class_code = class_code1 or class_code = class_code2. The results will display class_code and name

Is this possible using sql join syntax?

This is the table:

    class_code  name1     class_code1   name2     class_code2
    C0001       John      C0002         Ben       C0001
    C0002       Ren       C0001         Elizh     C0002
    C0001       Jeff      C0001         Harry     C0001

The output I need is:

    class_code        name
    C0001             Ben
    C0002             Elizh
    C0001             Jeff
    C0001             Harry

Advertisement

Answer

If I got it right

SELECT class_code, name1 
FROM tbl
WHERE class_code = class_code1 
UNION --ALL
SELECT class_code, name2
FROM tbl
WHERE class_code = class_code2
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement