{"id":64,"date":"2008-12-08T04:53:17","date_gmt":"2008-12-08T10:53:17","guid":{"rendered":"http:\/\/www.imed.ws\/blog\/2008\/12\/08\/sql-using-count-within-the-where-clause\/"},"modified":"2008-12-08T04:58:12","modified_gmt":"2008-12-08T10:58:12","slug":"sql-using-count-within-the-where-clause","status":"publish","type":"post","link":"https:\/\/www.imed.ws\/blog\/2008\/12\/08\/sql-using-count-within-the-where-clause\/","title":{"rendered":"SQL : Using Count within the Where clause"},"content":{"rendered":"<p>As trying to make a condition inside an SQL where clause based on the COUNT function or other functions, the trivial one is to use :<\/p>\n<p><code>SELECT count(items) as cnt from user_items where cnt&gt;3<\/code><\/p>\n<p>This is not going to work, and therefore you need to use the following one instead :<\/p>\n<p><code>SELECT count(items) as cnt from user_items <strong>having<\/strong> count(items)&gt;3<\/code><\/p>\n<p>In case you want to add a where clause to the SQL statement, here is how it should look :<\/p>\n<p><code>SELECT count(items) as cnt from user_items <strong>where id>1000<\/strong> <strong>having<\/strong> count(items)&gt;3<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As trying to make a condition inside an SQL where clause based on the COUNT function or other functions, the trivial one is to use : SELECT count(items) as cnt from user_items where cnt&gt;3 This is not going to work, and therefore you need to use the following one instead : SELECT count(items) as cnt [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/www.imed.ws\/blog\/wp-json\/wp\/v2\/posts\/64"}],"collection":[{"href":"https:\/\/www.imed.ws\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.imed.ws\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.imed.ws\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.imed.ws\/blog\/wp-json\/wp\/v2\/comments?post=64"}],"version-history":[{"count":0,"href":"https:\/\/www.imed.ws\/blog\/wp-json\/wp\/v2\/posts\/64\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.imed.ws\/blog\/wp-json\/wp\/v2\/media?parent=64"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.imed.ws\/blog\/wp-json\/wp\/v2\/categories?post=64"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.imed.ws\/blog\/wp-json\/wp\/v2\/tags?post=64"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}