当前位置:  开发笔记 > 后端 > 正文

使用UNION创建MySQL视图

如何解决《使用UNION创建MySQL视图》经验,为你挑选了1个好方法。

我正在尝试为以下查询创建一个视图.

SELECT DISTINCT
  products.pid        AS id,
  products.pname      AS name,
  products.p_desc     AS description,
  products.p_loc      AS location,
  products.p_uid      AS userid,
  products.isaproduct AS whatisit
FROM products
UNION
SELECT DISTINCT
  services.s_id       AS id,
  services.s_name     AS name,
  services.s_desc     AS description,
  services.s_uid      AS userid,
  services.s_location AS location,
  services.isaservice AS whatisit
FROM services

但是不能这样做.我正在使用MySql查询浏览器.我得到的错误是:

只能从SELECT命令的活动结果集创建视图

有人可以帮我这个吗?



1> 小智..:
CREATE VIEW vw_product_services AS
SELECT DISTINCT products.pid AS id,
                products.pname AS name,
                products.p_desc AS description,
                products.p_loc AS location,
                products.p_uid AS userid,
                products.isaproduct AS whatisit
           FROM products
          UNION
          SELECT DISTINCT services.s_id AS id,
                services.s_name AS name,
                services.s_desc AS description,
                services.s_uid AS userid,
                services.s_location AS location,
                services.isaservice AS whatisit
           FROM services

我试过这个,它有效!感谢大家 :)

推荐阅读
爱唱歌的郭少文_
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有