AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题

问题[union](dba)

Martin Hope
Tobi
Asked: 2022-01-30 23:13:01 +0800 CST

您可以通过 1 个查询并行分页 2 个表吗

  • 2

我想通过 2 个不同的来源进行分页。我想我可以使用将 2 个查询合并为一个的方法来做到这一点,但是,我不想并行合并它们。

是否可以选择第一个查询的大小,所以我可以限制第二个查询的大小;允许我正确分页;首先通过第一个查询;如果部分/没有结果;然后通过第二个查询分页。

我显然可以在我的应用程序级别实现这一点,但我更喜欢单个查询。


想了想,或许我可以做这样的事情?

with a as (... offset :offset limit :limit), 
    a_count as (select count(*) from a)
    select * a 
    union
    select ...
    offset (select count from a_count)
    limit greatest(:limit - (select count from a_count), 0)

这有效率吗?我可以改进什么?

postgresql union
  • 1 个回答
  • 700 Views
Martin Hope
Martial
Asked: 2022-01-11 02:31:54 +0800 CST

unsigned int 上的负值与 union all

  • 0

当我对带有 unsigned int 字段的查询进行联合时,它会返回否定结果。

有人可以向我解释这种行为吗?如何在“ticketIn”中拥有真实身份?

架构(MySQL v5.7)

CREATE TABLE IF NOT EXISTS `history` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `ticket` INT(11) unsigned NOT NULL,
  `group` INT(11) unsigned NOT NULL,
  `prev_group` INT(11) unsigned NOT NULL,
  `date_mod` DATETIME NOT NULL,
  PRIMARY KEY(Id)
);

-- data
INSERT INTO history
    (`ticket`, `group`, `prev_group`, `date_mod`)
VALUES
    (2201100001, 53042, 0, '2022-01-10 00:04:05.000'),
    (2201100002, 53042, 0, '2022-01-10 00:10:01.000'),
    (2201100003, 53042, 0, '2022-01-10 00:10:02.000'),
    (2201100003, 52973, 53042, '2022-01-10 00:11:25.000'),
    (2201100004, 53043, 0, '2022-01-10 00:54:41.000'),
    (2201100004, 52972, 53043, '2022-01-10 01:08:20.000')
;

查询 #1

SET
  @s = CAST('2022-01-10' AS DATE);

查询 #2

SELECT
  DATE_FORMAT(`history_out`.date_mod, '%Y-%m-%d') AS "thedate",
  `history_out`.group,
  `history_out`.ticket AS "ticketOut",
  NULL AS "ticketIn",
  `history_out`.id
FROM
  history `history_out`
WHERE
  (`history_out`.prev_group IN (53042))
  AND (`history_out`.date_mod >= @s)
UNION
ALL
SELECT
  DATE_FORMAT(`history_in`.date_mod, '%Y-%m-%d') AS "thedate",
  `history_in`.group,
  NULL AS "ticketOut",
  `history_in`.ticket AS "ticketIn",
  `history_in`.id
FROM
  history `history_in`
WHERE
  (`history_in`.group IN (53042))
  AND (`history_in`.date_mod >= @s);
日期 团体 出票 票务 ID
2022-01-10 52973 2201100003 4
2022-01-10 53042 -2093867295 1
2022-01-10 53042 -2093867294 2
2022-01-10 53042 -2093867293 3

在 DB Fiddle 上查看

mysql union
  • 1 个回答
  • 131 Views
Martin Hope
Jay Modi
Asked: 2021-10-19 03:43:02 +0800 CST

比较 2 个表后返回特定日期的数据计数

  • 1

这是 dbfiddle 以便更好地理解,阅读问题时请参考:https ://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=b89a233c62983a23f94120eb3779a831

我有 2 个表,称为列表和日志表。列表表包含产品参考号及其当前状态。因此,假设如果它的状态是当前发布并且稍后出售,则状态更新为已售。这里是参考号。在此表中是唯一的,因为 1 个产品的状态可以更改。

现在我有另一个名为 Logs 的表,该表记录了在特定时间范围内特定产品(由 refno 引用)发生的所有状态更改。列表表中的初始条目没有记录在这里,但是一旦它的状态发生变化,该条目就会显示在这里。

