I have a custom dropdown field setup on sign up where users select a ‘Category’ that best suits them.
How can I find users based on what they selected within that dropdown?
I have a custom dropdown field setup on sign up where users select a ‘Category’ that best suits them.
How can I find users based on what they selected within that dropdown?
To get all values for a named field, try this, with the name of your field entered into the field_name input:
--[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:
--[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:
--[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
Trying to find the correct query to be able to search all users which have a custom user field Pro Email, but to find the users which are not in the Pro group?
I moved your post to this topic. I think the last query in my previous post in this topic will get you close to what you are looking for. If your custom field is a confirmation type, enter ‘true’ for the field_value parameter. That will return all users who have checked the confirmation box for the field name you supply for the query’s field_name parameter.
The query will need to be added to so that you can filter out users who are already in your Pro group.
Any example for a query on how to filter out users already in that group and then only return users not in that group?
I have not used the data explorer plugin for Discourse that much.
Here’s one way to do it. It adds an excluded_users temporary table that’s made up of members of the excluded group. You will need to supply the group’s Name for the query’s group_name parameter.
--[params]
-- string :field_name
-- string :field_value
-- string :group_name
WITH ucf_name AS(
SELECT
CONCAT('user_field_', id) AS name
FROM user_fields
WHERE name = :field_name
),
excluded_users AS (
SELECT
gu.user_id
FROM group_users gu
JOIN groups g
ON g.id = gu.group_id
WHERE g.name = :group_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
AND u.id NOT IN (SELECT user_id FROM excluded_users)
Thank you for that data explorer query, much appreciated.
What would need to defined in the field_value since I just need to have to process if it has a value set for the Pro Email user custom field?
If Pro Email is a confirmation field (checkbox) then setting the query’s field_value parameter to true will work.
SInce all you are wanting to do is check that the field has been set, I think this version of the query will work better for you. It removes the field_value parameter and checks that the named field has been set with (ucf.value = '') IS NOT TRUE. This will return false for any users who have not set the field, and will also return false for users who have set the field and then deleted the value they set:
--[params]
-- string :field_name
-- string :group_name
WITH ucf_name AS(
SELECT
CONCAT('user_field_', id) AS name
FROM user_fields
WHERE name = :field_name
),
excluded_users AS (
SELECT
gu.user_id
FROM group_users gu
JOIN groups g
ON g.id = gu.group_id
WHERE g.name = :group_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 = '') IS NOT TRUE
AND u.id NOT IN (SELECT user_id FROM excluded_users)
If you’d like the query to return the field’s value, change the final select statement to:
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 (ucf.value = '') IS NOT TRUE
AND u.id NOT IN (SELECT user_id FROM excluded_users)
Thank you it works great!
您好 @simon ,如果我有多个自定义字段,并且只想返回用户列表以及他们对这些自定义字段的回答,那么以下哪个查询会起作用?我有一些问题,例如“您从哪里得知我们”,其中包含允许多项选择的复选框,以及另一个询问他们希望如何联系(电子邮件、短信和邮寄)的问题。
谢谢
Clement
以上所有查询都不能完全满足您的需求。在不编写新查询的情况下,最好的方法是多次运行我发布的第一个查询。为每个自定义字段运行一次。将字段名称输入查询的 field_name 输入中。我指的是这个查询:
--[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)