mysql - 错误 1064 : combining multiple strings in Concat() function:MySQL

标签 mysql

我无法执行以下过程并遇到错误:

1064 Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'aid=ass333' at line 1

CREATE DEFINER=`root`@`localhost` PROCEDURE `ref_table`()
BEGIN

declare t_name varchar(20);
declare aid1 varchar(20);
declare get_email varchar(20);

set @aid1 := 'ass333';
set @t_name := (select table_name from aid_ref where aid=@aid1);

set @get_email= CONCAT("SELECT email FROM ",@t_Name,"where aid=",@aid1);

Prepare stmt FROM @get_email;

Execute stmt;

End

如果我执行相同的过程而不使用 (,"where help=",@aid1);在 concat 函数中,我获取函数中所有电子邮件的列表,但是我需要从此过程中获取特定的电子邮件 ID

set @get_email= CONCAT("SELECT email FROM ",@t_Name,"where aid=",@aid1);

最佳答案

问题可能出在这一行:

set @get_email = CONCAT("SELECT email FROM ", @t_Name, "where @aid=", @aid1);

有人怀疑您缺少空格:

set @get_email = CONCAT("SELECT email FROM ", @t_Name, " where @aid=", @aid1);
--------------------------------------------------------^

如果值是通过用户输入提供的,那么您应该使用参数化查询。

编辑:

据推测,您的问题是值周围缺少单引号:

set @get_email = CONCAT("SELECT email FROM ", @t_Name, " where @aid = '", @aid1, "'");

请注意,如果要将参数值传递到查询字符串中,则应该使用参数化查询。

关于mysql - 错误 1064 : combining multiple strings in Concat() function:MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37482404/

相关文章:

php - 如何使用 LOAD DATA LOCAL INFILE REPLACE

mysql - 新项目 : MySQL or SQL 2005 Express

PHP MYSQL 返回字段中的数组

mysql - 如何在使用like搜索时使mysql查询速度更快

java - 如何在非主键上使用 hibernate 注释设置单向一对一关系?

mysql - 如何将 MySQL 表更改为 UTF-8?

java - SQLexecuteBatch处理速度慢

MySQL:合并两个表(相同模式)并创建一个新表 - SINGLE QUERY

php - MySQL 替换每条记录的值

php - PDO FetchAll从mysql回显表中的数据