0

我正在尝试为我创建的 2 个选择语句的 UNION 创建一个视图。

单独执行时,UNION 工作正常

但问题是当我将它作为视图执行时,只有 UNION 的第一部分被执行。

我正在使用的查询如下

   SELECT DISTINCT
  products.pid        AS id,
  products.pname      AS name,
  products.p_desc     AS description,
  products.p_uid      AS userid,
  products.p_loc      AS location,
  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

当我单独执行时,上述工作正常。但是当我将它用作视图时,它并没有给我服务部分的结果。

有人可以帮我吗?

4

1 回答 1

0

试试这个

   SELECT DISTINCT
  products.pid        AS id,
  products.pname      AS name,
  products.p_desc     AS description,
  products.p_uid      AS userid,
  products.p_loc      AS location,
  products.isaproduct AS whatisit
FROM products
UNION ALL
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

我不确定,我在我的服务器上用一张桌子试了一下,效果很好。我想它对你有用。提示这里唯一的区别是我使用“union all”而不是“union”

于 2009-02-06T04:16:57.360 回答