SQL - 在一个表的同一列中选择与上面不相似的行

时间:2015-06-28 20:45:45

标签: mysql sql

当我尝试使用 mysql 命令从数据库中获取结果时,我得到了以下结果:

+----+------+---------------------+ | id | full_name | club_name | +----+------+---------------------+ | 1 | Ahmed Sayed | El Ahly | +----+------+---------------------+ | 2 | Kareem Gaber | El Ahly | +----+------+---------------------+ | 3 | Wael Gamal | ENPPI | +----+------+---------------------+ | 4 | Mohab Saeed | Petrojet | +----+------+---------------------+ | 5 | Kamal saber | Cocorico | +----+------+---------------------+ | 6 | Mohamed mezo | Ismaily | +----+------+---------------------+ | 7 | Mohamed gad | Ismaily | +----+------+---------------------+ | 8 | moaz maged | Smouha | +----+------+---------------------+

类似的东西:

+----+------+---------------------+
| 1  | Ahmed Sayed  |   El Ahly   |
+----+------+---------------------+
| 2  | Kareem Gaber |   El Ahly   |
+----+------+---------------------+

但我有很多俱乐部名称类似于上面的

+----+------+---------------------+
| 6  | Mohamed mezo |   Ismaily   |
+----+------+---------------------+
| 7  | Mohamed gad  |   Ismaily   |
+----+------+---------------------+

ORDER BY RAND(club_name)

我尝试使用+----+------+---------------------+ | id | full_name | club_name | +----+------+---------------------+ | 1 | Ahmed Sayed | El Ahly | +----+------+---------------------+ | 2 | Kareem Gaber | Petrojet | +----+------+---------------------+ | 3 | Wael Gamal | ENPPI | +----+------+---------------------+ | 4 | Mohab Saeed | El Ahly | +----+------+---------------------+ | 5 | Kamal saber | Cocorico | +----+------+---------------------+ | 6 | Mohamed mezo | Ismaily | +----+------+---------------------+ | 7 | Mohamed gad | Ismaily | +----+------+---------------------+ | 8 | moaz maged | Smouha | +----+------+---------------------+ ,它给了我一个类似的结果

实施例

+----+------+---------------------+
| id |   full_name  |  club_name  |
+----+------+---------------------+
| 1  | Ahmed Sayed  |   El Ahly   |
+----+------+---------------------+
| 2  | Kareem Gaber |   Petrojet  |
+----+------+---------------------+
| 3  | Wael Gamal   |   ENPPI     |
+----+------+---------------------+
| 4  | Mohab Saeed  |   El Ahly   |
+----+------+---------------------+
| 5  | Kamal saber  |   Cocorico  |
+----+------+---------------------+
| 6  | Mohamed mezo |   Ismaily   |
+----+------+---------------------+
| 7  | Mohamed gad  |   Cocorico  |
+----+------+---------------------+
| 8  | moaz maged   |   Smouha    |
+----+------+---------------------+

所需的输出是:

callback()

可以 mysql 这样做,还是应该将 php mysql 集成? 任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

如果我理解正确,您不希望具有相同俱乐部名称的玩家在结果集中一个接一个地出现。

您可能需要的查询如下:

SELECT p2.id, p2.full_name, p2.club_name
FROM (
    SELECT 
        id,
        @row_number := CASE
            WHEN @clubName = club_name THEN @row_number + 1
            ELSE 1
        END AS sort_num,
        full_name,
        @clubName := club_name as club_name
    FROM
        players p1
    ORDER BY p1.club_name
) AS p2 ORDER BY p2.sort_num, p2.id;

让我试着解释一下。您需要按某些列对行进行排序,该列将包含每个非唯一俱乐部名称的唯一编号。 Oracle DB中有一个名为ROW_NUMBER的非常好的功能。不幸的是,MySQL没有它。所以我们正在效仿它。

子查询

SELECT 
    id,
    @row_number := CASE
        WHEN @clubName = club_name THEN @row_number + 1
        ELSE 1
    END AS sort_num,
    full_name,
    @clubName := club_name as club_name
FROM
    players p1
ORDER BY p1.club_name;

将产生以下结果:

+----+----------+--------------+-----------+
| id | sort_num | full_name    | club_name |
+----+----------+--------------+-----------+
|  5 |        1 | Kamal saber  | Cocorico  |
|  1 |        1 | Ahmed Sayed  | El Ahly   |
|  2 |        2 | Kareem Gaber | El Ahly   |
|  3 |        1 | Wael Gamal   | ENPPI     |
|  6 |        1 | Mohamed mezo | Ismaily   |
|  7 |        2 | Mohamed gad  | Ismaily   |
|  4 |        1 | Mohab Saeed  | Petrojet  |
|  8 |        1 | moaz maged   | Smouha    |
+----+----------+--------------+-----------+

注意sort_num列。对于俱乐部名称为“El Ahly”的行,它有两个unqiue值1和2.现在我们所做的就是按此列排序。

最终结果:

+----+--------------+-----------+
| id | full_name    | club_name |
+----+--------------+-----------+
|  1 | Ahmed Sayed  | El Ahly   |
|  3 | Wael Gamal   | ENPPI     |
|  4 | Mohab Saeed  | Petrojet  |
|  5 | Kamal saber  | Cocorico  |
|  6 | Mohamed mezo | Ismaily   |
|  8 | moaz maged   | Smouha    |
|  2 | Kareem Gaber | El Ahly   |
|  7 | Mohamed gad  | Ismaily   |
+----+--------------+-----------+

答案 1 :(得分:0)

如果我理解正确,你想从订购的俱乐部中选择,所以没有两个相邻的行有相同的俱乐部。

近似是交织俱乐部,你可以用子查询和变量来做:

select p.*
from (select p.*,
             (@rn := if(@c = club, @rn + 1,
                        if(@c := club, 1, 1)
                       )
             ) as rn
      from players p cross join
           (select @c := '', @rn := 0) params
      order by club
     ) p
order by rn, club;

在某些情况下,这不起作用。但是如果你的俱乐部有相同数量的成员,那就应该没问题。