3

我有一个包含复杂连接的查询。如果结果不为空,则类似于连接多个子选择的结果。它在我们运行 MySQL 5.1 的开发服务器上运行良好(尽管复杂性让我有点恶心),但在我们运行 5.0 报告的生产服务器上运行良好

FUNCTION database.concat does not exist

任何想法为什么会这样?不幸的是,此时升级不是一种选择,所以除非有人有一个好主意,否则我几乎无法重写这份报告。

查询如下(是的,我意识到我在严重滥用 mysql。让我们暂时搁置它。如果有人知道如何通过别名引用子选择会很方便,但我从来没有能够做到它,因此是重复的子选择。NULL如果任何参数是,则concat 也会返回NULL,因此是 if 语句。)

select (concat(if((select 
                       concat(if(b.foo is not null, b.foo, ""), 
                       " ", 
                       if(f.bar is not null, f.bar, ""))
                     from  `foo_table`  as f
                      left join `bar_table` as b
                        on b.SOME_ID = f.SOME_ID
                     where f.STUDENT_ID = t.STUDENT_ID
                       and bar.NewID = t.OldID order by bar.id limit 1) is not null,
                   (select 
                      concat(if(b.foo is not null, b.foo, ""), 
                       " ", 
                       if(f.bar is not null, f.bar, ""))
                     from  `foo_table`  as f
                      left join `bar_table` as b
                        on b.SOME_ID = f.SOME_ID
                     where f.STUDENT_ID = t.STUDENT_ID
                       and bar.NewID = t.OldID order by bar.id limit 1),
                   ""),
       " ",
       t.reason) as Reason
   from table as t
4

3 回答 3

18

并确保concat和之间没有空格(。像这样的东西:

SELECT CONCAT ('a', 'b');
于 2010-11-10T19:23:44.910 回答
5

CONCAT可在5.0.

确保您没有打错字:

SELECT  CONTACT(id, name)
FROM    items;

Error Code: 1305
FUNCTION test.CONTACT does not exist
于 2010-11-10T19:19:53.053 回答
2

我重写了查询,使用concat_ws它简单地忽略NULL值而不是像返回NULL一样返回concat。这是大大简化且实际有效的结果。

select concat_ws(" ", 
                 (select 
                    concat_ws(" ", b.bar, f.foo)
                   from  `foo_table`  as f
                   left join `bar_table` as  b
                     on b.SOME_ID = f.SOME_ID
                  where f.STUDENT_ID = t.STUDENT_ID
                    and f.NewID = t.OldID order by f.id limit 1 ),
               t.reason) as Reason,
   from table as t
于 2010-11-10T19:54:15.170 回答