[英]Insert into table1 from table2 based on conditions from table3
Hello i have some problem with this:你好,我有一些问题:
I have 3 tables我有3张桌子
Table users :表用户:
username![]() |
tele![]() |
notify![]() |
---|---|---|
user1![]() |
555-123456 ![]() |
true![]() |
user2![]() |
555-000123 ![]() |
false![]() |
user3![]() |
null![]() |
false![]() |
Table datesTimes表日期时间
date![]() |
userame![]() |
---|---|
2021-10-10 ![]() |
user1![]() |
2021-10-10 ![]() |
user2![]() |
... ![]() |
... ![]() |
Table out_notifications表out_notifications
telephone![]() |
message![]() |
---|
Now what i want insert data into table out_notifications from table datesTimes only for users which have telephone number and notification turned on.现在我想要将数据从表datesTimes 插入表out_notifications 中,仅适用于已打开电话号码和通知的用户。
So i have constructed following query:所以我构建了以下查询:
INSERT INTO out_notifications (telephone,message)
SELECT (select tele from users where users.username = datesTimes.username),
'some message' --(generated by system)
FROM datesTimes
WHERE date = '2021-10-10';
Now where place a subquery which select only users with tel.现在在哪里放置一个子查询,它只选择具有电话的用户。 number and notifications turned on ?
号码和通知打开了吗?
Used db PostgreSQL使用数据库 PostgreSQL
Thanks for any help.谢谢你的帮助。
use a join instead of a subselect.使用连接而不是子选择。
INSERT INTO out_notifications (telephone,message)
SELECT users.tele,
'some message' --(generated by system)
FROM users
JOIN datesTimes
ON users.username = datesTimes.username,
WHERE datesTimes.date = '2021-10-10'
AND users.tele <> ''
AND users.notify;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.