在MySQL视图中更新值

时间:2017-12-21 23:58:15

标签: mysql database

假设我们有这些表:

事件

id | name  | time      | action | entered
---+-------+-----------+--------+--------
 1 | james | today     | x      |
 1 | james | today     | x      | yes
 1 | james | today     | y      |
 1 | james | today     | y      | yes
 2 | carl  | today     | x      | 
 2 | carl  | today     | y      |
 3 | casey | yesterday | x      |
 3 | casey | yesterday | x      | yes
 3 | casey | yesterday | y      |
 3 | casey | yesterday | y      | yes
 4 | john  | today     | x      | yes

用户

name  | email        |   
------+--------------+
james | james@email  |   
casey | casey@email  |

我是否可以创建 视图 ,从而产生以下内容?:

EventsView

id | name        | time      | action | entered
---+-------------+-----------+--------+--------
 1 | james@email | today     | x      |
 1 | james@email | today     | x      | yes
 1 | james@email | today     | y      |
 1 | james@email | today     | y      | yes
 2 | carl        | today     | x      | 
 2 | carl        | today     | y      |
 3 | casey@email | yesterday | x      |
 3 | casey@email | yesterday | x      | yes
 3 | casey@email | yesterday | y      |
 3 | casey@email | yesterday | y      | yes
 4 | john        | today     | x      | yes

也就是说,我希望看到我有电子邮件的用户名,作为电子邮件,而不是名称本身。可能的?

1 个答案:

答案 0 :(得分:1)

实现此目的的一种方法是使用coalesce以及select e.id, coalesce(u.email, e.name) name, e.time, e.action, e.entered from events e left join users u on e.name = u.name 函数,该函数将返回其参数的第一个非null值。像这样:

axis.text.y = element_text(color = ...)

Live example with a view.