Unexpected data returned by PyMySQL when using GROUP_CONCAT

Viewed 85

I generated SQL query for my database as following:

SELECT match_id, Group_concat(name SEPARATOR ', ') AS 'winners' 
FROM   players 
WHERE  match_id = 4 
       AND rank = 1 
GROUP  BY match_id;

(for structure and example data, see sql fiddle)

which results in MySQL:

+----------+------------+
| match_id | winners    |
+----------+------------+
|        4 | P106, P107 |
+----------+------------+

So I tried to apply this SQL query into python using pymysql module:

conn = pymysql.connect(**DB_CONFIG, cursorclass=pymysql.cursors.DictCursor)
cur = conn.cursor()

sql = """SELECT match_id, Group_concat(name SEPARATOR ', ') AS 'winners' 
         FROM   players 
         WHERE  match_id = %s
                AND rank = 1 
         GROUP  BY match_id """
cur.execute(sql, (4, ))
result = cur.fetchall()

But strangely, the value of result was:

[{'match_id': 4, 'winners': 'P106'}]

So, why is the result of pymysql different from the result of MySQL Client? How can I fix it?

0 Answers
Related