简体   繁体   中英

SQL get values without duplicates (that are different in case)

I'm trying to get a list of all users in my database, but when I run the following query:

SELECT cu.user_name AS username, cu.display_name AS displayname, 
cm.lower_parent_name AS group, cu.email_address AS email
FROM cwd_user AS cu
INNER JOIN cwd_membership AS cm
ON cu.directory_id = cm.directory_id
AND cu.lower_user_name = cm.lower_child_name
AND cm.membership_type = 'GROUP_USER'
WHERE cm.lower_parent_name LIKE 'zz%'
ORDER BY cu.user_name;

I get duplicate entries (that's OK, because I have multiple values for the same user) but I'm only interested in the lowercase one.

+------------------------------------------------------------------+
|      username        |    displayname     |   group  |   email   |
+------------------------------------------------------------------+
|   firstname.lastname | Firstname Lastname | zz group | f.l@a.com |
|   Firstname.Lastname | Firstname Lastname | zz group | F.L@a.com |
+------------------------------------------------------------------+

I just want one of these users (preferably the first one), so I tried the following SQL query:

SELECT t.user_name, cu.display_name, cm.lower_parent_name, 
cu.email_address
FROM (
SELECT cu.user_name
FROM cwd_user AS cu
INNER JOIN cwd_membership AS cm ON cu.directory_id=cm.directory_id
AND cu.lower_user_name=cm.lower_child_name
AND cm.membership_type='GROUP_USER'
WHERE cm.lower_parent_name LIKE 'zz%'
GROUP BY LOWER(cu.user_name) ) u JOIN cwd_user t ON t.user_name = 
u.user_name ORDER BY t.user_name;

but with no luck. My SQL isn't that great, so I'm stuck here.

Any thoughts?

You sould select the proper value in the subselect and refer with the proper alias and you should use distinct not group by lower

SELECT  distinct 
        t.user_name
      , u.display_name
      , u.lower_parent_name
      , u.email_address
FROM (
  SELECT 
        cu.user_name
      , cu.display_name
      , cm.lower_parent_name
      , cu.email_address
  FROM cwd_user AS cu
  INNER JOIN cwd_membership AS cm  ON cu.directory_id=cm.directory_id
              AND cu.lower_user_name=cm.lower_child_name
              AND cm.membership_type='GROUP_USER'
  WHERE cm.lower_parent_name LIKE 'zz%'
) u 
JOIN cwd_user t ON t.user_name = u.user_name 
ORDER BY t.user_name;

and if you need only the lowercase user_name you can try binary lower

SELECT  distinct 
        t.user_name
      , u.display_name
      , u.lower_parent_name
      , u.email_address
FROM (
  SELECT 
        cu.user_name
      , cu.display_name
      , cm.lower_parent_name
      , cu.email_address
  FROM cwd_user AS cu
  INNER JOIN cwd_membership AS cm  ON cu.directory_id=cm.directory_id
              AND cu.lower_user_name=cm.lower_child_name
              AND cm.membership_type='GROUP_USER'
  WHERE cm.lower_parent_name LIKE 'zz%'
  AND BINARY cu.user_name = lower( cu.user_name)
) u 
JOIN cwd_user t ON t.user_name = u.user_name 
ORDER BY t.user_name;
SELECT distinct (t.user_name), cu.display_name, cm.lower_parent_name, 
cu.email_address
FROM (
SELECT cu.user_name
FROM cwd_user AS cu
INNER JOIN cwd_membership AS cm ON cu.directory_id=cm.directory_id
AND cu.lower_user_name=cm.lower_child_name
AND cm.membership_type='GROUP_USER'
WHERE cm.lower_parent_name LIKE 'zz%'
GROUP BY LOWER(cu.user_name) ) u JOIN cwd_user t ON t.user_name = 
u.user_name ORDER BY t.user_name;

In SQL SERVER, DISTINCT keyword picks out only different data.

我最终在一个能解决问题的bash脚本中创建了两个单独的查询。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM