# Query to filter users by sign-up 'custom fields'

**URL:** https://meta.discourse.org/t/query-to-filter-users-by-sign-up-custom-fields/100837
**Category:** Data & reporting
**Tags:** sql-query, user-custom-fields
**Created:** [October 30, 2018, 1:50am UTC](https://meta.discourse.org/t/query-to-filter-users-by-sign-up-custom-fields/100837 "2018-10-30T01:50:50Z")
**Posts on this page:** 1
**Showing post:** 2

<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: [October 30, 2018, 11:53pm UTC](https://meta.discourse.org/t/query-to-filter-users-by-sign-up-custom-fields/100837/2 "2018-10-30T23:53:26Z")

</div>

To get all values for a named field, try this, with the name of your field entered into the field\_name input:

```sql
--[params]
-- string :field_name

WITH ucf_name AS(
SELECT
CONCAT('user_field_', id) AS name
FROM user_fields
WHERE name = :field_name
)

SELECT
u.id AS user_id,
ucf.value
FROM users u
JOIN user_custom_fields ucf
ON ucf.user_id = u.id
WHERE ucf.name = (SELECT name FROM ucf_name)

```

To get the value of a user field for a specific user:

```sql
--[params]
-- string :field_name
-- string :username

WITH ucf_name AS(
SELECT
CONCAT('user_field_', id) AS name
FROM user_fields
WHERE name = :field_name
)

SELECT
u.id AS user_id,
ucf.value
FROM users u
JOIN user_custom_fields ucf
ON ucf.user_id = u.id
WHERE ucf.name = (SELECT name FROM ucf_name)
AND u.username = :username

```

To only get users who have entered a specific value for a field, try this, with the name of the field entered into the field\_name input, and the value you are looking for entered into the field\_value input:

```sql
--[params]
-- string :field_name
-- string :field_value

WITH ucf_name AS(
SELECT
CONCAT('user_field_', id) AS name
FROM user_fields
WHERE name = :field_name
)

SELECT
u.id AS user_id
FROM users u
JOIN user_custom_fields ucf
ON ucf.user_id = u.id
WHERE ucf.name = (SELECT name FROM ucf_name)
AND ucf.value = :field_value

```

---

_[View the full topic](https://meta.discourse.org/t/query-to-filter-users-by-sign-up-custom-fields/100837)._
