使用 UNION 创建 MySQL 视图
我正在尝试为以下查询创建一个视图。
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 命令的活动结果集创建
有人可以帮我吗?
I am trying to create a view for the following query.
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
But not able to do so. I am using MySql query browser. The error I am getting is:
A view can only be created from an active resultset of SELECT command
Can someone please help me with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我尝试了这个并且成功了!
感谢大家 :)
I tried this and it worked!
Thanks everyone :)
您可能想在第二个选择中更改用户 ID 和位置的顺序。 联合的所有选择中的列名称应 1 比 1 匹配。
编辑:对于查询浏览器,正如 this 指出的“要从查询创建视图,您必须已成功执行查询。更准确地说,视图是从最近成功执行的查询创建的,而不一定是从查询区域中当前的查询创建的”,
因此您需要执行在查询浏览器中创建视图之前先查询。
该错误来自查询浏览器而不是mysql。
You might want to swith the order of userid and location in the second select. The column names should match 1 to 1 in all selects of the union.
EDIT : For query browser , as this points out "To create a view from a query, you must have executed the query successfully. To be more precise, the view is created from the latest successfully executed query, not necessarily from the query currently in the Query Area"
so you need to execute the query first before you create the view in query browser.
The error is from the query browser and not mysql.
您将不同的类型合并到同一列中。 (名称可以不同,但类型必须相同,或者至少可以自动转换。)但正如 @Learning 指出的那样,看起来您已经扭曲了 SELECT 列枚举。
以防万一,正确的语法(对我有用)是
You have different types being unioned into the same column. (The names can be different, but the types have to be the same, or at least auto-castable.) But as @Learning points out, it looks like you've twisted the SELECT column enumerations.
Just in case, the proper syntax (which worked for me) is
关于 UNION 的一点评论。 UNION 仅返回结果集的不同值。 因此没有必要将 SELECT DISTINCT 与 UNION 结合使用。 不使用 DISTINCT 可能会更好地提高性能。
有关 UNION 的更多信息可以在这里找到:SQL UNION 运算符
Just a little remark about UNION. UNION only returns the distinct values of your resultset. So there is no need to use SELECT DISTINCT combined with a UNION. Probably better for performance to not use DISTINCT too.
More info on UNION can be found here: SQL UNION Operator
错误消息位于“QueryBrowser.pas”中,是 mysql-gui-tools。
它是由 a) 没有活动结果集和 b) 查询类型错误触发的。
删除“DISTINCT”有什么区别吗? 无论如何,这是 QueryBrowser 中的一个错误,而不是 MySQL 中的一个错误。 直接在 MySQL 中创建视图应该足以作为解决方法。
The error message is in "QueryBrowser.pas", part of mysql-gui-tools.
It is triggered by a) not having an active result set and b) the query having the wrong type.
Does removing the "DISTINCT" make any difference? In any case, this is a bug in QueryBrowser, rather than one MySQL. Creating the view directly in MySQL should suffice as a work-around.