假设我有以下列表表('D' => 'Draft', 'A' => 'Action', 'Y' => 'Publish', 'S' => 'Sold', 'N' => '让'):

INSERT INTO listings VALUES
 (3, 'Y','2021-05-02','2021-10-02','LP01'), (4, 'A','2021-05-01','2021-05-01','LP02'),
 (5, 'S','2020-10-01','2020-10-01','LP03'), (6, 'N','2021-05-01','2021-10-06','LP06'), 
 (10, 'D','2021-10-06','2021-10-06','LP05'), (11, 'D','2021-01-01','2021-01-01','LP04');

到目前为止,每个状态下的总计数将给出:

状态_1 C
发布 1
行动 1
卖 1
剩下 1
草稿 2

但是,如果我只想要 2020-10-01 的条目计数,它将在所有状态下显示 0,除了已售出,它会显示 1。

现在,在 2020 年 10 月 1 日到今天的这段时间里,如上所示,在列表表中输入了一些值,而且对于某些情况,状态也发生了变化。状态表:

INSERT INTO logs VALUES
 (1, 'Let','Action','2021-06-01','LP01'), (2, 'Action','Draft','2021-10-01','LP01'), 
 (3, 'Draft','Publish','2021-10-02','LP01'), (4, 'Action','Let','2021-10-06','LP06');

现在在我的列表中显示的是状态更改后的值。所以现在要获取特定日期的总计数,我让我的语句引用日志表中的日期并分别减去 status_to,并添加 status_from。对此的查询在上面提供的 dbfiddle 中。在这里,我让它返回发生在 2021 年 10 月 1 日或之前的数据,但它没有给出正确的输出。

此查询的另一个问题是我无法返回最初发生的条目的数据。例如,就像我上面提到的,2020-10-01 上的数据值应该显示 1 在已售出,而 0 在其他所有内容(期望的输出)下,但它不会这样做,因为日志表中没有记录何时最初添加了一个新条目。

所以基本上我在这里想要的是查询检查我的两个表中发生在今天和查询中指定的日期之间的事务,并减少在我在查询中指定的日期之后发生的任何事务。如果您想更简单地解释我想要实现的目标,请参考:

在此处输入图像描述

mysql union
  • 1 个回答
  • 56 Views
Martin Hope
1dymium
Asked: 2021-09-02 19:33:15 +0800 CST

这个声明具体是做什么的?

  • 0

该声明

SELECT password 
FROM users 
WHERE username='' 
UNION 
SELECT 'test' 
FROM users

返回

password
--------
test

和声明

SELECT username, password 
FROM users 
WHERE username='' 
UNION 
SELECT 'test', 'test' 
FROM users

返回

username        password
--------        --------
test            test

这里到底发生了什么?

因为如果您省略该WHERE子句,则第一个SELECT返回所有用户行并UNION SELECT添加到该行test test:

username        password
--------        --------
johnny          pw123
wiener          peter
test            test

我知道添加WHERE username=''子句不会返回任何行,因此UNION SELECT添加到任何包含test test.

但是当您只有语句SELECT 'test, 'test' FROM users时,它不会返回一个这样的行,而是为每个用户返回一行。所以多行。此语句的输出中的行数与 users 表中的用户数一样多:

username        password
--------        --------
test            test          <- one for johnny
test            test          <- one for wiener

所以我试图调和这些行为。SELECT以上通常会为每个用户返回一行,但是当它添加UNION SELECT到 first 返回的SELECT行时,它只返回一行,而不是每个用户一个。第一个SELECT不返回任何内容,除非该WHERE子句被省略,在这种情况下它返回所有用户行。

作为这个长问题的UNION SELECT附加内容test test,例如

username        password
--------        --------
johnny          pw123
wiener          peter

所以很自然,它们属于“用户名”和“密码”列。但是,当您有WHERE使第一个SELECT不返回任何内容的子句时,整个语句如何返回

password        username
--------        --------
test            test

并不是

Expr1000        Expr1000
--------        --------
test            test

是因为说SELECT返回“无”是不准确的,而是返回“在'用户名'和'密码'的列下没有行”,这意味着它为UNION SELECT这些列建立,而不管第一SELECT列下是否有任何行他们?

我是否通过苏格拉底的方法回答了我自己的问题?还是我的想法不对?如果我自己回答了,我很抱歉浪费你五分钟的时间阅读 400 字。

mysql union
  • 2 个回答
  • 39 Views
Martin Hope
Tom
Asked: 2021-03-10 12:22:23 +0800 CST

根据键忽略 UNION 中的前几行

  • 0

我有一个任务交付时间表,它基于任务部门(仅)、任务类别(仅)或产品任务和类别。这些规则可以直接应用于一个客户、一组客户或所有客户。

例子:

  • 客户 01 + 部门:设计 + 类别:创建徽标 = 24 小时
  • 客户 01 + 部门:设计 + 任何类别 = 48 小时全部
  • 客户 + 部门:设计 + 任何类别 = 72 小时全部
  • 客户 + 任何类别 + 类别:创建徽标 = 48 小时

该表的结构如下:

|rule_id|customer_id|departament_id|category_id|deadline|

创建表的 SQL:

CREATE TABLE public.test_customer (
    id INTEGER NOT NULL,
    name varchar(50) NOT NULL
);
CREATE TABLE public.test_departament (
    id INTEGER NOT NULL,
    name varchar(50) NOT NULL
);
CREATE TABLE public.test_category (
    id INTEGER NOT NULL,
    name varchar(50) NOT NULL
);
CREATE TABLE public.test_task (
    id INTEGER NOT NULL,
    customer_id INTEGER,
    departament_id INTEGER,
    category_id INTEGER,
    description varchar(50) NOT NULL
);
CREATE TABLE public.test_time_rule (
    id INTEGER NOT NULL,
    customer_id INTEGER,
    departament_id INTEGER,
    category_id INTEGER,
    time_deadline INTEGER
);

INSERT INTO public.test_customer (id, name) VALUES(1, 'Customer with Rule');
INSERT INTO public.test_customer (id, name) VALUES(2, 'Customer without Rule');

INSERT INTO public.test_departament (id, name) VALUES(1, 'Front-End');
INSERT INTO public.test_departament (id, name) VALUES(2, 'Back-End');
INSERT INTO public.test_departament (id, name) VALUES(3, 'DBA');

INSERT INTO public.test_departament (id, name) VALUES(1, 'Design');
INSERT INTO public.test_departament (id, name) VALUES(2, 'Create HTML Pages');
INSERT INTO public.test_departament (id, name) VALUES(3, 'Create Tables');
INSERT INTO public.test_departament (id, name) VALUES(4, 'Fix Bugs');

INSERT INTO public.test_task (id, customer_id, departament_id, category_id, description) VALUES(1, 1, 1, 1, 'Create New Form Design');
INSERT INTO public.test_task (id, customer_id, departament_id, category_id, description) VALUES(2, 1, 2, 4, 'Fix Bug on Customer Table');
INSERT INTO public.test_task (id, customer_id, departament_id, category_id, description) VALUES(2, 1, 3, 3, 'Create City Table');
INSERT INTO public.test_task (id, customer_id, departament_id, category_id, description) VALUES(3, 2, 1, 1, 'Create New Form Design');
INSERT INTO public.test_task (id, customer_id, departament_id, category_id, description) VALUES(4, 2, 3, 3, 'Create City Table');

INSERT INTO public.test_time_rule (id, customer_id, departament_id, category_id, time_deadline) VALUES(1, NULL, NULL, NULL, 20); --All Customers
INSERT INTO public.test_time_rule (id, customer_id, departament_id, category_id, time_deadline) VALUES(2, NULL, 1, NULL, 30); --All Customers + Departament
INSERT INTO public.test_time_rule (id, customer_id, departament_id, category_id, time_deadline) VALUES(3, NULL, NULL, 1, 40); --All Customers + Category
INSERT INTO public.test_time_rule (id, customer_id, departament_id, category_id, time_deadline) VALUES(4, NULL, 3, 3, 50); --All Customers + Departament + Category

