# Need help with an SQL badge query

**URL:** https://meta.discourse.org/t/need-help-with-an-sql-badge-query/72021
**Category:** Data & reporting
**Tags:** sql-triggered-badge
**Created:** [October 13, 2017, 4:08pm UTC](https://meta.discourse.org/t/need-help-with-an-sql-badge-query/72021 "2017-10-13T16:08:39Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Bernstein](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bernstein/32/304070_2.png) [@Bernstein](https://meta.discourse.org/u/Bernstein)
#### Post date: [October 13, 2017, 4:08pm UTC](https://meta.discourse.org/t/need-help-with-an-sql-badge-query/72021/1 "2017-10-13T16:08:39Z")

</div>

I’m not well-versed in these queries but I have been using this (and others with different post requirements) query I got from the Discourse meta site to reward posting to a particular category.

```
SELECT P.user_id, MIN(P.created_at) granted_at, MAX(P.id) post_id 
FROM badge_posts P JOIN topics T ON T.id = P.topic_id 
WHERE T.category_id IN (SELECT id FROM categories WHERE id = 56 OR parent_category_id = 6) 
AND P.user_id >= 0 GROUP BY P.user_id HAVING COUNT(*) >= 1

```

I was wondering how exactly I could change this to:

A) Require new TOPICS, not just posts in the category and  
B) How I could include subcategories. So not just category 6, but also subcategories 7,8, and 9.

Thanks so much.

---

<div class="post-metadata">

### Author: ![Bernstein](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bernstein/32/304070_2.png) [@Bernstein](https://meta.discourse.org/u/Bernstein)
#### Post date: [October 23, 2017, 3:18am UTC](https://meta.discourse.org/t/need-help-with-an-sql-badge-query/72021/2 "2017-10-23T03:18:19Z")

</div>

I finally realized why this code wasn’t working. I can’t award badges to categories that are limited to certain trust levels. I have a category that is only accessible/visible to trust level 2+

Badges won’t work for that group. Is that a bug or feature? Is there any way around it?

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [October 23, 2017, 3:19am UTC](https://meta.discourse.org/t/need-help-with-an-sql-badge-query/72021/3 "2017-10-23T03:19:30Z")

</div>

It is a feature, ask @sam about it.

---

<div class="post-metadata">

### Author: ![Bernstein](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bernstein/32/304070_2.png) [@Bernstein](https://meta.discourse.org/u/Bernstein)
#### Post date: [October 23, 2017, 3:41am UTC](https://meta.discourse.org/t/need-help-with-an-sql-badge-query/72021/4 "2017-10-23T03:41:44Z")

</div>

@sam, is there any way around this? The category is a corner of my forum designed to be earned by “leveling” up your trust level and contributing. I don’t mind if people who can’t access can see the badges earned from it. It’s meant to be something to work toward.

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [November 29, 2019, 9:36pm UTC](https://meta.discourse.org/t/need-help-with-an-sql-badge-query/72021/5 "2019-11-29T21:36:25Z")

</div>



---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [November 29, 2019, 9:43pm UTC](https://meta.discourse.org/t/need-help-with-an-sql-badge-query/72021/6 "2019-11-29T21:43:47Z")

</div>

> [@Bernstein](#):
>
> I finally realized why this code wasn’t working. I can’t award badges to categories that are limited to certain trust levels.

You can do this. The problem is that the query is using the `badge_posts` table. The `badge_posts` table doesn’t include posts from protected categories. If the query is changed to use the `posts` table instead of the `badge_posts` table, it should work.

---

<div class="post-metadata">

### Author: ![Bernstein](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bernstein/32/304070_2.png) [@Bernstein](https://meta.discourse.org/u/Bernstein)
#### Post date: [December 3, 2019, 10:34am UTC](https://meta.discourse.org/t/need-help-with-an-sql-badge-query/72021/7 "2019-12-03T10:34:53Z")

</div>

Oh wow, thank you so much!

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [September 8, 2023, 7:01am UTC](https://meta.discourse.org/t/need-help-with-an-sql-badge-query/72021/8 "2023-09-08T07:01:01Z")

</div>



---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [October 8, 2023, 7:01am UTC](https://meta.discourse.org/t/need-help-with-an-sql-badge-query/72021/9 "2023-10-08T07:01:16Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
