簡體   English   中英

根據 table3 中的條件從 table2 插入 table1

[英]Insert into table1 from table2 based on conditions from table3

你好,我有一些問題:

我有3張桌子

用戶

用戶名 電話 通知
用戶 1 555-123456 真的
用戶 2 555-000123 錯誤的
用戶 3 空值 錯誤的

日期時間

日期 用戶名
2021-10-10 用戶 1
2021-10-10 用戶 2
... ...

out_notifications

電話 信息

現在我想要將數據從表datesTimes 插入表out_notifications 中,僅適用於已打開電話號碼和通知的用戶。

所以我構建了以下查詢:

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';

現在在哪里放置一個子查詢,它只選擇具有電話的用戶。 號碼和通知打開了嗎?

使用數據庫 PostgreSQL

謝謝你的幫助。

使用連接而不是子選擇。

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