php / SQL - 在许多条件下每4个名字之间打印4次

时间:2015-07-22 21:02:37

标签: php mysql

我之前在此主题中已经提出过这个问题,请仔细阅读sql-print-many-word-between-every-columns-with-many-conditions

但是这个问题和之前的问题之间存在差异,我希望在很多俱乐部名称中成为一个集体球员或一组球员,例如:

El Ahly俱乐部名称有一个12玩家我想将其分成3 teamsteam4玩家我想要的计算每个俱乐部名称中的team个数 然后将它们放在不可变数字(组)4 8 16 32 64 128 256内,基于它们之间的teams打印**ANY WORD**的数量,在使用select full_name,club_name from players where y_of_birth=2000过滤数据后,所需的输出应该像那个:

更新:

+----+------+---------------------+
|   full_name  |  club_name       |
+--------------+------------------+
| Ahmed Sayed  |   El Ahly        |
+--------------+------------------+
| Kareem Gaber |   El Ahly        |
+--------------+------------------+
| Maher Zein   |   El Ahly        |
+--------------+------------------+
| Mohab Saeed  |   El Ahly        |
+----+------+--+------------------+
| ahmed shawky |   Petroget       |
+--------------+------------------+
| shaker ali   |   Petroget       |
+--------------+------------------+
| omar Saed    |   Petroget       |
+--------------+------------------+
| moataz Gamal |   Petroget       |
+------+-------+------------------+
| **ANY WORD** |                  |
+--------------+------------------+
| **ANY WORD** |                  |
+--------------+------------------+
| **ANY WORD** |                  |
+--------------+------------------+
| **ANY WORD** |                  |
+--------------+------------------+
| husein mazen |   Cocorico       |
+--------------+------------------+
| hassan maged |   Cocorico       |
+--------------+------------------+
| maged ahmoud |   Cocorico       |
+--------------+------------------+
| zakareya gad |   Cocorico       |
+--------------+------------------+
| Kamal saber  |   El Ahly        |
+--------------+------------------+
| gamel kamel  |   El Ahly        |
+--------------+------------------+
| omar galal   |   El Ahly        |
+--------------+------------------+
| Kamal saber  |   El Ahly        |
+--------------+------------------+
| **ANY WORD** |                  |
+--------------+------------------+
| **ANY WORD** |                  |
+--------------+------------------+
| **ANY WORD** |                  |
+--------------+------------------+
| **ANY WORD** |                  |
+--------------+------------------+
| khaled zein  |   Petroget       |
+--------------+------------------+
| shady sameer |   Petroget       |
+--------------+------------------+
| wahed mahmoud|   Petroget       |
+--------------+------------------+
| samy shahen  |   Petroget       |
+----+------+--+------------------+
| Mohamed gad  |   El Ahly        |
+----+------+--+------------------+
| ehab zeyad   |   El Ahly        |
+----+---------+------------------+
| moaz maged   |   El Ahly        |
+--------------+------------------+
| mazen mahmod |   El Ahly        |
+--------------+------------------+

在此示例中有2 groups of **ANY WORD**,因为它是6 group并将它们放在8 4中的8不可变组内或163264128256**ANY WORD**号码...且{2} club_name之间不相邻或select full_name,club_name from players where y_of_birth=2000

之间相邻的两个

示例:

4名球员作为一名球员

如果12之后的结果为12 players玩家,则表示3 groups = 4,因为每个群组都有**ANY WORD**个玩家下一步打印**ANY WORD**在3组之间进行4次(注意:+----+------+---------------------+ | full_name | club_name | +--------------+------------------+ | Ahmed Sayed | El Ahly | +--------------+------------------+ | Kareem Gaber | El Ahly | +--------------+------------------+ | Maher Zein | El Ahly | +--------------+------------------+ | Mohab Saeed | El Ahly | +----+------+--+------------------+ | ahmed shawky | Petroget | +--------------+------------------+ | shaker ali | Petroget | +--------------+------------------+ | omar Saed | Petroget | +--------------+------------------+ | moataz Gamal | Petroget | +------+-------+------------------+ | **ANY WORD** | | +--------------+------------------+ | **ANY WORD** | | +--------------+------------------+ | **ANY WORD** | | +--------------+------------------+ | **ANY WORD** | | +--------------+------------------+ | Kamal saber | El Ahly | +--------------+------------------+ | gamel kamel | El Ahly | +--------------+------------------+ | omar galal | El Ahly | +--------------+------------------+ | Kamal saber | El Ahly | +--------------+------------------+ 是这些组中的一组),因此输出将是这样的:

4 8 16 32 64 128 256

结论:

group 4是不可变组

16应该填充**ANY WORD** +----+------+---------------------+ | full_name | club_name | +--------------+------------------+ | Ahmed Sayed | El Ahly | +--------------+------------------+ | Kareem Gaber | El Ahly | +--------------+------------------+ | Maher Zein | El Ahly | +--------------+------------------+ | Mohab Saeed | El Ahly | +----+------+--+------------------+ | ahmed shawky | Petroget | +--------------+------------------+ | shaker ali | Petroget | +--------------+------------------+ | omar Saed | Petroget | +--------------+------------------+ | moataz Gamal | Petroget | +------+-------+------------------+ | **ANY WORD** | | +--------------+------------------+ | **ANY WORD** | | +--------------+------------------+ | **ANY WORD** | | +--------------+------------------+ | **ANY WORD** | | +--------------+------------------+ | Kamal saber | El Ahly | +--------------+------------------+ | gamel kamel | El Ahly | +--------------+------------------+ | omar galal | El Ahly | +--------------+------------------+ | Kamal saber | El Ahly | +--------------+------------------+ 玩家group 8之间没有相邻的玩家:

32

**ANY WORD**应该填充+----+------+---------------------+ | full_name | club_name | +--------------+------------------+ | Ahmed Sayed | El Ahly | +--------------+------------------+ | Kareem Gaber | El Ahly | +--------------+------------------+ | Maher Zein | El Ahly | +--------------+------------------+ | Mohab Saeed | El Ahly | +----+------+--+------------------+ | ahmed shawky | Petroget | +--------------+------------------+ | shaker ali | Petroget | +--------------+------------------+ | omar Saed | Petroget | +--------------+------------------+ | moataz Gamal | Petroget | +------+-------+------------------+ | **ANY WORD** | | +--------------+------------------+ | **ANY WORD** | | +--------------+------------------+ | **ANY WORD** | | +--------------+------------------+ | **ANY WORD** | | +--------------+------------------+ | husein mazen | Cocorico | +--------------+------------------+ | hassan maged | Cocorico | +--------------+------------------+ | maged ahmoud | Cocorico | +--------------+------------------+ | zakareya gad | Cocorico | +--------------+------------------+ | Kamal saber | El Ahly | +--------------+------------------+ | gamel kamel | El Ahly | +--------------+------------------+ | omar galal | El Ahly | +--------------+------------------+ | Kamal saber | El Ahly | +--------------+------------------+ | **ANY WORD** | | +--------------+------------------+ | **ANY WORD** | | +--------------+------------------+ | **ANY WORD** | | +--------------+------------------+ | **ANY WORD** | | +--------------+------------------+ | khaled zein | Petroget | +--------------+------------------+ | shady sameer | Petroget | +--------------+------------------+ | wahed mahmoud| Petroget | +--------------+------------------+ | samy shahen | Petroget | +----+------+--+------------------+ | Mohamed gad | El Ahly | +----+------+--+------------------+ | ehab zeyad | El Ahly | +----+---------+------------------+ | moaz maged | El Ahly | +--------------+------------------+ | mazen mahmod | El Ahly | +--------------+------------------+ group 16玩家group 32之间没有相邻的玩家:

group 64

以及

group 128 group 256 select p.* from (select p.*, (@rn := if(@c = club_name, @rn + 1, if(@c := club_name, 1, 1) ) ) as seqnum from players p cross join (select @c := '', @rn := 0) params order by club_name ) p order by floor((seqnum - 1) / 4), club_name select p.* from (select p.full_name,club_name, (@rn := if(@c = club_name, @rn + 1, if(@c := club_name, 1, 1) ) ) as seqnum from players p cross join (select @c := '', @rn := 0) params WHERE `y_of_birth` LIKE N'%2000%' order by club_name ) p order by floor((seqnum - 1) / 4), club_name +----+------+---------------------+ | full_name | club_name | +--------------+------------------+ | Ahmed Sayed | El Ahly | +--------------+------------------+ | Kareem Gaber | El Ahly | +--------------+------------------+ | Maher Zein | El Ahly | +--------------+------------------+ | Mohab Saeed | El Ahly | +----+------+--+------------------+ | kamal mado | Cocorico | +--------------+------------------+ | hossam magdy | Cocorico | +--------------+------------------+ | sayed ammar | Cocorico | +--------------+------------------+ | karem gad | Cocorico | +--------------+------------------+ | Mohamed gad | El Ahly | +----+------+--+------------------+ | ehab zeyad | El Ahly | +----+---------+------------------+ | moaz maged | El Ahly | +--------------+------------------+ | mazen mahmod | El Ahly | +--------------+------------------+ | shaker ali | Petroget | +--------------+------------------+ | omar Saed | Petroget | +--------------+------------------+ | moataz Gamal | Petroget | +--------------+------------------+ | gamael zayed | Petroget | +--------------+------------------+ | husein mazen | Cocorico | +--------------+------------------+ | mazen kamal | Cocorico | +--------------+------------------+ | maged ahmoud | Cocorico | +--------------+------------------+ | mesho kareem | Cocorico | +--------------+------------------+ | mano mazen | Petroget | +--------------+------------------+ | shady srour | Petroget | +--------------+------------------+ | mamdouh gamal| Petroget | +--------------+------------------+ | khaled zein | Petroget | +--------------+------------------+ | husein mazen | Cocorico | +--------------+------------------+ | hassan maged | Cocorico | +--------------+------------------+ | maged ahmoud | Cocorico | +--------------+------------------+ | zakareya gad | Cocorico | +--------------+------------------+ | shady sameer | Petroget | +--------------+------------------+ | wahed mahmoud| Petroget | +--------------+------------------+ | samy shahen | Petroget | +----+------+--+------------------+ | abdo samy | Petroget | +----+------+--+------------------+

已编辑:

我从 Gordon Linoff

获得此查询
[HttpGet] public ActionResult AnAction(string param1, int param2) {}

结合数据过滤后:

$(function() {
    $("#changer").on("click",function(e){
        e.preventDefault();
        var body = $("#section2");
        if(body.css("background-color").indexOf("rgb(0, 128, 0)") == -1 ) {                             
            body.css("background-color","rgb(0, 128, 0)");
        }else{
          body.css("background-color","rgb(255, 255, 255)");
        }
    });
})

输出类似的内容:

{{1}}

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:2)

我读了你之前的帖子和这篇文章。如果我明白了,最基本的事情就是你有团队,你有玩家,你想要在某些标准化的分组中显示数据(可以这么说)。

嗯,有一个恒定的数据,你总是在比较团队的规模。

我要做的是创建一个子查询,将团队与玩家数量分组并以某种方式使用它,如下所示:

select t.*
       , x.total_players
       -- , row_number() over(order by t.team_name) row_id
from team_table t
    join (
          select team_id
                , count(1) total_playerss
          from team_table 
          group by team_id
         ) x on x.team_id = t.team_id
order by t.team_name

当回显PHP中的结果集时,您可以构建一些函数,例如:

<?php

function add_some_word($playerCount) {

if ($playerCount >4 && $playerCount <= 8) {
echo '*** SOME WORD***';
echo '*** SOME WORD***';
echo '*** SOME WORD***';
}
else if ($playerCount >4 && $playerCount <= 8) {
echo '*** SOME WORD***';
echo '*** SOME WORD***';
}
// etc.
}

?>

使用结果集中的row_id,您可以看到团队的开始和结束时间。

因此,当它吐出行时,它可能是:“

<?php

$currentTeamName = "";

while ($row = pg_fetch_array($result,null,PGSQL_ASSOC)) {
// echo data you would normally send out

if ($currentTeamName != $row['team_name']) {
add_some_word($row['total_players']);
}

}

?>

我希望我的问题是正确的。我快速冲破了这个,但我认为这会有所帮助!