INSERT INTO public.test_time_rule (id, customer_id, departament_id, category_id, time_deadline) VALUES(5, 1, NULL, NULL, 20); --Customers With Rule
INSERT INTO public.test_time_rule (id, customer_id, departament_id, category_id, time_deadline) VALUES(6, 1, 1, NULL, 30); --Customers + Departament
INSERT INTO public.test_time_rule (id, customer_id, departament_id, category_id, time_deadline) VALUES(7, 1, NULL, 1, 40); --Customers + Category
INSERT INTO public.test_time_rule (id, customer_id, departament_id, category_id, time_deadline) VALUES(8, 1, 3, 3, 50); --Customers + Departament + Category

我正在尝试创建一个SELECT列出所有任务并显示适用于它的规则。为此,我正在使用规则的应用程序级别,它们是:

1 - 客户 + 部门 + 类别的规则(定义的客户 ID)

2 - 客户组 + 部门 + 类别的规则(定义的客户组 ID)

3 - 一般规则(所有客户)+ 部门 + 类别(未定义客户和客户组 ID)

4 - 客户+部门规则

我已经总结了这些规则,但它们最多可达到 16 条规则,并且我为每个规则创建了一个视图,其中包含与其参数匹配的任务。

基于这些规则,我创建了SELECT几个UNION ALL(性能原因),但由于之前的选择可以重复该行,我最终添加了NOT EXISTS条件来防止这个问题。

例子:

 SELECT
    1 as level,
    ttr.id as rule_id,
    ttr.customer_id as rule_customer_id,
    tt.customer_id as task_customer_id,
    ttr.departament_id as rule_departament_id,
    tt.departament_id as task_departament_id,
    ttr.category_id as rule_category_id,
    tt.category_id as task_category_id,
    tt.id as task_id,
    tt.description as task_description,
    ttr.time_deadline
FROM
    test_time_rule ttr,
    test_task tt
WHERE
    ttr.customer_id IS NOT NULL
    AND ttr.departament_id IS NOT NULL
    AND ttr.category_id IS NOT NULL
    AND tt.customer_id = ttr.customer_id
    AND tt.departament_id = ttr.departament_id
    AND tt.category_id = ttr.category_id
UNION
SELECT
    2 as level,
    ttr.id as rule_id,
    ttr.customer_id as rule_customer_id,
    tt.customer_id as task_customer_id,
    ttr.departament_id as rule_departament_id,
    tt.departament_id as task_departament_id,
    ttr.category_id as rule_category_id,
    tt.category_id as task_category_id,
    tt.id as task_id,
    tt.description as task_description,
    ttr.time_deadline
FROM
    test_time_rule ttr,
    test_task tt
WHERE
    ttr.customer_id IS NULL
    AND ttr.departament_id IS NOT NULL
    AND ttr.category_id IS NOT NULL
    AND tt.departament_id = ttr.departament_id
    AND tt.category_id = ttr.category_id
UNION
SELECT
    3 as level,
    ttr.id as rule_id,
    ttr.customer_id as rule_customer_id,
    tt.customer_id as task_customer_id,
    ttr.departament_id as rule_departament_id,
    tt.departament_id as task_departament_id,
    ttr.category_id as rule_category_id,
    tt.category_id as task_category_id,
    tt.id as task_id,
    tt.description as task_description,
    ttr.time_deadline
FROM
    test_time_rule ttr,
    test_task tt
WHERE
    ttr.customer_id IS NOT NULL
    AND ttr.departament_id IS NOT NULL
    AND ttr.category_id IS NULL
    AND tt.customer_id = ttr.customer_id
    AND tt.departament_id = ttr.departament_id
UNION
SELECT
    4 as level,
    ttr.id as rule_id,
    ttr.customer_id as rule_customer_id,
    tt.customer_id as task_customer_id,
    ttr.departament_id as rule_departament_id,
    tt.departament_id as task_departament_id,
    ttr.category_id as rule_category_id,
    tt.category_id as task_category_id,
    tt.id as task_id,
    tt.description as task_description,
    ttr.time_deadline
FROM
    test_time_rule ttr,
    test_task tt
WHERE
    ttr.customer_id IS NULL
    AND ttr.departament_id IS NOT NULL
    AND ttr.category_id IS NULL
    AND tt.departament_id = ttr.departament_id
UNION
SELECT
    5 as level,
    ttr.id as rule_id,
    ttr.customer_id as rule_customer_id,
    tt.customer_id as task_customer_id,
    ttr.departament_id as rule_departament_id,
    tt.departament_id as task_departament_id,
    ttr.category_id as rule_category_id,
    tt.category_id as task_category_id,
    tt.id as task_id,
    tt.description as task_description,
    ttr.time_deadline
FROM
    test_time_rule ttr,
    test_task tt
WHERE
    ttr.customer_id IS NOT NULL
    AND ttr.departament_id IS NULL
    AND ttr.category_id IS NOT NULL
    AND tt.customer_id = ttr.customer_id
    AND tt.category_id = ttr.category_id
UNION
SELECT
    6 as level,
    ttr.id as rule_id,
    ttr.customer_id as rule_customer_id,
    tt.customer_id as task_customer_id,
    ttr.departament_id as rule_departament_id,
    tt.departament_id as task_departament_id,
    ttr.category_id as rule_category_id,
    tt.category_id as task_category_id,
    tt.id as task_id,
    tt.description as task_description,
    ttr.time_deadline
FROM
    test_time_rule ttr,
    test_task tt
WHERE
    ttr.customer_id IS NULL
    AND ttr.departament_id IS NULL
    AND ttr.category_id IS NOT NULL
    AND tt.category_id = ttr.category_id
UNION
SELECT
    7 as level,
    ttr.id as rule_id,
    ttr.customer_id as rule_customer_id,
    tt.customer_id as task_customer_id,
    ttr.departament_id as rule_departament_id,
    tt.departament_id as task_departament_id,
    ttr.category_id as rule_category_id,
    tt.category_id as task_category_id,
    tt.id as task_id,
    tt.description as task_description,
    ttr.time_deadline
FROM
    test_time_rule ttr,
    test_task tt
WHERE
    ttr.customer_id IS NOT NULL
    AND ttr.departament_id IS NULL
    AND ttr.category_id IS NULL
    AND tt.customer_id = ttr.customer_id
UNION
SELECT
    8 as level,
    ttr.id as rule_id,
    ttr.customer_id as rule_customer_id,
    tt.customer_id as task_customer_id,
    ttr.departament_id as rule_departament_id,
    tt.departament_id as task_departament_id,
    ttr.category_id as rule_category_id,
    tt.category_id as task_category_id,
    tt.id as task_id,
    tt.description as task_description,
    ttr.time_deadline
FROM
    test_time_rule ttr,
    test_task tt
WHERE
    ttr.customer_id IS NULL
    AND ttr.departament_id IS NULL
    AND ttr.category_id IS NULL
ORDER BY
    level;

正如您可能已经注意到,在它进入的每个级别上,都会添加所有以前的视图,这会生成一个大而慢的 SQL。

我的常识告诉我,这不是进行咨询的最佳方式。

我考虑了一些基于先前联合中已显示的任务 ID 忽略行的东西。有没有更好的方法来创建这个 SELECT?

注意:我无法添加任务中使用的规则 ID,因为此规则可以更改、删除,甚至在将来添加到与任务最匹配的规则中。

postgresql union
  • 1 个回答
  • 40 Views
Martin Hope
Nick Buxton
Asked: 2020-04-24 11:46:49 +0800 CST

如何加入时间戳并包含 NULL 条目?

  • 0

我正在尝试查询以将每日会计发票的总和与每日预算进行比较。预算存储为一个月的一个值,因此我将该金额除以总天数,因为 4 月份将有 30 个条目。

但是,即使当天没有发票,我也希望这些天的预算金额永久存在,无论是当天还是实际上当天。

我当前的查询加入了时间戳的 date_part('day',a.date) ,该时间戳仅在当天有发票时才会出现。我正在使用 PostgreSQL 版本 11.3。我已经尝试了所有加入的可能性,并且每次都得到相同的结果。除非我使用 CROSS JOIN,它确实给出了所有 30 天,但每天所有发票的总和。我曾认为 FULL OUTER JOIN 会起作用,或者以其他方式使用 UNION ALL,但到目前为止我还没有成功。

