# How to query a confirmation-type custom user field?

**URL:** <https://meta.discourse.org/t/how-to-query-a-confirmation-type-custom-user-field/68397>\
**Category:** Support\
**Created:** [19. August 2017 um 23:12 UTC](https://meta.discourse.org/t/how-to-query-a-confirmation-type-custom-user-field/68397 "2017-08-19T23:12:11Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)\
**Post date:** [20. August 2017 um 22:49 UTC](https://meta.discourse.org/t/how-to-query-a-confirmation-type-custom-user-field/68397/6 "2017-08-20T22:49:19Z")

</div>

This is because a row is only added to the user\_custom\_fields table **after** a user has changed the value. So what you want to do is

- Go through the users table
- For each user, see if there’s a user\_custom\_fields row for `user_field_5`
- If so, check whether it’s true, otherwise assume it’s false

In my testing, ticking a boolean saves as `true` in the database, while unticking saves as `NULL`

So I think the SQL you want is

```plaintext
SELECT u.id as user_id, cf.value
FROM users u
LEFT JOIN user_custom_fields cf 
ON (cf.user_id = u.id and cf.name like 'user_field_5')
WHERE cf.value IS DISTINCT FROM 'true'

```

Note that `IS DISTINCT FROM` is needed rather than `!=`, because it can deal with one of the values being `NULL`.

---

_[View the full topic](https://meta.discourse.org/t/how-to-query-a-confirmation-type-custom-user-field/68397)._
