Mysql 多条件sql语句
我有一个包含英国或美国用户和国家的表格,现在一个用户可以在不同的国家出现多次。我需要选择一个具有唯一用户的列表,其国家或等于英国、美国或两者 这是我尝试的方法,但它似乎不起作用Mysql 多条件sql语句,mysql,Mysql,我有一个包含英国或美国用户和国家的表格,现在一个用户可以在不同的国家出现多次。我需要选择一个具有唯一用户的列表,其国家或等于英国、美国或两者 这是我尝试的方法,但它似乎不起作用 select user, case when COUNT(*) <= 1 and count(select where = "UK") = count(*) then "UK" when COUNT(*) <= 1 and count(select where =
select user,
case
when COUNT(*) <= 1 and count(select where = "UK") = count(*) then "UK"
when COUNT(*) <= 1 and count(select where = "US") = count(*) then "US"
when COUNT(*) <= 1 and count(select where = "US") != count(*) then "BOTH"
END CASE
as country from users;
你可以这样做:
SELECT
temp.user,
if(temp.CountryList = "UK,US", "BOTH", temp.CountryList) as country
FROM
(
SELECT
user,
group_concat(DISTINCT country ORDER BY country) as CountryList
FROM USERS
GROUP BY user
) temp
请提供表格结构和一些条目。
SELECT
temp.user,
if(temp.CountryList = "UK,US", "BOTH", temp.CountryList) as country
FROM
(
SELECT
user,
group_concat(DISTINCT country ORDER BY country) as CountryList
FROM USERS
GROUP BY user
) temp