dongzhi4690 2012-09-25 14:32
浏览 116
已采纳

MySQL:使用单个SQL命令将数据库A中的多行插入到数据库B中

I have a database with table named 'Customers' which it contains customer's phone number. Its definitely more than one row.

On the other hand, I also have database named 'SMS' with a table in it, called 'Outbox'.

In order to show all phone number from 'Customers' table, I use this SQL query :

SELECT Customers.PhoneNumber FROM Customers WHERE PurchaseDate BETWEEN 2012-01-01 AND 2012-01-31;

Now, I need those phone numbers (multiple rows) to be inserted into 'Outbox' at once. Every phone number will receive same message. so, basically I just need to know how to use this INSERT query for multiple rows :

INSERT INTO Outbox (PhoneNumber, Message) VALUES (*Multiple PhoneNumbers Here*, 'Same Message To All Customers');

any idea how to do it? thanks before.

  • 写回答

4条回答 默认 最新

  • duanhao4909 2012-09-25 14:37
    关注

    I assume you want a separate row for each phone number. You can do this by combining INSERT with SELECT:

    INSERT INTO db2.Outbox (PhoneNumber, Message)
        SELECT Customers.PhoneNumber, 'Same Message To All Customers' as message
        FROM db1.Customers
        WHERE PurchaseDate BETWEEN 2012-01-01 AND 2012-01-31;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含