简体   繁体   English

根据 table3 中的条件从 table2 插入 table1

[英]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用户 1 555-123456 555-123456 true真的
user2用户 2 555-000123 555-000123 false错误的
user3用户 3 null空值 false错误的

Table datesTimes日期时间

date日期 userame用户名
2021-10-10 2021-10-10 user1用户 1
2021-10-10 2021-10-10 user2用户 2
... ... ... ...

Table out_notificationsout_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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM