Django:如何使用 HAVING 子句显式进行查询?

发布于 2024-09-14 15:52:32 字数 862 浏览 4 评论 0原文

我需要执行一些如下所示的 SQL:

select approve_firm_id,approve_dt,approve_result 
from main_approve 
group by approve_firm_id 
having MAX(approve_dt) and approve_result=0;

运行 (mysql-5.1), 但如果我在 Django 模型中尝试像这样:

Approve.objects.annotate(max_dt=Max('approve_dt')).
   filter(max_dt__gt=0).filter(approve_result=0).query

生成的查询是这样的:

SELECT `main_approve`.`id`, `main_approve`.`approve_result`,
`main_approve`.`approve_dt`, `main_approve`.`approve_user_id`,
`main_approve`.`approve_firm_id`, `main_approve`.`exported_at`,
MAX(`main_approve`.`approve_dt`) AS `max_dt` FROM `main_approve` 
WHERE (`main_approve`.`approve_result` = 0 ) 
GROUP BY `main_approve`.`id` 
HAVING MAX(`main_approve`.`approve_dt`) > 0
ORDER BY NULL

我需要 WHERE 子句位于 GROUP BY 子句之后。

I need to execute some SQL that looks like this:

select approve_firm_id,approve_dt,approve_result 
from main_approve 
group by approve_firm_id 
having MAX(approve_dt) and approve_result=0;

it runs (mysql-5.1),
but if I try in the Django model like this:

Approve.objects.annotate(max_dt=Max('approve_dt')).
   filter(max_dt__gt=0).filter(approve_result=0).query

The query generated is this:

SELECT `main_approve`.`id`, `main_approve`.`approve_result`,
`main_approve`.`approve_dt`, `main_approve`.`approve_user_id`,
`main_approve`.`approve_firm_id`, `main_approve`.`exported_at`,
MAX(`main_approve`.`approve_dt`) AS `max_dt` FROM `main_approve` 
WHERE (`main_approve`.`approve_result` = 0 ) 
GROUP BY `main_approve`.`id` 
HAVING MAX(`main_approve`.`approve_dt`) > 0
ORDER BY NULL

I need the WHERE clause to be AFTER the GROUP BY clause.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

是你 2024-09-21 15:52:32

SQL 还能工作吗? having MAX(approve_dt) 部分看起来确实很可疑。也许您的意思是这样的:

SELECT DISTINCT
    main_approve.approve_firm_id,
    main_approve.approve_dt,
    main_approve.approve_result
FROM
    main_approve
    JOIN (
        SELECT 
            approve_firm_id,
            MAX(approve_dt) AS max_dt
        FROM
            main_approve
        GROUP BY 
            approve_firm_id
    ) AS t
    ON
        main_approve.approve_firm_id = t.approve_firm_id
        AND main_approve.approve_dt = t.max_dt
WHERE
    main_approve.approve_result = 0;

在您知道 SQL 到底是什么之后,构造 ORM 表达式会更容易。

Does the SQL even work? The having MAX(approve_dt) part certainly looks suspicious. Perhaps you mean this:

SELECT DISTINCT
    main_approve.approve_firm_id,
    main_approve.approve_dt,
    main_approve.approve_result
FROM
    main_approve
    JOIN (
        SELECT 
            approve_firm_id,
            MAX(approve_dt) AS max_dt
        FROM
            main_approve
        GROUP BY 
            approve_firm_id
    ) AS t
    ON
        main_approve.approve_firm_id = t.approve_firm_id
        AND main_approve.approve_dt = t.max_dt
WHERE
    main_approve.approve_result = 0;

It will be easier to construct the ORM expression after you know what exactly is the SQL going to be.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文