gpt4 book ai didi

Python MySQL 查询多个join

转载 作者:太空宇宙 更新时间:2023-11-03 10:31:00 26 4
gpt4 key购买 nike

我想将几个表相互连接起来。数据库连接已建立,但出现此错误:

SyntaxError: EOL while scanning string literal

import mysql.connector
from mysql.connector import Error
import csv
import pandas as pd
import sqlite3
import numpy as np

cnx = mysql.connector.connect(user = "n.neshat",
passwd = "Start123",
db = "base",
host="192.168.126.29")
cursor = cnx.cursor()

test = ("select * from projects\
join positions\ on projects.id = positions.project_id\
join loads\ on positions.id = loads.position_id\
join equipments\ on positions.id = equipments.position_id\
join module_fields\ on positions.id = module_fields.position_id\
join configurations\ on positions.id = configurations.position_id\
join module_blocks\ on configurations.id = module_blocks.configuration_id")

cursor.execute(test)

myresult = cursor.fetchall()

最佳答案

为避免意外的 SQL 语法拼写错误,我建议使用 Python 的多行语法。而且,使用别名:

test = """SELECT * FROM projects pr
INNER JOIN positions po ON pr.id = po.project_id
INNER JOIN loads l ON po.id = l.position_id
INNER JOIN equipments e ON po.id = e.position_id
INNER JOIN module_fields mf ON po.id = mf.position_id
INNER JOIN configurations c ON po.id = c.position_id
INNER JOIN module_blocks mb ON c.id = mb.configuration_id"""

关于Python MySQL 查询多个join,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58554051/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com