我正在尝试为我创建的 2 个选择语句的 UNION 创建一个 View 。
UNION 在单独执行时工作正常
但问题是当我将它作为 View 执行时,只有 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
WHERE services.s_name
当我单独执行时,上面的工作正常。但是当我将它用作 View 时,它并没有给我服务部分的结果。
有人可以帮我解决这个问题吗?
如果您可以为上面的每个单独查询提供结果集,然后还为 UNION 查询提供结果集,我们可能会为您的问题提供更好的答案。我的直觉 react 是第二个查询可能会返回重复值,并且由于您使用的是 UNION,因此将删除重复值。如果您使用 UNION ALL,那么将返回所有重复的行。例如,如果第一个查询返回行:
1 name1 description1 10 Home Y
2 name2 description2 20 Work Y
第二行返回:
1 name1 description1 10 Home Y
结果输出将是:
1 name1 description1 10 Home Y
2 name2 description2 20 Work Y
如果您想要返回所有行:
1 name1 description1 10 Home Y
2 name2 description2 20 Work Y
1 name1 description1 10 Home Y
那么您将使用 UNION ALL 而不是 UNION 语句。
Tôi là một lập trình viên xuất sắc, rất giỏi!