我的 python
文件中有当前的 sqlite3
代码:
Data = Cursor.execute("""
SELECT Username, Password
FROM PatientTable
WHERE Username = '{}'
""".format(Username))
Data = Data.fetchall()
数据库中有多个表:PatientTable
、DoctorTable
、ManagerTable
。每个属性分别在第二列和第三列中具有用户名
和密码
属性。
问:我当前的代码仅从 PatientTable
选择数据,但我需要从所有三个表中选择数据并识别每个项目在哪个表中数据来自。
现在,我想我可以使用多个 SQL 语句来完成此操作,但这似乎有些过分了。我考虑过使用 JOIN
但没有外键 - 将数据库相互关联的键。
提前致谢。
最佳答案
您似乎正在寻找 UNION。您可以为每个查询添加固定列,以区分结果集中的每条记录来自哪个表:
SELECT 'PatientTable' source_table, Username, Password
FROM PatientTable
WHERE Username = '{}'
UNION ALL SELECT 'DoctorTable' , Username, Password
FROM DoctorTable
WHERE Username = '{}'
UNION ALL SELECT 'ManagerTable', Username, Password
FROM ManagerTable
WHERE Username = '{}'
关于python - 从多个没有外键的表中选择数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54840116/