I have Four table, Now I display four common field using UNION sql query.
Question : I want to display one uncommon field ( Company ) which in table name as Multiple Product with current result.How can I do it ?
Current SQL :
SELECT * FROM ( SELECT reference_number,name,contact_no,email,create_date,'Credit Card' AS card_type FROM credit_card UNION SELECT reference_number,name,contact_no,email,create_date,'Personal Loan' AS card_type FROM personal_loan UNION SELECT reference_number,name,contact_no,email,create_date,'Financial Health' AS card_type FROM financial_health UNION SELECT reference_number,name,contact_no,email,create_date,'Multiple Product' AS card_type FROM multiple_products ) A
Advertisement
Answer
You can just add an empty (or NULL
) field to the other SELECT
queries in the space where you want to return the Company
field.
SELECT * FROM ( SELECT reference_number, name, contact_no, email, create_date, 'Credit Card' AS card_type, '' AS company -- use NULL here if you prefer FROM credit_card UNION SELECT reference_number,name,contact_no,email,create_date,'Personal Loan' , '' FROM personal_loan UNION SELECT reference_number,name,contact_no,email,create_date,'Financial Health' , '' FROM financial_health UNION SELECT reference_number,name,contact_no,email,create_date,'Multiple Product' , Company FROM multiple_products ) A
Note that you only need column aliases on the first SELECT
.