【问题标题】:convert int to use it with concat()将 int 转换为与 concat() 一起使用
【发布时间】:2014-01-28 03:10:27
【问题描述】:

我写了这个 sql 语句:

SELECT concat(
            'INSERT INTO dev_sys_index (name,tableid,descrition) VALUES (idx_',
            name,
            ',',
            id,
            ',eindeutige Kombination aus abfid und parentid);') 
    FROM dev_sys_tables 
    WHERE datenmodell=350 

但是输出只是一堵十六进制数字,因为id 是一个整数。我尝试了很多方法,例如将 id 转换为字符串 CAST( id as varchar).,但没有任何效果。我仍然收到此错误消息。

错误:您的 SQL 语法有错误;检查手册 对应于您的 MySQL 服务器版本,以便使用正确的语法 靠近'varchar ),',eindeutige Kombination aus abfid und parentid);'

)

任何帮助将不胜感激!

工作正常:

concat(
    'INSERT INTO dev_sys_index (name,tableid,descrition) VALUES (idx_',
    name,
    ',',
    cast(id as char),
    ',eindeutige Kombination aus abfid und parentid);') 

只得到一堵十六进制数的墙:

concat(
    'INSERT INTO dev_sys_index (name,tableid,descrition) VALUES (idx_',
    name,
    ',',
    id,
    ',eindeutige Kombination aus abfid und parentid);')

更新 问题解决了。我必须使用cast(whatever as char) 而不是cast(whatever as varchar)

【问题讨论】:

  • 你到底想做什么?
  • 我正在尝试使用 concat 创建一个插入语句,而 concat 只接受字符串变量,因此主要问题是将 id 从 int 转换为 string 以在此 concat 语句中使用它。
  • mysql中的CONCAT可以接受整数作为参数。 CONCAT(1, 2) 会给12
  • 我知道....这是我的问题
  • 我试试这个SELECT concat('INSERT INTO dev_sys_index (name,tableid,descrition) VALUES (idx_',"asd",',',1,',eindeutige Kombination aus abfid und parentid);'),它的工作原理.....

标签: mysql sql concat


【解决方案1】:

您根本不需要转换或转换(无论如何您应该使用cast(whatever as char) 而不是varchar)。

您在eindeutige Kombination aus abfid und parentid 附近缺少",否则您的插入语句将不起作用:

SELECT concat('INSERT INTO dev_sys_index (name,tableid,descrition) VALUES (idx_',name,',',id,',"eindeutige Kombination aus abfid und parentid");') 
FROM dev_sys_tables 
WHERE datenmodell=350 

P.S.:这是manual entry 声明

结果的类型可以是以下值之一:

  • 二进制[(N)]
  • CHAR[(N)]
  • 日期
  • 日期时间
  • 十进制[(M[,D])]
  • 签名 [整数]
  • 时间
  • 无符号[整数]

证明:

mysql> select id, id from tasks;
+------+------+
| id   | id   |
+------+------+
|    1 |    1 |
|    2 |    2 |
|    3 |    3 |
+------+------+
3 rows in set (0.00 sec)

mysql> SELECT concat('INSERT INTO dev_sys_index (name,tableid,descrition) VALUES (idx_',id,',',id,',"eindeutige Kombination aus abfid und parentid");') from tasks;
+-------------------------------------------------------------------------------------------------------------------------------------------+
| concat('INSERT INTO dev_sys_index (name,tableid,descrition) VALUES (idx_',id,',',id,',"eindeutige Kombination aus abfid und parentid");') |
+-------------------------------------------------------------------------------------------------------------------------------------------+
| INSERT INTO dev_sys_index (name,tableid,descrition) VALUES (idx_1,1,"eindeutige Kombination aus abfid und parentid");                     |
| INSERT INTO dev_sys_index (name,tableid,descrition) VALUES (idx_2,2,"eindeutige Kombination aus abfid und parentid");                     |
| INSERT INTO dev_sys_index (name,tableid,descrition) VALUES (idx_3,3,"eindeutige Kombination aus abfid und parentid");                     |
+-------------------------------------------------------------------------------------------------------------------------------------------+
3 rows in set (0.00 sec)

P.S.:您可以在这里看到,在 name 列中,您还缺少 idx_1 周围的引号。

【讨论】:

  • no ',eindeutige Kombination aus abfid und parentid);'已经是一个字符串。当我删除 id 并在那里放置一个字符串变量时,它可以正常工作...所以我与此无关。我知道错误消息看起来是问题所在,但事实并非如此。
  • 但你是对的。我应该使用 cast(whatever as char) 而不是 cast(whatever as varchar) 那是问题所在。谢谢!
  • @kai 不,你混淆了 concat 函数中的一些逗号和单引号。正确格式化您的代码,这不太可能发生。我用我的调整尝试了你的查询,它工作得很好。无需转换或转换。
  • 请看我的编辑...对我来说,如果我转换 int,它可以在没有你的引号的情况下工作!如果我不转换它也执行,但我只得到十六进制数。
  • 是的,CONCAT() 有效,但插入语句无效!这就是我想告诉你的。
【解决方案2】:

使用 CONVERT 函数,我认为这会解决您的问题

【讨论】:

  • 我尝试了很多方法,例如 CONVERT(VARCHAR,id) 但没有任何效果。我在网上搜索了很长时间。
猜你喜欢
  • 2019-01-09
  • 2010-12-27
  • 2013-10-17
  • 2012-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-30
  • 1970-01-01
相关资源
最近更新 更多