WITH RECURSIVE t(days,budget) AS (
SELECT 1 as days,b.amount/30 AS budget
FROM budgets b
WHERE b.id = 10
UNION ALL
SELECT t.days + 1,t.budget
FROM t WHERE t.days < 30)
SELECT
t.days AS "Day",
COALESCE ( SUM ( CASE WHEN a.account_id IN (17,43,44)
THEN a.credit - a.debit ELSE 0 END),0) AS "Invoices",
t.budget AS "Budget"
FROM t
LEFT JOIN accounting a
ON date_part('day',a.date) = t.days
WHERE a.date >= '2020-04-01'
AND a.date <= '2020-04-30'
AND a.account_id = 20
GROUP BY 1,3;

对于输出,假设第 1 天、第 2 天和第 3 天已经过去,而第 2 天没有条目。
当前产出
日发票预算
1 100 50
3 75 50

期望产出
日发票预算
1 100 50
2 0 50
3 75 50
4 0 50
5 0 50
...
30 0 50

我愿意接受其他查询想法来实现这一点。感谢您的帮助!

union datetime
  • 1 个回答
  • 264 Views
Martin Hope
ssp4all
Asked: 2020-02-24 17:25:30 +0800 CST

关系代数中的包联合操作

  • 0

在此处输入图像描述

我很难理解袋子联合在关系代数中是如何运作的?

这里, 1) 有一个模式 (X, Y, A) 2) (Y, A) 3) (X, A) 4) (Y, A) 所以在对 1, 2, 3 进行联合运算后输出会是什么样子, 和 4? 在我对联合操作模式的理解中,应该是相同的,但是这里 4 的 2 输出具有不同的模式。

答案:C

union relational-algebra
  • 1 个回答
  • 589 Views
Martin Hope
Phist0ne
Asked: 2020-01-14 08:44:27 +0800 CST

Microsoft SQL Server 2014 从交叉应用中的查询嵌套

  • 10

从 OUTER APPLY 语句中的嵌套查询中进行选择时,嵌套查询似乎在某些情况下只被评估一次。

向 Azure 反馈论坛报告的错误:https ://feedback.azure.com/forums/908035-sql-server/suggestions/39428632-microsoft-sql-server-2014-incorrect-result-when-s

这是预期的行为,还是我在文档中遗漏了什么,或者这是 SQL Server 中的错误?

此外,是否有可能强制评估每一行的嵌套查询?

测试用例 1

评估 VALUES 中每一行的嵌套 FROM 查询(恕我直言预期行为)

SELECT
    v,
    v2
FROM
    (VALUES (1), (2), (3), (4)) AS inner_query(v)
    OUTER APPLY (
        SELECT
            MAX(inner_v2) AS v2
        FROM (
            SELECT 
                15 AS id,
                v AS inner_v2
        ) AS outer_query
        GROUP BY id
    ) AS outer_apply

结果:

| v | v2|
|---|---|
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |

测试用例 2

它还评估 VALUES 中每一行的嵌套 FROM 查询(恕我直言预期行为)

SELECT
    v,
    v2
FROM
    (VALUES (1), (2), (3), (4)) AS inner_query(v)
    OUTER APPLY (
        SELECT
            MAX(inner_v2) AS v2
        FROM (
            SELECT 
                15 AS id,
                v AS inner_v2
            UNION ALL
            SELECT
                id AS id,
                TestCaseTemp2.v AS inner_v2
            FROM
                (VALUES (1337, 0)) AS TestCaseTemp2(id, v)
            WHERE TestCaseTemp2.v != 0
        ) AS outer_query
        GROUP BY id
    ) AS outer_apply;

结果:

| v | v2|
|---|---|
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |

测试用例 3

只计算一次嵌套的 FROM 查询

CREATE TABLE TestCaseTemp
(
    id int,
    v int
);
INSERT INTO TestCaseTemp VALUES (1337, 0);

SELECT
    v,
    v2
