# SQL query to get the queue of all the PMs in the system

**URL:** https://meta.discourse.org/t/sql-query-to-get-the-queue-of-all-the-pms-in-the-system/38089
**Category:** Support
**Created:** [1월 17, 2016, 11:06오후 UTC](https://meta.discourse.org/t/sql-query-to-get-the-queue-of-all-the-pms-in-the-system/38089 "2016-01-17T23:06:09Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![meglio](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/meglio/32/71444_2.png) [@meglio](https://meta.discourse.org/u/meglio)
#### Post date: [1월 17, 2016, 11:06오후 UTC](https://meta.discourse.org/t/sql-query-to-get-the-queue-of-all-the-pms-in-the-system/38089/1 "2016-01-17T23:06:09Z")

</div>

Please help - can’t figure out how PMs are stored in the database. Is it still in topics/posts? If yes, then how would I identify, which topic is private and for which users?

---

<div class="post-metadata">

### Author: ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)
#### Post date: [1월 18, 2016, 7:07오후 UTC](https://meta.discourse.org/t/sql-query-to-get-the-queue-of-all-the-pms-in-the-system/38089/2 "2016-01-18T19:07:22Z")

</div>

It uses the same tables but PM topics have an `archetype = 'private_message'`. To see who can access that private message, look inside the `topic_allowed_users` for the `topic_id`.

---

<div class="post-metadata">

### Author: ![meglio](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/meglio/32/71444_2.png) [@meglio](https://meta.discourse.org/u/meglio)
#### Post date: [1월 27, 2016, 1:52오전 UTC](https://meta.discourse.org/t/sql-query-to-get-the-queue-of-all-the-pms-in-the-system/38089/3 "2016-01-27T01:52:35Z")

</div>

Here is the query to get all PMs for last 7 days.

```sql
SELECT p.user_id, p.topic_id, p.post_number, p.raw, p.created_at::date
FROM posts p
LEFT JOIN topics t on t.id = p.topic_id
WHERE t.archetype = 'private_message'
  AND t.title <> 'YOUR_WELCOME_MESSAGE_TITLE'
  AND p.created_at::date > now()::date - 8
ORDER BY p.created_at DESC

```

- Replace `YOUR_WELCOME_MESSAGE_TITLE` by the exact title of your welcome message, so that all welcome messages generated automatically are excluded (is there a better way to exclude automatically generated messages?)

- Adjust how many days to look back for by changing the number in **`now()::date - 8`**.  
8 means 7 days;  
7 means 6 days  
…and so on.

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [6월 8, 2024, 12:45오후 UTC](https://meta.discourse.org/t/sql-query-to-get-the-queue-of-all-the-pms-in-the-system/38089/4 "2024-06-08T12:45:53Z")

</div>

이 주제는 3064일 후 자동으로 닫혔습니다. 더 이상 새 답변을 게시할 수 없습니다.
