I have two tables having different columns and two columns are common.
Some of records are same in both the tables. Need to query to fetch all two table records with joining column,
Used MySQL database.
Table1 – T1
transaction_id, device_id, is_valid, description
123 DEV001 1 valid device
123 DEV002 0 Device doesn't exist
Table – T2
transaction_id, device_id, error, status, store_id
123 DEV001 valid device Success 12
The query should return below records,
123 DEV001 1 valid device Success 12
123 DEV002 0 Device doesn't exist Failed
123 DEV001 1 valid device Success 12
The first table holds both valid and invalid devices based on column is_valid but second table contains only valid devices with no column is_valid.
So, I need all records from second table and first table need only is_valid false records based on filter column transaction_id.
2
Answers
It seems you need following query. Since we’re always fetching invalid item from
t1
, we can hardcodestatus
column asFailed
& sincet2
always contains valid item, we can hardcodeis_valid
as1
.UNION ALL
the tables. Use window functions to copy T2 values to corresponding T1 rows where absent.