简体   繁体   English

MySQL 使用主键从视图创建表

[英]MySQL CREATE TABLE FROM VIEW with PRIMARY KEY

I have the following MySQL view:我有以下 MySQL 视图:

在此处输入图片说明

And am trying to write an SQL script to create a table populated with data from the view while assigning a primary key.并且我正在尝试编写一个 SQL 脚本来创建一个表,该表在分配主键时填充了视图中的数据。

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:这给了我以下结果:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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