gpt4 book ai didi

mysql - 能否将2个完全不同的查询结果分成1个结果

转载 作者:行者123 更新时间:2023-11-29 01:09:58 25 4
gpt4 key购买 nike

我试图将数字结果从 2 个完全不同的查询中分离出来。

最终结果应该是查询 1 除以查询 2

查询 1 =

SELECT COUNT(DISTINCT(table1.ID)) AS count_1
FROM table1
INNER JOIN op
INNER JOIN Org
ON table1.EID = op.id
AND Op.OrgID = Org.ID
WHERE table1.TitleID = 123
AND op.BrandID = 1
AND op.Start <= NOW() AND op.End >= NOW();

查询 2 =

SELECT COUNT(DISTINCT user.id) AS count_2
FROM table1 INNER JOIN user INNER JOIN ur
ON table1.EID = user.id AND ur.userID = user.id
WHERE
user.BrandID = 1
AND table1.TitleID = 123
AND ur.role = 0
AND user.Inactive = 0;

最佳答案

当然!您可以使用子选择来实现这一点,尽管它会非常冗长!

SELECT
(
SELECT COUNT(DISTINCT(table1.ID)) AS count_1
FROM table1
INNER JOIN op
INNER JOIN Org
ON table1.EID = op.id
AND Op.OrgID = Org.ID
WHERE table1.TitleID = 123
AND op.BrandID = 1
AND op.Start <= NOW() AND op.End >= NOW()
) / (
SELECT COUNT(DISTINCT user.id) AS count_2
FROM table1 INNER JOIN user INNER JOIN ur
ON table1.EID = user.id AND ur.userID = user.id
WHERE
user.BrandID = 1
AND table1.TitleID = 123
AND ur.role = 0
AND user.Inactive = 0
);

格式化你觉得最不难看的。

关于mysql - 能否将2个完全不同的查询结果分成1个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19103351/

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