dongyi5817 2016-12-02 14:29
浏览 57
已采纳

将不同表中的不同字段存储到临时表中

I am trying to create a temporary table where it has two fields (user,pass) which was a combination from three tables

TABLES

agents
username | password

clients
username | password

admin
username | password

i want their data from their fields to be stored to a temporary table

TEMPORARY TABLE

temp_tbl
username | password

This is my MySQL query (i know this is a wrong syntax)

CREATE TEMPORARY TABLE temp_tbl ENGINE=MEMORY 
AS (SELECT username,password FROM agents UNION SELECT username,password FROM clients UNION SELECT username,password FROM admin) AS col1, col2;
  • 写回答

3条回答 默认 最新

  • dsds33222 2016-12-02 14:37
    关注

    Pretty close

    CREATE TEMPORARY TABLE temp_tbl ENGINE=MEMORY 
    AS SELECT * FROM 
     (SELECT username,password FROM agents 
     UNION SELECT username,password FROM clients 
     UNION SELECT username,password FROM admin) AS t1;
    

    You have to wrap those three unions inside a another SELECT and you need to give that derived table an alias. Just one name please not two (t1 instead of col1, col2)

    Unless you wrap the whole thing aaround another query as I have done, you will get a syntax error.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献