#1479 closed enhancement (fixed)
Add more filters to the query string
| Reported by: | christined | Owned by: | skippy |
|---|---|---|---|
| Priority: | normal | Milestone: | |
| Component: | General | Version: | 1.5.2 |
| Severity: | normal | Keywords: | posts filter bg|has-patch bg|dev-feedback |
| Cc: | Focuses: |
Description
I'm wanting to write a posts query for a plugin which needs to use a "having" clause. However, it's not something I seem to be able to wrangle at the moment. The having clause needs to go between the group by and order by clauses; but there doesn't seem to be a way for me to put anything between the two. Putting the having clause either before the group by, or after the order by gives me a SQL error.
The sql that is output at the moment, seems to look like
SELECT * FROM posts {posts_join} WHERE {posts_where} GROUP BY posts.ID ORDER BY (posts_order)
What I am wanting to be able to do, is something along the lines of
SELECT * FROM posts {posts_join} WHERE {posts_where} GROUP BY posts.ID (posts_having) ORDER BY (posts_order)
or
SELECT * FROM posts {posts_join} WHERE {posts_where} GROUP BY posts.ID (posts_group_by) ORDER BY (posts_order)
Cheers,
Christine
Attachments (1)
Change History (8)
#1
@
21 years ago
Hi Christine,
Try the patch attached for a possible solution. Untested so it may have php errors.
westi
#2
@
21 years ago
- Priority lowest → normal
- Summary Add a filter for posts_having or posts_group_by → Add more filters to the query string
as a complement to Christine's request, it is mostly useless to group by in an sql statement with a distinct clause, since distinct means group by everything. so what we really want here is two plugin hooks, one for the group by clause, and another one for the select clause, in order to define aggregates.
e.g.
select *,
case
when this
then do_this
when that
then do_that
else do_else
as my_aggr
...
group by ID, my_aggr
...
Also, the distinct clause is useless, since we're grouping by ID.
And lastly, there is a strong case to name the tables used so as to allow a double join on the posts table, for instance.
In all, it might make sense to to remove all the query filters and to replace them with a the_request hook. that way, plugin devs will only need to work out one query hook, and there will no longer be any incentive to throw a 1=0 where clause to later catch the_posts and do the query 'the right way'.
#3
@
21 years ago
- Keywords bg|has-patch bg|dev-feedback added
- Owner changed from to
- Status new → assigned
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
diff to add a filter for posts_grouped_by