MYSQL从3个不同的字段中获取DISTINCT行

时间:2018-10-11 04:26:06

标签: mysql

嗨,我将如何更改此查询,以使其输出不同的结果。我需要对iosroid进行3个查询,如果两个ios & roid都是NULL ...,则基于u.sacid并获取最新的列表在last_login(日期)之前确定。如果可以将所有3个查询合并为一个查询,那将是很好的选择。棘手的部分是用户0101可以有一个IOS条目,然后同一用户0101的另一行可以有一个机器人条目。

现在,我的查询仅能捕获所有内容,并且我使用PHP进行过滤...代码更多,效率不高。

$sql = "select ur.user_id, u.sacid, u.fname, u.lname, u.email, u.last_login, ur.role_id, 
        r.name as role_name, i.device_token AS ios, dg.group_name, dg.ad_group, 
        a.device_token AS roid
        FROM users AS u
        inner join user_roles AS ur on u.id = ur.user_id
        inner join roles AS r on ur.role_id = r.id
        left outer join ios_tokens AS i on u.sacid = i.sacf_id
        left outer join roid_tokens AS a on u.sacid = a.sacf_id
        left outer join board_groups AS dg on dg.role_id = r.id
        order by u.email asc";

当前结果输出

+---------+---------+-------+-------+--------------+---------------------+---------+-----------+-----------+------------+----------+----------+
| user_id | sacid   | fname | lname | email        | last_login          | role_id | role_name | ios       | group_name | ad_group | roid     |
+---------+---------+-------+-------+--------------+---------------------+---------+-----------+-----------+------------+----------+----------+
| 313     | p139k3l | John  | Doe   | john@doe.com | 2018-09-24 16:23:46 | 6       | ops       | NULL      | MainDevice | Offser   | NULL     |
+---------+---------+-------+-------+--------------+---------------------+---------+-----------+-----------+------------+----------+----------+
| 313     | p139k3l | John  | Doe   | john@doe.com | 2018-09-24 16:15:46 | 6       | ops       | NULL      | MainDevice | Offser   | NULL     |
+---------+---------+-------+-------+--------------+---------------------+---------+-----------+-----------+------------+----------+----------+
| 303     | m2363   | Scot  | Do    | Jane@do.com  | 2018-09-14 13:43:33 | 6       | ops       | fSerXerer | Sites      | FS2      | NULL     |
+---------+---------+-------+-------+--------------+---------------------+---------+-----------+-----------+------------+----------+----------+
| 303     | m2363   | Scot  | Do    | Jane@do.com  | 2018-09-14 13:23:33 | 6       | ops       | NULL      | Ops        | IFS      | UxAG_IFS |
+---------+---------+-------+-------+--------------+---------------------+---------+-----------+-----------+------------+----------+----------+
| 163     | m5217   | Val   | Baa   | Val@baa.com  | 2018-06-13 17:31:20 | 6       | ops       | CSerde    | ICOM       | COM_SITE | NULL     |
+---------+---------+-------+-------+--------------+---------------------+---------+-----------+-----------+------------+----------+----------+

所需的输出

+---------+---------+-------+-------+--------------+---------------------+---------+-----------+-----------+------------+----------+----------+
| user_id | sacid   | fname | lname | email        | last_login          | role_id | role_name | ios       | group_name | ad_group | roid     |
+---------+---------+-------+-------+--------------+---------------------+---------+-----------+-----------+------------+----------+----------+
| 313     | p139k3l | John  | Doe   | john@doe.com | 2018-09-24 16:23:46 | 6       | ops       | NULL      | MainDevice | Offser   | NULL     |
+---------+---------+-------+-------+--------------+---------------------+---------+-----------+-----------+------------+----------+----------+
| 303     | m2363   | Scot  | Do    | Jane@do.com  | 2018-09-14 13:43:33 | 6       | ops       | fSerXerer | Sites      | FS2      | NULL     |
+---------+---------+-------+-------+--------------+---------------------+---------+-----------+-----------+------------+----------+----------+
| 303     | m2363   | Scot  | Do    | Jane@do.com  | 2018-09-14 13:23:33 | 6       | ops       | NULL      | Ops        | IFS      | UxAG_IFS |
+---------+---------+-------+-------+--------------+---------------------+---------+-----------+-----------+------------+----------+----------+
| 163     | m5217   | Val   | Baa   | Val@baa.com  | 2018-06-13 17:31:20 | 6       | ops       | CSerde    | ICOM       | COM_SITE | NULL     |
+---------+---------+-------+-------+--------------+---------------------+---------+-----------+-----------+------------+----------+----------+

1 个答案:

答案 0 :(得分:1)

使用不同的

$sql = "select distinct ur.user_id, u.sacid, u.fname, u.lname, u.email, u.last_login, ur.role_id, 
        r.name as role_name, i.device_token AS ios, dg.group_name, dg.ad_group, 
        a.device_token AS roid
        FROM users AS u
        inner join user_roles AS ur on u.id = ur.user_id
        inner join roles AS r on ur.role_id = r.id
        left outer join ios_tokens AS i on u.sacid = i.sacf_id
        left outer join roid_tokens AS a on u.sacid = a.sacf_id
        left outer join board_groups AS dg on dg.role_id = r.id
        order by u.email asc";