2
select g.id, ifnull(s.count, 0)
from t_goods g
left join v_stock s on g.id = s.id

This sql works well, and then I create a view:

create or drop view v_goods_stock as
select g.id, ifnull(s.count, 0)
from t_goods g
left join v_stock s on g.id = s.id

I get the error:

[Err] 1064 - You have an error in your SQL syntax

My mysql version is 5.5.

I try to remove the ifnull function like this

create or drop view v_goods_stock as select g.id id, s.count c from t_goods g left join v_stock s on g.id = s.id

I still get the same error.

4

2 回答 2

2

您必须为计算值指定列名:

create or replace view v_goods_stock as
select g.id, ifnull(s.count, 0) as count
from t_goods g  
left join v_stock s on g.id = s.id

默认列名是表达式本身ifnull(s.count, 0),它不是一个有效的名称(包含括号、逗号等)

于 2013-04-21T02:25:58.370 回答
0

您的问题很简单,与以下内容无关IFNULL():MySQL 无法识别语法CREATE OR DROP VIEW。(这里是 的语法参考CREATE VIEW

如果您需要此语句在视图已经存在时工作,请删除单词OR DROP,或者将其替换为。OR REPLACE

于 2013-04-21T03:22:26.213 回答