# 徽章 SQL 错误：q.user\_id

**URL:** https://meta.discourse.org/t/badge-sql-error-q-user-id/86640
**Category:** Data & reporting
**Tags:** sql-triggered-badge
**Created:** [2018年五月2日 21:13 UTC](https://meta.discourse.org/t/badge-sql-error-q-user-id/86640 "2018-05-02T21:13:22Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![hross](https://avatars.discourse-cdn.com/v4/letter/h/65b543/32.png) [@hross](https://meta.discourse.org/u/hross)
#### Post date: [2018年五月2日 21:13 UTC](https://meta.discourse.org/t/badge-sql-error-q-user-id/86640/1 "2018-05-02T21:13:22Z")

</div>

你好！

我正在为那些帖子通过“解决方案复选框”插件被标记为解决方案的用户创建一个徽章。这是 SQL 查询：

```
SELECT
       u.username AS "username",
       count(*) AS "count_received_best_answer",
       current_timestamp granted_at
FROM
       notifications n
JOIN
       users u ON n.user_id = u.id
JOIN
       topics t ON n.topic_id = t.id
JOIN
       categories c ON t.category_id = c.id
WHERE
       n.notification_type = 14 AND
       n.data LIKE '%solved.accepted_notification%' AND 
       n.created_at > CURRENT_DATE - INTERVAL '30 days'
GROUP BY u.username
HAVING count(*) > 0
ORDER BY count(*)

```

然而，当我尝试预览徽章时，出现了以下错误：

 ![23%20PM](https://global.discourse-cdn.com/meta/original/3X/7/6/76a7989fbd435cf5b2277666a2a873ebb1607a1f.png)

通过在此处搜索，我了解到 q.user\_id 是这些徽章作业后台中的一个字段，但我无法弄清楚为什么会出现这个错误。感谢任何帮助！

---

<div class="post-metadata">

### Author: ![gerhard](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gerhard/32/119479_2.png) [@gerhard](https://meta.discourse.org/u/gerhard)
#### Post date: [2018年五月2日 21:35 UTC](https://meta.discourse.org/t/badge-sql-error-q-user-id/86640/2 "2018-05-02T21:35:14Z")

</div>

Your query needs to return at least the following columns:

- `user_id`
- `granted_at `

Also, you should definitely take a look at [Add Badge to Solve answer - #2 by dax](https://meta.discourse.org/t/add-badge-to-solve-answer/79126/2). 😉

---

<div class="post-metadata">

### Author: ![hross](https://avatars.discourse-cdn.com/v4/letter/h/65b543/32.png) [@hross](https://meta.discourse.org/u/hross)
#### Post date: [2018年五月2日 21:55 UTC](https://meta.discourse.org/t/badge-sql-error-q-user-id/86640/3 "2018-05-02T21:55:47Z")

</div>

YES. That was all I needed. Not sure why staring at it for four hours did not make it obvious. thanks much @gerhard

---

<div class="post-metadata">

### Author: ![jerdog](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jerdog/32/122843_2.png) [@jerdog](https://meta.discourse.org/u/jerdog)
#### Post date: [2018年五月2日 22:28 UTC](https://meta.discourse.org/t/badge-sql-error-q-user-id/86640/4 "2018-05-02T22:28:42Z")

</div>

How does this differ from the [Add Badge to Solve answer](https://meta.discourse.org/t/add-badge-to-solve-answer/79126/2) topic? I am looking to do the same thing but unsure if either one would be applied for _every_ solution or a set number?

---

<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: [2020年十月11日 10:24 UTC](https://meta.discourse.org/t/badge-sql-error-q-user-id/86640/5 "2020-10-11T10:24:27Z")

</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: [2023年八月26日 22:15 UTC](https://meta.discourse.org/t/badge-sql-error-q-user-id/86640/6 "2023-08-26T22:15:40Z")

</div>

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