如何仅在一个请求中创建 mysql table 和编辑(或选择)某些列的名称
How to create mysql table and edit (or choose) the name of some columns, in only one request
我有一个 table "cmd-services",我使用以下 SQL 请求创建一个新的 table "list-poles",其中包含从 "cmd-services" table。
我使用了这个查询:
CREATE TABLE `list-poles` AS SELECT `Code P`, `Name P`, count(`CodeP`), sum(`Qte Sortie`) FROM hospital.`cmd-services` group by `Code P` order by `Code P`;
问题是在新的 table "list-poles" 中,我得到名为 "count(Code P
)" 和 "sum(Qte Sortie
)" 的列。要更改这两列的名称,我需要执行另外两个查询:
alter table `list-poles` change `count(`Code P`)` `nbre cmds par an` int;
alter table `list-poles` change `sum(``Qte Sortie``)` `Qte cmds par an` double;
我的问题是:当我创建 table 时,如何在第一个请求中直接选择这两个 table 的名称?
感谢您的帮助。
此致,
我会为这些列使用 ALIAS。
CREATE TABLE `list-poles` AS
SELECT
`Code P`,
`Name P`, -- You probably should have `MIN` or `MAX` or something here
-- Or include `Name P` in the GROUP BY
COUNT(`CodeP`) AS `nbre cmds par an`, -- AS lets you name the column
SUM(`Qte Sortie`) AS `Qte cmds par an` -- AS lets you name the column
FROM
hospital.`cmd-services`
GROUP BY
`Code P`
ORDER BY
`Code P`
;
我有一个 table "cmd-services",我使用以下 SQL 请求创建一个新的 table "list-poles",其中包含从 "cmd-services" table。 我使用了这个查询:
CREATE TABLE `list-poles` AS SELECT `Code P`, `Name P`, count(`CodeP`), sum(`Qte Sortie`) FROM hospital.`cmd-services` group by `Code P` order by `Code P`;
问题是在新的 table "list-poles" 中,我得到名为 "count(Code P
)" 和 "sum(Qte Sortie
)" 的列。要更改这两列的名称,我需要执行另外两个查询:
alter table `list-poles` change `count(`Code P`)` `nbre cmds par an` int;
alter table `list-poles` change `sum(``Qte Sortie``)` `Qte cmds par an` double;
我的问题是:当我创建 table 时,如何在第一个请求中直接选择这两个 table 的名称?
感谢您的帮助。 此致,
我会为这些列使用 ALIAS。
CREATE TABLE `list-poles` AS
SELECT
`Code P`,
`Name P`, -- You probably should have `MIN` or `MAX` or something here
-- Or include `Name P` in the GROUP BY
COUNT(`CodeP`) AS `nbre cmds par an`, -- AS lets you name the column
SUM(`Qte Sortie`) AS `Qte cmds par an` -- AS lets you name the column
FROM
hospital.`cmd-services`
GROUP BY
`Code P`
ORDER BY
`Code P`
;