使用 UNION 创建 MySQL 视图

发布于 2024-07-13 03:53:06 字数 647 浏览 7 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

最美不过初阳 2024-07-20 03:53:07
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

我尝试了这个并且成功了!
感谢大家 :)

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

I tried this and it worked!
Thanks everyone :)

骑趴 2024-07-20 03:53:07

您可能想在第二个选择中更改用户 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.

望喜 2024-07-20 03:53:07

您将不同的类型合并到同一列中。 (名称可以不同,但​​类型必须相同,或者至少可以自动转换。)但正如 @Learning 指出的那样,看起来您已经扭曲了 SELECT 列枚举。

以防万一,正确的语法(对我有用)是

CREATE VIEW myView 
AS  
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

CREATE VIEW myView 
AS  
SELECT ... 
隱形的亼 2024-07-20 03:53:07

关于 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

我们的影子 2024-07-20 03:53:07

错误消息位于“QueryBrowser.pas”中,是 mysql-gui-tools

procedure TQueryBrowserForm.SQLCreateViewClick(Sender: TObject);
// ... 
begin
  if Assigned(ActiveResultset) and (ActiveResultset.ResultSet.query.query_type = MYX_QT_SELECT)then
    // ... 
  else
    ShowError('Creation error', _('A view can only be created from a active resultset of SELECT command.'), []);
end;

它是由 a) 没有活动结果集和 b) 查询类型错误触发的。

删除“DISTINCT”有什么区别吗? 无论如何,这是 QueryBrowser 中的一个错误,而不是 MySQL 中的一个错误。 直接在 MySQL 中创建视图应该足以作为解决方法。

The error message is in "QueryBrowser.pas", part of mysql-gui-tools.

procedure TQueryBrowserForm.SQLCreateViewClick(Sender: TObject);
// ... 
begin
  if Assigned(ActiveResultset) and (ActiveResultset.ResultSet.query.query_type = MYX_QT_SELECT)then
    // ... 
  else
    ShowError('Creation error', _('A view can only be created from a active resultset of SELECT command.'), []);
end;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文