MariaDB select 仅结果大于
MariaDB select only results greater than
我有一个如下所示的查询:
select uid,leadcount,salescount,leadcount/salescount 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
结果是:
我试图只显示 diff
大于 2.5
的结果(所以在这种情况下只有 uid
5 应该在输出中可见)。
我尝试使用 WHERE diff >= 2.5
,但我得到了 "You have an error in your SQL syntax"。
在MySQL和MariaDB中,你可以在查询的末尾添加一个HAVING
子句:
HAVING diff >= 2.5
这类似于 WHERE
但可以使用别名。
只需在 WHERE
子句中重复原始表达式,而不是使用别名:
SELECT uid leadcount, salescount, leadcount/salescount AS diff
FROM
(
...
) t
WHERE leadcount/salescount >= 2.5;
在 WHERE
子句中引用 diff
的问题在于它尚不可用。另外,假设您不打算聚合,请参阅戈登的另一个选项的答案。但是,使用 HAVING
只是 MySQL 或 MariaDB 上的一个选项。
我有一个如下所示的查询:
select uid,leadcount,salescount,leadcount/salescount 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
结果是:
我试图只显示 diff
大于 2.5
的结果(所以在这种情况下只有 uid
5 应该在输出中可见)。
我尝试使用 WHERE diff >= 2.5
,但我得到了 "You have an error in your SQL syntax"。
在MySQL和MariaDB中,你可以在查询的末尾添加一个HAVING
子句:
HAVING diff >= 2.5
这类似于 WHERE
但可以使用别名。
只需在 WHERE
子句中重复原始表达式,而不是使用别名:
SELECT uid leadcount, salescount, leadcount/salescount AS diff
FROM
(
...
) t
WHERE leadcount/salescount >= 2.5;
在 WHERE
子句中引用 diff
的问题在于它尚不可用。另外,假设您不打算聚合,请参阅戈登的另一个选项的答案。但是,使用 HAVING
只是 MySQL 或 MariaDB 上的一个选项。