简体   繁体   中英

MySQL CREATE TABLE FROM VIEW with PRIMARY KEY

I have the following MySQL view:

在此处输入图片说明

And am trying to write an SQL script to create a table populated with data from the view while assigning a primary key.

I managed to accomplish this using a script similar to that below:

CREATE TABLE `tmp_div_numprojects`(
`dn_id` INT(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`dn_id`), KEY(`managing_division`))
SELECT
`managing_division` AS `managing_division`,
COUNT(DISTINCT `project_id`) AS `number_of_projects`
FROM
    `report_view`
GROUP BY
    `managing_division`

This gives me the following result:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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