从多个表创建表并添加新列

时间:2019-04-19 12:27:57

标签: sql

我有三个表格的托运人,供应商和测验。这些表的列为:

测验:

id(PK)
name
Origin
Destination
total_trucks
material_type
scheduled_date
offered_price
owner_id
subject_id 

供应商:

user_id
name
company_name
email
gst
Pan
address
origin_city
service

托运人:

user_id
fname
lname
email
company_name
gst
pan
address 
city

我想使用所有这三个表创建一个新表,并在新表中添加一些新列。我可以用这个实现吗?:

CREATE TABLE new_table
    (
     new columns and their type
    )
    AS
    (
     SELECT columns from three tables
     FROM shipper,supplier,quiz
    );

新表的列为:

id
number
date
name(from shipper)
amount
origin(from quiz)
destination(from quiz)
name(from supplier)
amount
some other columns

我又如何调整列的顺序?

2 个答案:

答案 0 :(得分:1)

我认为您需要查看

package main

import (
    "fmt"
    "io"
    "net/http"
    "os"
    "strconv"
)

func main() {
    url_id_num := 23430815+2
    for i := 0; i < 10; i++ {
        url_id := strconv.Itoa(url_id_num+i)
        filename := url_id+".pdf"
        fileUrl := "https://someurLid="+url_id

        if err := DownloadFile(filename, fileUrl); err != nil {
            panic(err)
        }
        fmt.Println(fileUrl)
    }
}

func DownloadFile(filepath string, url string) error {

    resp, err := http.Get(url)
    if err != nil {
        return err
    }
    defer resp.Body.Close()

    out, err := os.Create(filepath)
    if err != nil {
        return err
    }
    defer out.Close()

    _, err = io.Copy(out, resp.Body)
    return err
}

答案 1 :(得分:0)

从select创建表的语法很简单:

CREATE TABLE new_table AS
   SELECT columns from joined tables;   -- FROM shipper,supplier,quiz

新表继承了所选属性的列属性,因此您不必包括类型定义。

请注意不同表之间的列名称相同。如果您希望多次包含它们,则必须使用别名。