在 MySQL 查询中使用 @var 作为 table 前缀的语法
Syntax for using @var for table prefix in MySQL query
我想使用 @var
来存储 table 前缀,例如:
SET @prefix = 'wp_';
要在下面的查询中创建 table 名称 wp_users
(wp_
加上 users
)和 wp_usermeta
:
SET @username = 'username';
SET @password = 'P@55W0rd';
SET @email = 'em@il.com';
INSERT INTO `wp_users` (`ID`, `user_login`, `user_pass`, `user_nicename`, `user_email`, `user_url`, `user_registered`, `user_activation_key`, `user_status`, `display_name`)
VALUES
(NULL , @username, MD5(@password), '', @email, '', NOW(), '', '0', '');
INSERT INTO `wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`)
VALUES
(NULL, LAST_INSERT_ID(), 'wp_capabilities', 'a:1:{s:13:"administrator";s:1:"1";}'),
(NULL, LAST_INSERT_ID(), 'wp_user_level', '10');
我怎样才能做到这一点?我找不到任何使用 @var
作为 table 名称一部分的示例。
我会批评你只是因为我向 google 询问 "mysql variable syntax" 并且得到了正确的页面,
https://dev.mysql.com/doc/refman/5.0/en/user-variables.html:
You can store a value in a user-defined variable in one statement and
then refer to it later in another statement. This enables you to pass
values from one statement to another.
User variables are written as @var_name, where the variable name
var_name consists of alphanumeric characters, “.”, “_”, and “$”. A
user variable name can contain other characters if you quote it as a
string or identifier (for example, @'my-var', @"my-var", or
@my-var
).
*根据 this 无法完成:
SQL variables can be used only where expressions are allowed, not
where constants or literal identifiers must be provided. Although it's
tempting to attempt to use variables for such things as table names,
it doesn't work.
Last modified on Saturday, 05 December 2009 14:35
- 顺便说一句,搜索“变量只能在允许表达式的地方使用”会产生相同内容的大量副本——尽管这似乎超出了这个问题的范围确定来源。
FWIW - 我确实使用变量作为 table 名称进行了测试:
SET @userstable = 'wp_users';
INSERT INTO `@userstable` [...]
INSERT INTO @userstable [...]
并分别得到以下错误:
1146 - Table '[dbname].@userstable' doesn't exist
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@userstable
... 使事情复杂化 - 我确实发现 this post relating to stored procedures 和 mysql。但相同的语法在 INSERT 上失败。
唉,当通过 phpMyAdmin 将用户添加到 WordPress 数据库时,我只需要手动编辑所有 table 前缀。
我想使用 @var
来存储 table 前缀,例如:
SET @prefix = 'wp_';
要在下面的查询中创建 table 名称 wp_users
(wp_
加上 users
)和 wp_usermeta
:
SET @username = 'username';
SET @password = 'P@55W0rd';
SET @email = 'em@il.com';
INSERT INTO `wp_users` (`ID`, `user_login`, `user_pass`, `user_nicename`, `user_email`, `user_url`, `user_registered`, `user_activation_key`, `user_status`, `display_name`)
VALUES
(NULL , @username, MD5(@password), '', @email, '', NOW(), '', '0', '');
INSERT INTO `wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`)
VALUES
(NULL, LAST_INSERT_ID(), 'wp_capabilities', 'a:1:{s:13:"administrator";s:1:"1";}'),
(NULL, LAST_INSERT_ID(), 'wp_user_level', '10');
我怎样才能做到这一点?我找不到任何使用 @var
作为 table 名称一部分的示例。
我会批评你只是因为我向 google 询问 "mysql variable syntax" 并且得到了正确的页面, https://dev.mysql.com/doc/refman/5.0/en/user-variables.html:
You can store a value in a user-defined variable in one statement and then refer to it later in another statement. This enables you to pass values from one statement to another.
User variables are written as @var_name, where the variable name var_name consists of alphanumeric characters, “.”, “_”, and “$”. A user variable name can contain other characters if you quote it as a string or identifier (for example, @'my-var', @"my-var", or @
my-var
).
*根据 this 无法完成:
SQL variables can be used only where expressions are allowed, not where constants or literal identifiers must be provided. Although it's tempting to attempt to use variables for such things as table names, it doesn't work.
Last modified on Saturday, 05 December 2009 14:35
- 顺便说一句,搜索“变量只能在允许表达式的地方使用”会产生相同内容的大量副本——尽管这似乎超出了这个问题的范围确定来源。
FWIW - 我确实使用变量作为 table 名称进行了测试:
SET @userstable = 'wp_users';
INSERT INTO `@userstable` [...]
INSERT INTO @userstable [...]
并分别得到以下错误:
1146 - Table '[dbname].@userstable' doesn't exist
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@userstable
... 使事情复杂化 - 我确实发现 this post relating to stored procedures 和 mysql。但相同的语法在 INSERT 上失败。
唉,当通过 phpMyAdmin 将用户添加到 WordPress 数据库时,我只需要手动编辑所有 table 前缀。