从另一个表更新具有字段布尔要求的表

时间:2017-01-10 12:21:36

标签: sql postgresql

我有2张桌子

表1

id   kode(boolean)
-----------------
1    false
2    false
3    true

表2

id   num
1    499
2    390
3    500

结果: table1

id   kode(boolean)
------------------
1    True
2    True
3    True

我想在table2.num < 500

时更新table1 kode = true

如何为Postgres做到这一点

1 个答案:

答案 0 :(得分:0)

您可以执行如下所示的更新联接。虽然不确定它是SQL Server还是Postgres,因为评论SQL Server没有boolean类型而不是bit UPDATE t1 SET t1.kode = true FROM table1 t1 JOIN table2 t2 ON t1.id = t2.id WHERE t2.num < 500; }字段。

UPDATE table1 AS t1 
SET kode = true
FROM table2 AS t2
WHERE t1.id = t2.id AND t2.num < 500;

这是Postgres语法:

SELECT 
    10 * (customer_x / 10),
    10 * (customer_y / 10),
    COUNT (*)
FROM t_customer
GROUP BY 
    customer_x / 10,
    customer_y / 10 
ORDER BY 3 DESC;