MariaDB 比较两个表中的数字计数
MariaDB comparing number count from two tables
我正在尝试将 table 中一个值的总 COUNT
与另一个值的总 COUNT
进行比较,我只是不确定如何正确地进行比较.
如果 total_sales
table 中的 SalesID
总数 COUNT
高于 total_leads
中的 fid
总数 COUNT
table 然后我想输出这些值。
表格:
CREATE TABLE total_leads
(`uid` int, `tstamp` int, `fid` int, `phone` varchar(2))
;
INSERT INTO total_leads
(`uid`, `tstamp`, `fid`, `phone`)
VALUES
(1, 1299795364, 1525, '""'),
(1, 1309854563, 4031, '""'),
(1, 1309854562, 3134, '""'),
(5, 1280752536, 18, '""'),
(5, 1280942244, 219, '""'),
(5, 1309862562, 1234, '""'),
(5, 1480752536, 184, '""'),
(5, 1520942244, 119, '""')
;
CREATE TABLE total_sales
(`UserID` int, `SalesID` int, `Time` varchar(8))
;
INSERT INTO total_sales
(`UserID`, `SalesID`, `Time`)
VALUES
(1, 172, '12:57:43'),
(1, 3563, '15:59:49'),
(1, 9508, '01:46:47'),
(5, 18935, '07:26:07'),
(5, 19378, '08:06:41'),
(1, 144, '11:52:41'),
(1, 9248, '02:43:40'),
(1, 3423, '14:54:45'),
(5, 11935, '03:21:06'),
(1, 1448, '05:02:24')
;
MySQL 命令:
SELECT x.uid, COUNT(*), COUNT(DISTINCT x.fid)
FROM total_leads AS x
WHERE x.uid BETWEEN 1 AND 5
GROUP BY x.uid;
SELECT ud.UserId, COUNT(*), COUNT(DISTINCT ud.SalesID)
FROM total_sales AS ud
WHERE ud.UserId BETWEEN 1 AND 5
GROUP BY ud.UserID;
它returns (total_leads, total_sales):
uid COUNT(*) COUNT(DISTINCT x.fid)
1 3 3
5 5 5
UserId COUNT(*) COUNT(DISTINCT ud.SalesID)
1 7 7
5 3 3
所以我希望得到如下结果:
UserId Sales Leads Dif
1 7 3 4
即使只知道哪个 UserID
有 Sales
> fid
也完全没问题:
UserId COUNT(*) COUNT(DISTINCT ud.SalesID)
1 7 7
我尝试了几种方法,例如 UNION
,但只是将所有值合并在一起。
使用子查询连接
select uid,leadcount,salescount,salescount-leadcount as diff from
(
SELECT x.uid, COUNT(*) leadcount, COUNT(DISTINCT x.fid)
FROM total_leads AS x
WHERE x.uid BETWEEN 1 AND 5
GROUP BY x.uid
) t1 left join
(
SELECT ud.UserId, COUNT(*) salescount, COUNT(DISTINCT ud.SalesID)
FROM total_sales AS ud
WHERE ud.UserId BETWEEN 1 AND 5
GROUP BY ud.UserID
) t2 on t1.uid=t2.UserId
试试这个代码,
SELECT x.uid,COUNT(DISTINCT x.fid) AS leads,COUNT(DISTINCT ud.SalesID) AS
sales,((COUNT(DISTINCT ud.SalesID))-(COUNT(DISTINCT x.fid)))
FROM
total_leads AS X,total_sales AS ud WHERE x.uid=ud.UserId AND ud.UserId BETWEEN 1 AND 5
GROUP BY ud.UserID;
您可以使用以下方法来完成此操作,您可以在其中比较每个 table
的总计数
select t1.uid,t2.sales_count,t1.leads_count,
case when t1.leads_count > t2.sales_count then t1.leads_count -t2.sales_count
else t2.sales_count -t1.leads_count
end as diff
from
(
select uid,count(fid) as leads_count
from total_leads
group by uid
)t1
inner join
(
select UserID,count(SalesID)as sales_count
from total_sales
group by UserID
)t2 on t1.uid = t2.userid ;
我正在尝试将 table 中一个值的总 COUNT
与另一个值的总 COUNT
进行比较,我只是不确定如何正确地进行比较.
如果 total_sales
table 中的 SalesID
总数 COUNT
高于 total_leads
中的 fid
总数 COUNT
table 然后我想输出这些值。
表格:
CREATE TABLE total_leads
(`uid` int, `tstamp` int, `fid` int, `phone` varchar(2))
;
INSERT INTO total_leads
(`uid`, `tstamp`, `fid`, `phone`)
VALUES
(1, 1299795364, 1525, '""'),
(1, 1309854563, 4031, '""'),
(1, 1309854562, 3134, '""'),
(5, 1280752536, 18, '""'),
(5, 1280942244, 219, '""'),
(5, 1309862562, 1234, '""'),
(5, 1480752536, 184, '""'),
(5, 1520942244, 119, '""')
;
CREATE TABLE total_sales
(`UserID` int, `SalesID` int, `Time` varchar(8))
;
INSERT INTO total_sales
(`UserID`, `SalesID`, `Time`)
VALUES
(1, 172, '12:57:43'),
(1, 3563, '15:59:49'),
(1, 9508, '01:46:47'),
(5, 18935, '07:26:07'),
(5, 19378, '08:06:41'),
(1, 144, '11:52:41'),
(1, 9248, '02:43:40'),
(1, 3423, '14:54:45'),
(5, 11935, '03:21:06'),
(1, 1448, '05:02:24')
;
MySQL 命令:
SELECT x.uid, COUNT(*), COUNT(DISTINCT x.fid)
FROM total_leads AS x
WHERE x.uid BETWEEN 1 AND 5
GROUP BY x.uid;
SELECT ud.UserId, COUNT(*), COUNT(DISTINCT ud.SalesID)
FROM total_sales AS ud
WHERE ud.UserId BETWEEN 1 AND 5
GROUP BY ud.UserID;
它returns (total_leads, total_sales):
uid COUNT(*) COUNT(DISTINCT x.fid)
1 3 3
5 5 5
UserId COUNT(*) COUNT(DISTINCT ud.SalesID)
1 7 7
5 3 3
所以我希望得到如下结果:
UserId Sales Leads Dif
1 7 3 4
即使只知道哪个 UserID
有 Sales
> fid
也完全没问题:
UserId COUNT(*) COUNT(DISTINCT ud.SalesID)
1 7 7
我尝试了几种方法,例如 UNION
,但只是将所有值合并在一起。
使用子查询连接
select uid,leadcount,salescount,salescount-leadcount as diff from
(
SELECT x.uid, COUNT(*) leadcount, COUNT(DISTINCT x.fid)
FROM total_leads AS x
WHERE x.uid BETWEEN 1 AND 5
GROUP BY x.uid
) t1 left join
(
SELECT ud.UserId, COUNT(*) salescount, COUNT(DISTINCT ud.SalesID)
FROM total_sales AS ud
WHERE ud.UserId BETWEEN 1 AND 5
GROUP BY ud.UserID
) t2 on t1.uid=t2.UserId
试试这个代码,
SELECT x.uid,COUNT(DISTINCT x.fid) AS leads,COUNT(DISTINCT ud.SalesID) AS
sales,((COUNT(DISTINCT ud.SalesID))-(COUNT(DISTINCT x.fid)))
FROM
total_leads AS X,total_sales AS ud WHERE x.uid=ud.UserId AND ud.UserId BETWEEN 1 AND 5
GROUP BY ud.UserID;
您可以使用以下方法来完成此操作,您可以在其中比较每个 table
的总计数select t1.uid,t2.sales_count,t1.leads_count,
case when t1.leads_count > t2.sales_count then t1.leads_count -t2.sales_count
else t2.sales_count -t1.leads_count
end as diff
from
(
select uid,count(fid) as leads_count
from total_leads
group by uid
)t1
inner join
(
select UserID,count(SalesID)as sales_count
from total_sales
group by UserID
)t2 on t1.uid = t2.userid ;