# Creating custom badge with custom rules

**URL:** https://meta.discourse.org/t/creating-custom-badge-with-custom-rules/257688
**Category:** Data & reporting
**Tags:** sql-triggered-badge
**Created:** [March 10, 2023, 11:27am UTC](https://meta.discourse.org/t/creating-custom-badge-with-custom-rules/257688 "2023-03-10T11:27:22Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Kushalghosh9899](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kushalghosh9899/32/292844_2.png) [@Kushalghosh9899](https://meta.discourse.org/u/Kushalghosh9899)
#### Post date: [March 10, 2023, 11:27am UTC](https://meta.discourse.org/t/creating-custom-badge-with-custom-rules/257688/1 "2023-03-10T11:27:22Z")

</div>

I want to create a custom badge in which these rules will be added, which simply means that when user accomplish these below things then the particular related badge will be given to them automatically.  
Users need to accomplish these below things/Rules -

1. User should have created at least 2 posts
2. Uses should have uploaded profile pic
3. User should have liked at least one post

Note- I have seen some badges creation with the help of SQL queries, actually I am very new to this so please kindly tell me where to put SQL queries (if required) to achieve these above rules.

---

<div class="post-metadata">

### Author: ![Lhc\_fl](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lhc_fl/32/268115_2.png) [@Lhc\_fl](https://meta.discourse.org/u/Lhc_fl)
#### Post date: [March 10, 2023, 3:01pm UTC](https://meta.discourse.org/t/creating-custom-badge-with-custom-rules/257688/2 "2023-03-10T15:01:02Z")

</div>

May it help?

> [@Enable Badge SQL](https://meta.discourse.org/t/enable-badge-sql/47894):
>
> bookmark This guide explains how to enable Badge SQL in Discourse and discusses the associated security and performance risks. person_raising_hand Required user level: Administrator warning Enabling Badge SQL can pose security and performance risks. To maintain security and performance integrity, as of Discourse 1.6, the ability for administrators to edit badge SQL directly is disabled by default. The two primary concerns around enabling Badge SQL are: Security: Allowing direct SQL …

> [@\[Superseded\] What cool badge queries have you come up with?](https://meta.discourse.org/t/what-cool-badge-queries-have-you-come-up-with/18978):
>
> warning Enabling Badge SQL entails security and performance risks so it is not available by default. For more information, see: [Enable Badge SQL](https://meta.discourse.org/t/badge-sql-can-no-longer-be-edited-by-default/47894) This topic became too long to be easily searched and has been broken down into separate topics. If you have any questions about how to write a badge query, or have one to share, please start a new topic in the #data & reporting category and tag it with the #sql-triggered-badge tag. +1 More ideas: [Some common badge queries](https://meta.discourse.org/t/some-common-badge-queries-idea/31859)

---

<div class="post-metadata">

### Author: ![Kushalghosh9899](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kushalghosh9899/32/292844_2.png) [@Kushalghosh9899](https://meta.discourse.org/u/Kushalghosh9899)
#### Post date: [March 11, 2023, 11:24am UTC](https://meta.discourse.org/t/creating-custom-badge-with-custom-rules/257688/3 "2023-03-11T11:24:23Z")

</div>

hey, this is really helpful.  
I have enabled the SQL queries option, just stuck with a small thing after that I am good to go.

How to view the database because my custom badge rule is complex so that is why I need few things -

1. How to view the database, so that I can build my own query
2. How to test these badges after building queries.

Kindly answer these 2 things after that I can peacefully sleep.

---

<div class="post-metadata">

### Author: ![Lhc\_fl](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lhc_fl/32/268115_2.png) [@Lhc\_fl](https://meta.discourse.org/u/Lhc_fl)
#### Post date: [March 11, 2023, 11:28am UTC](https://meta.discourse.org/t/creating-custom-badge-with-custom-rules/257688/4 "2023-03-11T11:28:46Z")

</div>

The easiest way is to install the [discourse-data-explorer](https://github.com/discourse/discourse-data-explorer) plugin, which will provide an interface like this.

 ![image](https://global.discourse-cdn.com/meta/original/4X/f/f/8/ff8f08a9901771d6d80ebefe90f641ddfda3f6ad.png)

 ![image](https://global.discourse-cdn.com/meta/original/4X/a/a/e/aae5703a020d3ac57b6f55e2c7b3e08d5ab8aadb.png)

---

<div class="post-metadata">

### Author: ![Kushalghosh9899](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kushalghosh9899/32/292844_2.png) [@Kushalghosh9899](https://meta.discourse.org/u/Kushalghosh9899)
#### Post date: [March 13, 2023, 6:44am UTC](https://meta.discourse.org/t/creating-custom-badge-with-custom-rules/257688/5 "2023-03-13T06:44:59Z")

</div>

I have installed the plugin and made the below SQL command which is giving me the users who are fulfilling these 3 rules but I don’t know how to convert it into a trigger when an user acts on the post.

SQL command -  
`select users.username, user_stats.user_id from user_stats Inner Join users ON user_stats.user_id = users.id where post_count = 2 AND likes_given = 1 AND uploaded_avatar_id > 0`

Following this post [Creating triggered custom badge queries](https://meta.discourse.org/t/create-triggered-custom-badge-queries/19336) but still unable to create because I am really new to the discourse.

Kindly help me out to fix this, if I can complete this once then I can build my other own custom badges too with less friction.  
@Lhc_fl

---

<div class="post-metadata">

### Author: ![Lhc\_fl](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lhc_fl/32/268115_2.png) [@Lhc\_fl](https://meta.discourse.org/u/Lhc_fl)
#### Post date: [March 13, 2023, 3:03pm UTC](https://meta.discourse.org/t/creating-custom-badge-with-custom-rules/257688/6 "2023-03-13T15:03:07Z")

</div>

This is a SQL code of “Trigger: Update daily”;

```sql
SELECT st.user_id user_id, current_timestamp granted_at
FROM user_stats st
JOIN users u
ON st.user_id = u.id
WHERE st.post_count >= 2 AND st.likes_given >= 1 AND u.uploaded_avatar_id > 0

```

The other triggers depend on something specific, and I don’t recommend using them unless you find a SQL expert. (I’m not familiar with SQL myself

---

<div class="post-metadata">

### Author: ![Kushalghosh9899](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kushalghosh9899/32/292844_2.png) [@Kushalghosh9899](https://meta.discourse.org/u/Kushalghosh9899)
#### Post date: [March 14, 2023, 5:07am UTC](https://meta.discourse.org/t/creating-custom-badge-with-custom-rules/257688/8 "2023-03-14T05:07:58Z")

</div>

Thank you so much for this, just having three basic questions -

1. What is **st** and **u**  **table** and what is the use of both this table and why these tables are not visible in the explorer plugin tab

2. When this trigger update daily will run, to be specific I am asking about time.

3. What is the use of **Run revocation query daily**

---

<div class="post-metadata">

### Author: ![ClawdiaWolf](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/clawdiawolf/32/262518_2.png) [@ClawdiaWolf](https://meta.discourse.org/u/ClawdiaWolf)
#### Post date: [March 14, 2023, 6:31am UTC](https://meta.discourse.org/t/creating-custom-badge-with-custom-rules/257688/9 "2023-03-14T06:31:44Z")

</div>

With regards to 1?, those are table alias names.

Do you see the FROM user\_stats st line? That’s telling the query that st is short for the user\_stats table

The same is true in JOIN statement where it says **users u**. It is saying that in query u is an alias for the users table in that query. Aliases are used to allow simplifying queries to use less typing which reduces the chances of errors.

With regards to 3, if you enable that option then if a user no longer qualifies for a badge based on the query it will remove the badge from their account.

---

<div class="post-metadata">

### Author: ![Kushalghosh9899](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kushalghosh9899/32/292844_2.png) [@Kushalghosh9899](https://meta.discourse.org/u/Kushalghosh9899)
#### Post date: [March 14, 2023, 7:41am UTC](https://meta.discourse.org/t/creating-custom-badge-with-custom-rules/257688/10 "2023-03-14T07:41:57Z")

</div>

Thanks for clarifying my doubts, now just the second point is remaining

1. When this trigger update daily will run, to be specific I am asking about time.

---

<div class="post-metadata">

### Author: ![ClawdiaWolf](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/clawdiawolf/32/262518_2.png) [@ClawdiaWolf](https://meta.discourse.org/u/ClawdiaWolf)
#### Post date: [March 14, 2023, 7:48am UTC](https://meta.discourse.org/t/creating-custom-badge-with-custom-rules/257688/11 "2023-03-14T07:48:49Z")

</div>

Whoops. Thought I answered that.

The query runs every 24 hours based on what time of day you started the container. So I have one instance that runs it around 0:00UTC (roughly) and another instance runs it a little after 6:00 UTC

---

<div class="post-metadata">

### Author: ![Kushalghosh9899](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kushalghosh9899/32/292844_2.png) [@Kushalghosh9899](https://meta.discourse.org/u/Kushalghosh9899)
#### Post date: [March 14, 2023, 8:02am UTC](https://meta.discourse.org/t/creating-custom-badge-with-custom-rules/257688/12 "2023-03-14T08:02:44Z")

</div>

Can we change the timings at which this query will run or can we modify this query to` Trigger: when user acts on a posts` or when `user edits or creates a post`?  
If possible kindly change the query as I am new to this discourse system if you can help me out once then from next time I can build my own custom badge.
