gpt4 book ai didi

sql - 联合查询在一列上不同

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

我希望第二个查询的结果覆盖第一个查询的结果:

SELECT "panel_restaurants_restaurant"."id",
"panel_restaurants_restaurant"."name",
"panel_restaurants_restaurant"."logo",
"panel_restaurants_restaurantfeatures"."currency" AS "currency",
ST_DistanceSphere(location, ST_GeomFromText('POINT(0.0 0.0)',4326)) AS "distance",
"panel_meals_meal"."id" AS "meal_id",
"panel_meals_meal"."status" AS "meal_status",
"panel_meals_meal"."available_count" AS "available_dishes",
"panel_meals_meal"."discount_price" AS "discount_price",
"panel_meals_meal"."normal_price" AS "normal_price",
"panel_meals_meal"."collection_from" AS "pickup_from",
"panel_meals_meal"."collection_to" AS "pickup_to",
"panel_meals_meal"."description" AS "meal_description"
FROM "panel_restaurants_restaurant"
INNER JOIN "panel_restaurants_restaurantfeatures" ON (
"panel_restaurants_restaurantfeatures"."restaurant_id" = "panel_restaurants_restaurant"."id")
LEFT OUTER JOIN "panel_meals_meal" ON ("panel_restaurants_restaurant"."id" = "panel_meals_meal"."restaurant_id"
AND "panel_meals_meal"."status" = 0
AND (
("panel_meals_meal"."collection_from" AT TIME ZONE 'Europe/Warsaw')::date = DATE 'today' OR
("panel_meals_meal"."collection_from" AT TIME ZONE 'Europe/Warsaw')::date = DATE 'tomorrow'
)
AND "panel_meals_meal"."collection_to" > '2017-07-29 19:33:47.992075+00:00'
AND "panel_meals_meal"."available_count" > 0)
WHERE "panel_restaurants_restaurant"."status" = 2
UNION
SELECT "panel_restaurants_restaurant"."id",
"panel_restaurants_restaurant"."name",
"panel_restaurants_restaurant"."logo",
"panel_restaurants_restaurantfeatures"."currency" AS "currency",
ST_DistanceSphere(location, ST_GeomFromText('POINT(0.0 0.0)',4326)) AS "distance",
"panel_meals_meal"."id" AS "meal_id",
"panel_meals_meal"."status" AS "meal_status",
"panel_meals_meal"."initial_count" AS "available_dishes",
"panel_meals_meal"."discount_price" AS "discount_price",
"panel_meals_meal"."normal_price" AS "normal_price",
"panel_meals_meal"."collection_from" AS "pickup_from",
"panel_meals_meal"."collection_to" AS "pickup_to",
"panel_meals_meal"."description" AS "meal_description"
FROM "panel_restaurants_restaurant"
INNER JOIN "panel_restaurants_restaurantfeatures" ON (
"panel_restaurants_restaurantfeatures"."restaurant_id" = "panel_restaurants_restaurant"."id")
LEFT OUTER JOIN "panel_meals_meal" ON (
"panel_restaurants_restaurant"."id" = "panel_meals_meal"."restaurant_id" AND
"panel_meals_meal"."status" = 0)
INNER JOIN "panel_meals_mealrepeater" ON (
"panel_meals_mealrepeater"."meal_id" = "panel_meals_meal"."id")
WHERE "panel_restaurants_restaurant"."status" = 2 AND "panel_meals_mealrepeater"."saturday" = true
ORDER BY distance ASC

例如 - 第一个查询可能会为来自 panel_meals_meal 表的内容返回空值,但第二个查询会返回一些内容 - 在那种情况下,我将为 id< 提供相同的值namelogocurrencydistance 和不同的值(第一个查询返回的空值,和来自另一个的 something)对于所有其他的。

所以问题是 - 我如何使这个 UNION 在一定范围的列上不同(实际上只有一个就足够了 - id)?

最佳答案

您可以通过 FULL OUTER JOIN 而不是 UNION 来做您想做的事,并使用 COALESCE 来发挥您的优势。

我简化了您的场景以专注于 FULL OUTER JOIN 部分:

这是表格(将它们视为您第一个 SELECT 之前的结果,在UNION 之前,和第二个 SELECTUNION 之后:

CREATE TABLE table_a
(
id INTEGER NOT NULL PRIMARY KEY,
name TEXT,
logo TEXT
) ;
CREATE TABLE table_b
(
id INTEGER NOT NULL PRIMARY KEY,
name TEXT,
logo TEXT
) ;

这些是我们在其中的数据:

INSERT INTO 
table_a
(id, name, logo)
VALUES
(1, 'Name1-A', 'Logo1-A'),
(2, NULL, NULL),
(3, 'Name3-A', NULL),
(4, NULL, 'Logo4-A'),
(5, 'Name5-only-in-A', NULL);

INSERT INTO
table_b
(id, name, logo)
VALUES
(1, 'Name1-B', 'Logo1-B'),
(2, 'Name2-B', NULL),
(3, 'Name3-B', 'Logo3-B'),
(4, 'Name4-B', 'Logo4-B'),
(6, 'Name6-only-in-B', 'Logo6-B');

您要查找的查询是通过加入 完成的,您可以从table_atable_b 中检索所有行.然后,您使用:

SELECT
id,
COALESCE(a.name, b.name) AS name,
COALESCE(a.logo, b.logo) AS logo
FROM
table_a AS a
FULL OUTER JOIN table_b AS b USING(id)
ORDER BY
id ;
id | name            | logo   -: | :-------------- | :------ 1 | Name1-A         | Logo1-A 2 | Name2-B         | null    3 | Name3-A         | Logo3-B 4 | Name4-B         | Logo4-A 5 | Name5-only-in-A | null    6 | Name6-only-in-B | Logo6-B

dbfiddle here


在您的情况下,将 table_a AS a 替换为完整的第一个 (SELECT ...) AS a,对于 b 也是如此.我假设 id 是您的主键。

引用资料:

关于sql - 联合查询在一列上不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45393416/

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