从两个表创建mysql中的VIEW

时间:2016-01-09 19:19:17

标签: mysql sql select view dovecot

我有一个包含用户邮件服务器的表。此表用于dovecot中的imap身份验证:

+-------------+------------------+-------------------+------------------+------+------+---------------------------------------+--------+---------+---------------------------+
| user_name   | domain_name      | passwd            | pwd_hash         | uid  | gid  | mailbox_basepath                      | enable | quota   | desc_rec                  |
+-------------+------------------+-------------------+------------------+------+------+---------------------------------------+--------+---------+---------------------------+
| logistic    | piduna.pp.ua     | loG-1990M         | _no_hash_passwd_ | 2000 | 2000 | /home/maildir/piduna.pp.ua/           |      1 | 2048000 | box for logistic          |
| 1c          | piduna.pp.ua     | 1c_user_1c        | _no_hash_passwd_ | 2000 | 2000 | /home/maildir/piduna.pp.ua            |      1 | 2048000 | Denisyuk V.V.             |
| admin       | piduna.pp.ua     | AAddMmM1N         | _no_hash_passwd_ | 2000 | 2000 | /home/maildir/piduna.pp.ua            |      1 | 2048000 | Admin                     |
| al.service  | piduna.pp.ua     | Alumo_Serv4321    | _no_hash_passwd_ | 2000 | 2000 | /home/maildir/piduna.pp.ua            |      1 | 2048000 | Alumo Service             |

我需要创建包含两列的表。第一栏,all @ piduna.pp.ua。第二栏是我的所有电子邮件帐户。在一个表中,我知道如何从user_name和domain_name进行连接。看:

CREATE VIEW `forwardings_all_view` AS select lcase(concat(`users`.`user_name`,'@',`users`.`domain_name`)) AS `email_fqn` from `users` where (`users`.`enable` = 1)

但是,如何添加VIEW,第二栏:all@piduna.pp.ua。

就像那样:

+------------------------------+-------------------------------------------------+
| email_fqn                    | source                                          | 
+------------------------------+-------------------------------------------------+
| .logistic@piduna.pp.ua       | all@piduna.pp.ua                                | 
| 1c@piduna.pp.ua              |                                                 | 
| admin@piduna.pp.ua           |                                                 |
| al.service@piduna.pp.ua      |                                                 | 

1 个答案:

答案 0 :(得分:1)

试试这个:

create view first_user as
select user_name from users where enable limit 1;

create view forwardings_all_view as
select lcase(concat(a.`user_name`,'@',a.`domain_name`)) AS `email_fqn`,
  if(b.user_name is null, '', 'all@piduna.pp.ua') `source`
from `users` a
left join first_user b on a.user_name = b.user_name
where a.`enable` = 1;

fiddle