仅从一列中删除重复项

时间:2016-02-23 05:37:42

标签: mysql sql database

所以我会有这样的事情。它的结果是对两个表的查询。

+------------------+------------------------------------+
| character_name   | title                              |
+------------------+------------------------------------+
| derp             | a                                  |
| derp             | b                                  |
| derp             | c                                  | 
| herp             | a                                  |
| herp             | b                                  |
| herp             | c                                  |
+-------------------------------------------------------+

我想要这个

+------------------+------------------------------------+
| character_name   | title                              |
+------------------+------------------------------------+
| derp             | a                                  |
|                  | b                                  |
|                  | c                                  | 
| herp             | a                                  |
|                  | b                                  |
|                  | c                                  |
+-------------------------------------------------------+

这可能吗?

3 个答案:

答案 0 :(得分:4)

这是你的意思吗?

select case when i.rnk = 1 THEN i.character_name  ELSE '' END as NAME
, i.title
from(
    select *
    , row_number() over (partition by character_name order by title) as rnk
    from t1
) i
order by i.character_name 

第一个名称将获得值1,您在结果中显示,您隐藏的任何其他内容。

更新 - MySql版本

SET @r_name:='';

select name, title FROM (
    select @r_name:=CASE WHEN @r_name = name THEN '' 
    ELSE name
    END AS name
    , @r_name:=name
    , title
    FROM
    t1
) t2

答案 1 :(得分:2)

您可以在用户变量的帮助下完成此操作。

SQL:

SET @last_name = '';
SELECT
    IF(switch_name, tbl2.character_name, '') character_name,
    tbl2.title
FROM
    (
    SELECT 
        character_name, 
        title, 
        character_name != @last_name switch_name, 
        @last_name:=character_name 
    FROM
        tbl
    ) tbl2;

结果:

mysql> SELECT character_name, title FROM tbl;
+----------------+-------+
| character_name | title |
+----------------+-------+
| derp           | a     |
| derp           | b     |
| derp           | c     |
| herp           | a     |
| herp           | b     |
| herp           | c     |
+----------------+-------+
6 rows in set (0.00 sec)

mysql> SET @last_name = '';
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> SELECT
    ->     IF(switch_name, tbl2.character_name, '') character_name,
    ->     tbl2.title
    -> FROM
    ->     (
    ->     SELECT
    ->         character_name,
    ->         title,
    ->         character_name != @last_name switch_name,
    ->         @last_name:=character_name
    ->     FROM
    ->         tbl
    ->     ) tbl2;
+----------------+-------+
| character_name | title |
+----------------+-------+
| derp           | a     |
|                | b     |
|                | c     |
| herp           | a     |
|                | b     |
|                | c     |
+----------------+-------+
6 rows in set (0.00 sec)

答案 2 :(得分:2)

它适用于您的指定表

   SELECT CASE  
    WHEN character_name ='derp' AND title ='a' THEN 'derp'
    WHEN character_name ='derp' AND title ='b' THEN ''
    WHEN character_name ='derp' AND title ='c' THEN ''
    WHEN character_name ='herp' AND title ='a' THEN 'herp'
    WHEN character_name ='herp' AND title ='b' THEN ''
    WHEN character_name ='herp' AND title ='c' THEN ''
    end as character_name,
    title
    from table_name;

注意:如果您的数据是动态的,如上所示,则必须重写/优化查询以获得所需的结果。