在第二次调用我的函数后,我得到以下错误:用作表达式的子查询返回多行

时间:2017-03-23 16:29:16

标签: python database psycopg2 psql

这是我的playerStandings()函数的定义:

def playerStandings():
    """Returns a list of the players and their win records, sorted by wins.

    The first entry in the list should be the player in first place, or a player
    tied for first place if there is currently a tie.

    Returns:
      A list of tuples, each of which contains (id, name, wins, matches):
        id: the player's unique id (assigned by the database)
        name: the player's full name (as registered)
        wins: the number of matches the player has won
        matches: the number of matches the player has played
    """
    db = connect()
    c = db.cursor()
    c.execute("select players.id,players.name, count((select matches.winner from matches where matches.winner = players.id))as win, count((matches.winner))as matches from players left join matches on players.id = matches.winner or players.id = matches.loser group by players.id order by win desc,id")
    standing = c.fetchall()
    db.close()
    return standing

第一次调用此函数时,程序运行正常,问题是当我尝试再次打印我的排名时。以下是我的主要功能。

standings = playerStandings()
print playerStandings()
[id1,id2,id3,id4,id5,id6] = [row[0] for row in standings]
reportMatch(id2,id1)
reportMatch(id4,id3)
reportMatch(id5,id6)
print playerStandings()
print swissPairings()
reportMatch(id2,id4)
reportMatch(id1,id5)
reportMatch(id6,id3)
print playerStandings()
print swissPairings()

1 个答案:

答案 0 :(得分:0)

请勿在{{1​​}}内放置SELECT。使用COUNT()计算符合其他条件的行。

SUM()