FROM
    (VALUES (1), (2), (3), (4)) AS inner_query(v)
    OUTER APPLY (
        SELECT
            MAX(inner_v2) AS v2
        FROM (
            SELECT 
                15 AS id,
                v AS inner_v2
            UNION ALL
            SELECT
                id AS id,
                TestCaseTemp.v AS inner_v2
            FROM
                TestCaseTemp
            WHERE TestCaseTemp.v != 0
        ) AS outer_query
        GROUP BY id
    ) AS outer_apply;

DROP TABLE TestCaseTemp;

结果:

| v | v2|
|---|---|
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 1 |
sql-server union
  • 2 个回答
  • 487 Views
Martin Hope
Hab G.
Asked: 2019-10-22 05:33:27 +0800 CST

使用 UNION 和左连接的 SQL 数据库查询

  • 1

我在同一个 SQL Server 2008 R2 数据库中有 6 个表(t1、t2、t3、...、t6。我想将它们分成两组。第 1 组(TG1)包括表 1、2 和 3。第 1 组(TG1 ) 包括表 4、5 和 6。我创建了两个查询,每个查询都使用 union

示例图像

SELECT TG1.*
  FROM (SELECT t1.PID AS ID, t1.FVALUE AS FVALUE, t1.TVALUE AS TVALUE
         FROM mydb.myuser.t1
         UNION 
         SELECT t2.PID AS ID, t2.FVALUE AS FVALUE, t2.TVALUE AS TVALUE
         FROM mydb.myuser.t2 
         UNION 
         SELECT t3.PID AS ID, t3.FVALUE AS FVALUE, t3.TVALUE AS TVALUE
         FROM mydb.myuser.t3) AS TG1 
SELECT TG2.*
  FROM (SELECT t4.PID AS PID, t4.VALUE AS VALUE, t4.VALUEID AS VALUEID, t4.VALUEDESC AS VALUEDESC
        FROM mydb.myuser.T4
        SELECT t4.PID AS PID, t4.VALUE AS VALUE, t4.VALUEID AS VALUEID, t4.VALUEDESC AS VALUEDESC
        FROM mydb.myuser.T4
        SELECT t4.PID AS PID, t4.VALUE AS VALUE, t4.VALUEID AS VALUEID, t4.VALUEDESC AS VALUEDESC
        FROM mydb.myuser.T4) AS TG2

我得到以下两个表

在此处输入图像描述

我要创建的最终查询如下面的屏幕截图所示,获取 TG1 的所有值并填充 TG2 中与 TG1 的值匹配的其他字段。

在此处输入图像描述

上面屏幕截图中的值是使用 vlookup 在 Excel 中填充的,我需要帮助才能在 SQL 查询中执行相同的操作。我感谢您的帮助。

query union
  • 1 个回答
  • 53 Views
Martin Hope
Uriel
Asked: 2018-07-17 10:58:31 +0800 CST

来自巨大 UNION ALL VIEW 的最快 MySQL 分页和排序

  • 3

假设我们有一个包含当前信息的表“月”和包含历史信息的表“年”的模式。每个表有大约 65,000 行。

这个模式存在于几个地方,比如地方 A、B 和 C。

要求从每个模式中获取有序信息。没有可能的“物化视图”

据我说,我需要统一所有结果(没有限制):

CREATE VIEW all_results AS
SELECT * FROM A.month
UNION ALL
SELECT * FROM A.year
UNION ALL
SELECT * FROM B.month
....

...然后应用 ORDER BY、COUNT 和 LIMIT

SELECT * FROM all_results ORDER BY column1 LIMIT 0,23
SELECT count(*) FROM all_results

但是这种方法需要将近 20 秒(这对于 Web 解决方案来说太多了)。

有一个更好的方法吗?

(从 dup 问题添加)此变体也需要 20 秒。

SELECT SQL_CALC_FOUND_ROWS * FROM(
SELECT * FROM A.month
UNION ALL
SELECT * FROM A.year
UNION ALL
SELECT * FROM B.month
....
) AS united
LIMIT 0,23

并计算总数:SELECT FOUND_ROWS()

mysql union
  • 1 个回答
  • 4412 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve