我有两个表,vessel_details和vessel_history。两个表都具有相同的字段。我必须比较所有字段的值,并选择具有不同值的列而不是null。例如,ship_name字段,我必须检查vessel_details表中字段ship_name的值是否与vessel_history表中的ship_name不同。如果值不同且不为null,则必须选择并显示vessel_history中的值。
正确查询的任何想法? 感谢。
答案 0 :(得分:1)
SELECT vessel_details.<your_id>, 'Difference in Column ship_name for ship_name = ' + vessel_history.ship_name
FROM vessel_details
JOIN vessel_history ON vessel_details.<your_id> ON vessel_history.<id_from_vessel_details>
WHERE vessel_details.ship_name <> vessel_history.ship_name
UNION
SELECT vessel_details.<your_id>, 'Difference in Column ship_model for ship_model = ' + vessel_history.ship_model
FROM vessel_details
JOIN vessel_history ON vessel_details.<your_id> ON vessel_history.<id_from_vessel_details>
WHERE vessel_details.ship_model <> vessel_history.ship_model
.
.
.
对于您要检查的所有列,等等。
我想找你的是什么?
每个项目编辑一行:
SELECT vessel_details.<your_id>,
CASE WHEN vessel_details.ship_name <> vessel_history.ship_name THEN Convert(bit, 1) ELSE Convert(bit, 0) END AS ship_name_different,
CASE WHEN vessel_details.ship_model <> vessel_history.ship_model THEN Convert(bit, 1) ELSE Convert(bit, 0) END AS ship_model_different
FROM vessel_details
JOIN vessel_history ON vessel_details.<your_id> ON vessel_history.<id_from_vessel_details>
WHERE vessel_details.ship_name <> vessel_history.ship_name
OR vessel_details.ship_model <> vessel_history.ship_model