使用select和where更新表

时间:2019-04-03 01:01:30

标签: mysql sql select

我有两个表:

def GO(win,board):
    for x, o, b in win:
        if board[x] == board[o] == board[b]:
            print("Player {0} wins!\n".format(board[x]))
            print("Congratulations!\n")
            return True
    if 9 == sum((pos == 'X' or pos == 'O') for pos in board):
        print("The game ends in a tie\n")
        # return False if there is no winner 
        return False
def tic_tac_toe():
    board = [None] + list(range(1, 10))
    win = [(1, 2, 3),(4, 5, 6),(7, 8, 9),(1, 4, 7),(2, 5, 8),(3, 6, 9),(1, 5, 9),(3, 5, 7),]
    for player in 'XO' * 9:
        drawboard(board)
        if GO(win,board):
            # returns the winner if there is one    
            return player
        elif GO(win, board) is False:
            # returns False if winner is a tie     
            return False  
        print("Player {0}".format(player))
        board[t(board)] = player
        print()

我需要在表def main(): count_x = 0 count_o = 0 while True: score = tic_tac_toe() if score == 'X': count_x += 1 elif score == 'O': count_o += 1 print("The running score is " + '('+ str(count_x), str(count_y) +')') if input("Play again (y/n)\n") != "y": break main() 上创建列table ficha_atendimento int id string cidadaos_cns table cidadaos int id string cns ,我喜欢用cidadaos表的ID填充它,就像这样:

int cidadaos_id

但这不起作用...有帮助吗?

3 个答案:

答案 0 :(得分:2)

您需要执行两个单独的操作来实现。首先,将cidadao_id列添加到表中。然后,从UPDATE表中cidados其值:

ALTER TABLE ficha_atendimento ADD COLUMN cidadao_id INT NOT NULL;
UPDATE ficha_atendimento f
JOIN cidadaos c ON c.cns = f.cns
SET f.cidadao_id = c.id

您可能还希望在FOREIGN KEY值上添加cidadao_id

ALTER TABLE ficha_atendimento ADD FOREIGN KEY (cidadao_id) REFERENCES cidadaos(id)

答案 1 :(得分:1)

您似乎想要一个update

update ficha_atendimento fa join
       cidadaos c
       on c.cns = fa.cns
    set fa.cidadao_id = c.id ;

答案 2 :(得分:1)

原则是:

INSERT INTO table2 (column1, column2, column3, ...) SELECT column1, column2, column3, ... FROM table1 WHERE condition;

如果您按照规则放置,我认为您可以运行您的代码。而且,如果您有一些要演示的数据将对您有所帮助。