gpt4 book ai didi

mysql - 在 SQL 语句中组合 UNION ALL 和 OUTER JOIN

转载 作者:行者123 更新时间:2023-11-29 22:19:09 24 4
gpt4 key购买 nike

我有两个查询,一个使用 union all 函数。

select * from (

Select

ticket_id as Ticket_Num
, name as Name
, approved_at as Approval_Date
, price as Price
, quantity as Quantity
, (price * quantity) as Cumulative_Price

From purchase_list_items

union all

select
''
, ''
, datetime('now')
, ''
, 'Total'
, sum(in1.Cumulative_Price)


from (

select
ticket_id as Ticket_Num
, name as Name
, approved_at as Approval_Date
, price as Price
, quantity as Quantity
, (price * quantity) as Cumulative_Price

From purchase_list_items
)
in1

)
fullInner1



order by Approval_Date

另一个只是简单的左/右外连接。

select 
uw.first_name as Purchased_By
from tickets t

left outer join ticket_work w on t.id = w.ticket_id
left outer join users uw on w.user_id = uw.id

where time_spent is not null

group by uw.id, uw.first_name, t.c_location

我希望能够将第二个查询合并到第一个查询中,以创建列“Purchased_By”。然而,由于 UNION ALL,我正在努力实现它。

本质上,每张票证都有一个与之关联的 user_id。每个 user_id 都附有一个名字。我将两个部分分开,工作正常。

例如,user_id 是 Bob。这在第二个查询中效果很好。我现在只想显示鲍勃写了票#12345

编辑:这是创建表语句

CREATE TABLE "purchase_list_items" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(200) NOT NULL, "user_id" integer NOT NULL, "purchased" boolean DEFAULT 'f', "ticket_id" integer, "created_at" datetime, "received" boolean DEFAULT 'f', "received_at" datetime, "purchased_at" datetime, "price" float DEFAULT 0.0, "approved" boolean DEFAULT 'f', "approved_at" datetime, "charge_to" varchar(255) DEFAULT '', "agreement_id" integer, "part_number" varchar(255), "purchase_order" varchar(255), "notes" text, "quantity" integer DEFAULT 1, "shipping_code" varchar(255), "updated_at" datetime, "purchased_for_id" integer, "purchased_for_type" varchar(255), "category" varchar(255) DEFAULT 'Miscellaneous', "subcategory" varchar(255) DEFAULT 'Uncategorized', "order_number" varchar(255), "quote_id" integer DEFAULT 0, "upc" varchar(255) DEFAULT '', "gid" varchar(255) DEFAULT '', "research_class" varchar(255) DEFAULT '', "research_code" varchar(255) DEFAULT '', "product_image" varchar(255) DEFAULT '', "purchase_link" varchar(255) DEFAULT '', "vendor_id" integer);

CREATE INDEX "index_purchase_list_items_on_user_id" ON "purchase_list_items" ("user_id");

CREATE INDEX "index_purchase_list_items_on_ticket_id" ON "purchase_list_items" ("ticket_id");

CREATE INDEX "index_purchase_list_items_on_agreement_id" ON "purchase_list_items" ("agreement_id");

CREATE INDEX "index_purchase_list_items_on_purchased_for" ON "purchase_list_items" ("purchased_for_id", "purchased_for_type");

第二个表:

CREATE TABLE "tickets" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "summary" varchar(50) NOT NULL, "status" varchar(255) NOT NULL, "description" text, "priority" integer, "created_at" datetime, "updated_at" datetime, "closed_at" datetime, "created_by" integer, "assigned_to" integer, "viewed_at" datetime, "reopened" boolean, "requires_purchase" boolean, "category" varchar(255), "external_id" varchar(255), "email_message_id" varchar(255), "status_updated_at" datetime, "warning_alert_count" integer DEFAULT 0, "error_alert_count" integer DEFAULT 0, "muted" boolean, "site_id" integer, "master_ticket_id" integer, "c_location" varchar(255), "c_jobscope_case_id" text, "reported_by_id" integer, "due_at" datetime, "c_status" varchar(255) DEFAULT 'Active', "c_jobscope_database" varchar(255) DEFAULT 'ALL', "remote_id" integer, "synced_at" datetime, "sharer_id" integer, "parent_id" integer, "c_original_est_due_date" date, "c_jobscope_priority" varchar(255), "c_priority" integer, "c_sciforma_ticket_number" integer DEFAULT NULL);

CREATE INDEX "index_tickets_on_remote_id" ON "tickets" ("remote_id");

CREATE INDEX "index_tickets_on_synced_at" ON "tickets" ("synced_at");

CREATE INDEX "index_tickets_on_sharer_id" ON "tickets" ("sharer_id");

CREATE INDEX "index_tickets_on_parent_id" ON "tickets" ("parent_id");

CREATE INDEX "index_tickets_on_assigned_to" ON "tickets" ("assigned_to");

CREATE INDEX "index_tickets_on_status" ON "tickets" ("status");

CREATE INDEX "index_tickets_on_due_at" ON "tickets" ("due_at");

最佳答案

purchase_list_items 表已包含user_id。查找相应名称的查询如下所示:

SELECT first_name
FROM users
WHERE user_id = ?

这可以直接集成到更大的查询中,如 correlated subquery :

SELECT ...,
(price * quantity) as Cumulative_Price,
(SELECT first_name
FROM users
WHERE user_id = purchase_list_items.user_id
) AS Purchased_By
FROM purchase_list_items
...

关于mysql - 在 SQL 语句中组合 UNION ALL 和 OUTER JOIN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30921543/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com