mysql语句用于添加多个列,无论它们是否存在
我正在尝试为软件程序编写更新 mysql 语句。目前,这就是我的声明,但我现在不知道如何添加多个列来更新。
此代码当前会添加该列,无论该列是否存在。
/*delimiter '//'
CREATE PROCEDURE addcol() BEGIN
IF NOT EXISTS(
SELECT * FROM information_schema.COLUMNS
WHERE COLUMN_NAME=`top_status` AND TABLE_NAME='categories'
)
THEN
ALTER TABLE `categories`
ADD COLUMN `top_status` tinyint(1) NOT NULL default '1';
END IF;
END;
delimiter ';'
CALL addcol();
DROP PROCEDURE addcol;*/
有人可以向我提供正确的声明来让这样的事情发挥作用......即。 WHERE COLUMN_NAME = column1、column2 等。
我尝试了各种变体,除了上述之外,所有变体都在 phpmyadmin 中返回错误。
I'm trying to write an update mysql statement for a software program. Currently this is what I have for the statment, but I don't now how to add multiple columns to update.
This code currently adds the column whether it's there or not.
/*delimiter '//'
CREATE PROCEDURE addcol() BEGIN
IF NOT EXISTS(
SELECT * FROM information_schema.COLUMNS
WHERE COLUMN_NAME=`top_status` AND TABLE_NAME='categories'
)
THEN
ALTER TABLE `categories`
ADD COLUMN `top_status` tinyint(1) NOT NULL default '1';
END IF;
END;
delimiter ';'
CALL addcol();
DROP PROCEDURE addcol;*/
Can someone supply me with the correct statement to get something like this to work...ie. WHERE COLUMN_NAME = column1, column2 etc..
I've tried all kinds of variations and all of them return an error in phpmyadmin except the above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 ALTER TABLE 语句向表中添加多列:
Use ALTER TABLE statement to add multiple columns to a table:
您确实想单独测试新列是否存在,并单独添加每个新列(如果尚不存在)?
如果您希望
INFORMATION_SCHEMA
查询测试列column1、column2 等任何是否存在,您可以使用IN
语句。如果您想测试它们是否全部存在,那么您可以使用IN
,而不是检查结果集是否为空,而是选择COUNT(*)
并检查返回的计数是否是预期的数量(涉及的列数)。Surely you want to individually test for the existence of the new columns, and add each one individually if it doesn't already exist?
If you want the
INFORMATION_SCHEMA
query to test for the existence of any of the columns column1, column2 etc. you can use anIN
statement. If you want to test whether they all exist then you can useIN
and instead of checking whether the resultset is empty, select aCOUNT(*)
and check whether the count returned is the expected number (the number of columns involved).