Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL如何合并3行并计算结果?_Mysql - Fatal编程技术网

MySQL如何合并3行并计算结果?

MySQL如何合并3行并计算结果?,mysql,Mysql,我现在正在写一个小剧本,从今天早上开始我就被卡住了 以下是我的字段: id|player1 | player2 | player3 示例行: 1 | toto |-|- 2 |塔塔|托托|- 3|titi|tutu |- 4 |塔塔|提提|托托 我希望能够合并行player1、player2、player3并计算条目的数量,根据我给出的示例行,得到一个如下的顶部: 托托3 塔塔2 图图2 提提1 我通常都在写基本的查询,所以对于那一个,我真的被卡住了,尝试了好几个小时,无法得到我想要的。。有什

我现在正在写一个小剧本,从今天早上开始我就被卡住了

以下是我的字段:

id|player1 | player2 | player3

示例行:

1 | toto |-|-
2 |塔塔|托托|-
3|titi|tutu |-
4 |塔塔|提提|托托

我希望能够合并行player1、player2、player3并计算条目的数量,根据我给出的示例行,得到一个如下的顶部: 托托3 塔塔2 图图2 提提1

我通常都在写基本的查询,所以对于那一个,我真的被卡住了,尝试了好几个小时,无法得到我想要的。。有什么想法吗

select count(*) from ((select id,player1 as players from tablename) union (select id,player2 as players from tablename) union (select id,player3 as players from tablename)) as dummytable group by players

我还没有测试过,但看看逻辑:)

它读起来好像您正在开始做一些事情,而且您的数据库设计已经非常不规范,这可能会导致性能问题。为了减少这种情况,我建议在你进一步行动之前考虑重组。玩家1、玩家2和玩家3都是玩家,因此可以正常化。现有表格的每一行都可以被视为
游戏
或类似游戏,因此可以正常化。然后,您将看到将玩家链接到游戏的任务。这样,您就不会多次存储相同的播放器详细信息,从长远来看,所有针对表的查询都会更快,即使初始插入稍微复杂一些

players (
    id INT UNSIGNED PK AI,
    name VARCHAR
    .... other player data ....
)

// Assumed a name here
games (
    id INT UNSIGNED PK AI
    .... game data ....
)

// Note the 2-field primary key here.  Prevents 1 player being in the same game twice.
playersInGames (
    game_id INT UNSIGNED PK,
    player_id INT UNSIGNED PK,
    playerNumber TINYINT UNSIGNED  // You don't even need this, if you don't care about player number assigments just don't include this column
)
示例数据

// players
id      name
1       toto
2       tata
3       titi
4       tutu

// games
id
1
2
3
4

// playersInGames
game_id     player_id       playerNumber
1           1               1
2           2               1
2           1               2
3           3               1
3           4               2
4           2               1
4           3               2
4           1               3
这将反过来允许您执行简单的查询,例如

SELECT 
    p.*,
    COUNT(DISTINCT pig.game_id) AS gamesPlayerIsIn
FROM players AS p
LEFT JOIN playersInGames AS pig
ON pig.player_id = p.id
GROUP BY p.id
编辑如果您真的不想更改表架构,以下内容就足够了,但会让维护代码的人哭笑不得。它将计算每个玩家最多一次表行

SELECT
    players.playerName,
    COUNT(DISTINCT players.id) AS numberOfRows
FROM (
    (
        SELECT DISTINCT id, player1 AS playerName FROM tableName
    ) UNION DISTINCT (
        SELECT DISTINCT id, player2 AS playerName FROM tableName
    ) UNION DISTINCT (
        SELECT DISTINCT id, player3 AS playerName FROM tableName
    )
) AS players

你有列出所有玩家的表格吗?你是说
我想合并player1…
?@penartur:没有,没有列表,所有东西都在表格里。@莫斯蒂·莫斯塔科:我需要的是得到一份所有球员的名单,无论是在行球员1、球员2还是球员3,并计算他们被列出的次数,以获得一份拥有最多记录的球员名单。