# A badge awarded to posts that contain attachments?

**URL:** https://meta.discourse.org/t/a-badge-awarded-to-posts-that-contain-attachments/276676
**Category:** Data & reporting
**Tags:** sql-triggered-badge
**Created:** [27 januari 2015 om 07:37 UTC](https://meta.discourse.org/t/a-badge-awarded-to-posts-that-contain-attachments/276676 "2015-01-27T07:37:16Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![mochabcha](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mochabcha/32/115463_2.png) [@mochabcha](https://meta.discourse.org/u/mochabcha)
#### Post date: [27 januari 2015 om 07:37 UTC](https://meta.discourse.org/t/a-badge-awarded-to-posts-that-contain-attachments/276676/1 "2015-01-27T07:37:16Z")

</div>

How about a badge awarded to posts that contain attachments? What sql query would accomplish that?

---

<div class="post-metadata">

### Author: ![PJH](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pjh/32/39071_2.png) [@PJH](https://meta.discourse.org/u/PJH)
#### Post date: [15 februari 2015 om 14:14 UTC](https://meta.discourse.org/t/a-badge-awarded-to-posts-that-contain-attachments/276676/2 "2015-02-15T14:14:09Z")

</div>

> [@mochabcha](#):
>
> How about a badge awarded to posts that contain attachments? What sql query would accomplish that?

Depends on what about those posts you’re after. Basically a post with an attachment is generally one with a picture in, so something like:

```sql
SELECT user_id
FROM posts
WHERE raw ilike '%<img src="%"'

```

would probably be a good start.

---

<div class="post-metadata">

### Author: ![mochabcha](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mochabcha/32/115463_2.png) [@mochabcha](https://meta.discourse.org/u/mochabcha)
#### Post date: [10 april 2015 om 15:07 UTC](https://meta.discourse.org/t/a-badge-awarded-to-posts-that-contain-attachments/276676/3 "2015-04-10T15:07:13Z")

</div>

[https://meta.discourse.org/t/what-cool-badge-queries-have-you-come-up-with/18978/23?u=mochabcha](https://meta.discourse.org/t/what-cool-badge-queries-have-you-come-up-with/18978/23)

I get a message of

`An error occurred: Contract violation: Query does not return a 'granted_at' column`

when trying that one.

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [10 april 2015 om 17:59 UTC](https://meta.discourse.org/t/a-badge-awarded-to-posts-that-contain-attachments/276676/4 "2015-04-10T17:59:58Z")

</div>

Change the beginning to one of the following:

```
SELECT user_id, current_timestamp granted_at
SELECT user_id, post.created_at granted_at
SELECT user_id, post.updated_at granted_at

```

Remember that bronze badges not “granted\_at” the last ~48 hours will not give a notification.
