Postgresql Union组合两个表



我有两个相似的表,但新的&老了。

table01老

<表类> id 客户 产品数量tbody><<tr>001Cust001Soap200002Cust002洗发水23003Cust003番茄酱30

demo
PostgreSQL DISTINCT ON with different ORDER BY

WITH cte AS (
(
SELECT
*
FROM
customer1
ORDER BY
quantity)
UNION ALL (
SELECT
*
FROM
customer2
ORDER BY
quantity))
SELECT DISTINCT ON (id, customer, product)
*
FROM
cte
ORDER BY
1,
2,
3,
4 DESC;

UNION ALL使表中的所有行都在那里,然后在删除重复项时区分,使用ORDER BY来决定哪一行被"保存";

最新更新