Article View: comp.databases.mysql
Article #4When To Use “Having” Clauses
From: Lawrence D’Oliveiro
Date: Thu, 26 Nov 2026 23:41
Date: Thu, 26 Nov 2026 23:41
16 lines
2162 bytes
2162 bytes
Trying to list block_recip entries in order in which they were last
used, including the number of times each one was used, omitting ones
which were never used:
select block_recip.email as email, from_unixtime(max(block_recip_hits.timestamp)) as last, count(*) from block_recip left join block_recip_hits on block_recip.email = block_recip_hits.email where block_recip.enabled = "1" and last is not null group by block_recip.email order by max(block_recip_hits.timestamp);
I get:
ERROR 1054 (42S22): Unknown column 'last' in 'WHERE'
So the “last” column can’t be referenced in a “where”-clause because
it doesnt’ actually exist until after the “group by” takes effect.
But this works:
select block_recip.email as email, from_unixtime(max(block_recip_hits.timestamp)) as last, count(*) from block_recip left join block_recip_hits on block_recip.email = block_recip_hits.email where block_recip.enabled = "1" group by block_recip.email having last is not null order by max(block_recip_hits.timestamp);
Technical Details
Message-ID:
<11eag2f$15jpi$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailShow Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: Lawrence D’Oliveiro <ldo@nz.invalid> Newsgroups: comp.databases.mysql Subject: When To Use “Having” Clauses Date: Thu, 26 Nov 2026 23:41:04 -0000 (UTC) Organization: A noiseless patient Spider Lines: 16 Message-ID: <11eag2f$15jpi$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Thu, 26 Nov 2026 23:41:04 +0000 (UTC) Injection-Info: dont-email.me; logging-data="1232690"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+OF2DmUTOhIDgpejFn2Sxe"; posting-host="0403721b1fbb6ccdd2d81412590093c8" User-Agent: Pan/0.165 (Kostiantynivka) Cancel-Lock: sha1:R0IOAsHpVMp8+ZTcRwj/7Qwuv7s= sha256:c2J2URUCmpXvsaBKYjypSyOnYc59UjfwPpnQep5hEoI= sha1:YtzSgmKZQ88omVt9qCzM7Nob5M4= sha256:VHnYkd0/aikDy8TG36vjiuPAbwQxF/EQmH9d9+JoKTA=