我想(直接)生成一个包含另一个表的2列内容的表。我如何更改列的名称(在新表中重命名)?
以下是一个例子:
CREATE TABLE X AS
SELECT
Table1.name,
Table1.act
FROM Y
- &gt;我不想在原始表格中将“name”和“act”列命名为 - 我希望将“name”替换为“customer”,将“act”替换为“status”< / p>
答案 0 :(得分:2)
这样做:
CREATE TABLE X AS
SELECT
Table1.name as customer,
Table1.act as status
FROM Y
答案 1 :(得分:1)
您只需在查询中指定名称别名:
CREATE TABLE X AS
SELECT
Table1.name AS customer,
Table1.act AS status
FROM Y
或者,您可以在表名后面的括号中指定列定义:
CREATE TABLE X (customer VARCHAR(10), status VARCHAR(10)) AS
SELECT
Table1.name,
Table1.act
FROM Y
答案 2 :(得分:0)
您需要使用While循环来执行此操作:
$my_query = "SELECT * FROM tj_ethnicity FROM Table_X";
while($row = mysql_fetch_assoc($my_query))
{
$new_query = "CREATE TABLE `."$row['customer']".` (
`customerid` int(11) NOT NULL AUTO_INCREMENT,
`status` varchar(50) DEFAULT ."$row['status']".,
`created` datetime NOT NULL,
PRIMARY KEY (`customerid`)
)";
}