psycopg2 PSQL SELECT语句返回0L而不是0

时间:2016-05-16 07:19:15

标签: python-2.7 psycopg2 psql

我的SELECT语句出现意外结果。

我有以下代码:

db_cursor.execute("""SELECT player.player_id, player.player_name, 
                       COUNT(match.winner_id) as wins, COUNT(match.loser_id + match.winner_id) as total_matches
                       FROM player LEFT JOIN match ON player.player_id = match.winner_id
                       GROUP BY player.player_id
                       ORDER BY wins ASC""")
results = db_cursor.fetchall()
print results

打印时我得到以下输出。

[(317, 'Bruno Walton', 0L, 0L), (318, "Boots O'Neal", 0L, 0L), (319, 'Cathy Burton', 0L, 0L), (320, 'Diane Grant', 0L, 0L)]
[(318, "Boots O'Neal", 0L, 0L), (320, 'Diane Grant', 0L, 0L), (317, 'Bruno Walton', 1L, 1L), (319, 'Cathy Burton', 1L, 1L)]

什么是0L和1L?我认为'L'仅适用于非常大的数字,而不是一个数字。

对不起,我是初学者,所以它可能非常明显,但任何帮助都会受到赞赏。如果它有帮助,这是表的psql:

CREATE TABLE IF NOT EXISTS player
(player_id SERIAL PRIMARY KEY, player_name TEXT);

CREATE TABLE IF NOT EXISTS match
(match_id SERIAL PRIMARY KEY, winner_id INTEGER references player(player_id),
 loser_id INTEGER references player(player_id));

1 个答案:

答案 0 :(得分:1)

您可以向查询添加强制转换操作符,即COUNT(match.winner_id):: int和COUNT(match.loser_id + match.winner_id):: int。否则,您必须在结果集中强制转换列。

db_cursor.execute("""SELECT player.player_id, player.player_name, 
                       COUNT(match.winner_id)::int as wins, COUNT(match.loser_id + match.winner_id)::int as total_matches
                       FROM player LEFT JOIN match ON player.player_id = match.winner_id
                       GROUP BY player.player_id
                       ORDER BY wins ASC""")