简体   繁体   English

COALESCE是否可以与VIEW一起正常使用?

[英]Does COALESCE work properly with VIEWs?

I have my view definition: 我有我的view定义:

CREATE VIEW `view` AS
  SELECT a.id,
    COALESCE(COALESCE(b.name, c.name), a.name) AS name
  FROM a_table a
  LEFT JOIN b_table b on a.b_id = b.id
  LEFT JOIN c_table c on a.c_id = c.id

And after I'm updating a_table row with new name it doesn't get updated in my view . 在用新name更新a_table行之后,它在我的view中不会得到更新。 But if I change name to COALESCE(a.name, COALESCE(b.name, c.name)) AS name it works. 但是,如果我将name更改为COALESCE(a.name, COALESCE(b.name, c.name)) AS name它就可以工作。

As I understand the reason is in COALESCE . 据我了解,原因在COALESCE It takes the first not null value and in my case it's b.name and gets updated only when b.name is updated. 它采用第一个not null值,在我的情况下为b.name ,仅在更新b.name更新。

Is there any option to updated the view when any of COALESCE values are changed? 更改任何COALESCE值时,是否可以选择更新view

COALESCE() works fine in views. COALESCE()在视图中工作正常。 It also takes multiple arguments, so I would suggest writing this as: 它还需要多个参数,因此我建议将其编写为:

CREATE VIEW `view` AS
  SELECT a.id, COALESCE(b.name, c.name, a.name) AS name
  FROM a_table a LEFT JOIN
       b_table b 
       ON a.b_id = b.id LEFT JOIN
       c_table c 
       ON a.c_id = c.id;

Views are not "updated". 视图不是“更新的”。 They are SQL code that is plugged into queries when the view is reference. 它们是引用视图时插入查询中的SQL代码。 The data comes from the underlying tables. 数据来自基础表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM