如何在varchar上使用GROUP BY?

时间:2019-07-24 10:52:18

标签: mysql mariadb

我试图按两个值分组,因为我想从数据库中删除重复项,但是以某种方式我的group by子句不想工作(字段在varchar中)。我还检查了是否有空间或更多,但没有。

该服务器是mariadb服务器,我使用一个简单的HeidiSQL客户端

SELECT Player, COUNT(Player)
FROM Players
GROUP BY Player

我希望它只是按分组,以便删除重复的值。

这是我目前得到的结果:

SQL query

与SELECT CONCAT相同: SQL query

1 个答案:

答案 0 :(得分:1)

我找到了答案,问题是在其他行上有​​换行符,所以mysql无法用我的SELECT找到它,我创建了一个bash脚本来修复它:

#!/bin/bash
export MYSQL_PWD="YOURPWD"


runSQL() {
        runSQLOutput=$(mysql -sN --user="YOURUSER" -h 127.0.0.1 -P 3306 --database="Factorio-Stats")
}

runSQL <<EOF
        SELECT Player
        FROM Players
EOF

mapfile -t Players < <(printf "%s\n" "$runSQLOutput")
        for Players in "${Players[@]}"
                do
                        test=`echo $Players | sed 's~[^[:alnum:]/]\+~~g'`
                        runSQL <<EOF
                                SELECT Id_Players
                                FROM Players
                                WHERE Player = "$Players"
EOF

                        runSQL <<EOF
                                UPDATE Players
                                SET Player="$test"
                                WHERE Id_Players="$runSQLOutput"
EOF
                        echo "updated $test"
done