我有两个表,每个表有四列。两者都可能包含重复的行。我想在一个表中存储匹配的条目,而在另一个表中存储不匹配的条目。 这是我正在尝试的代码,但没有得到所需的输出。
{{1}}
答案 0 :(得分:1)
查找匹配的行:
SELECT
`vendor`.`date`,
`vendor`.`truckno`,
`vendor`.`bagscount`
FROM `vendor`
INNER JOIN `truck`
ON `vendor`.`date`=`truck`.`date`
AND `vendor`.`truckno`=`truck`.`truckno`
AND `vendor`.`bagscount`=`truck`.`bagscount`
查找vendor
中不在truck
SELECT
`vendor`.`date`,
`vendor`.`truckno`,
`vendor`.`bagscount`
FROM `vendor`
LEFT JOIN `truck`
ON `vendor`.`date`=`truck`.`date`
AND `vendor`.`truckno`=`truck`.`truckno`
AND `vendor`.`bagscount`=`truck`.`bagscount`
WHERE
`truck`.`date` IS NULL
查找truck
中不在vendor
SELECT
`truck`.`date`,
`truck`.`truckno`,
`truck`.`bagscount`
FROM `truck`
LEFT JOIN `vendor`
ON `truck`.`date`=`vendor`.`date`
AND `truck`.`truckno`=`vendor`.`truckno`
AND `truck`.`bagscount`=`vendor`.`bagscount`
WHERE
`vendor`.`date` IS NULL
左连接(最后两个)假设date
永远不为空。
您也可以通过在INSERT INTO table_name SELECT ...{rest of select query}...
选择前面插入另一个表。
答案 1 :(得分:0)
在SQL查询中使用JOIN
。
SELECT *
FROM vendor V
JOIN trucks T ON
V.key = T.key AND V.key2 = T.key2 ....