使用 Joins 获取特定的数据集

Using Joins to fetch specific sets of data

我正在尝试使用从 3 个不同的 MySQL 数据库表接收到的数据打印试算表。

结构如下:

[PostGL]:AccountLink、借记和贷记

[GLAccountTypes]:账户类型,描述

[帐户]:帐户链接、帐户类型、帐号

我正在尝试使用以下 SELECT 查询来获取数据:

Views.py:

def Kyletrb(request):
    trialBalance =  'SELECT  [dbo].[PostGL].[Debit] , [dbo].[PostGL].[Credit], [dbo].[Accounts].[Master_Sub_Account] ,[dbo].[_etblGLAccountTypes].[cAccountTypeDescription] FROM [Kyle] INNER JOIN [Kyle].[dbo].[_etblGLAccountTypes] ON [Kyle].[dbo].[PostGL].[AccountLink] = SELECT [Kyle].[dbo].[Accounts].[AccountLink] INNER JOIN [Kyle].[dbo].[PostGL] ON [Kyle].[dbo].[Accounts].[iAccountType] =   [dbo].[_etblGLAccountTypes].[idGLAccountType] '

    cursor.execute(trialBalance);
    trialBalanceFinal = [tup[0] for tup in cursor.fetchall()]

    return render(request , 'main/Kyletrb.html' , {'trialBalance' : trialBalanceFinal} )

错误信息:

NameError at /Kyletrb name 'cursor' is not defined Request Method: GET Request URL: http://localhost:8000/Kyletrb Django Version: 3.2 Exception Type: NameError Exception Value:
name 'cursor' is not defined Exception Location: C:\Users\KylePOG\Documents\GMA Programming\accConnect\main\views.py, line 26, in Kyletrb Python Executable: C:\Users\KylePOG\AppData\Local\Programs\Python\Python39\python.exe Python Version: 3.9.4 Python Path:
['C:\Users\KylePOG\Documents\GMA Programming\accConnect', 'C:\Users\KylePOG\AppData\Local\Programs\Python\Python39\python39.zip', 'C:\Users\KylePOG\AppData\Local\Programs\Python\Python39\DLLs', 'C:\Users\KylePOG\AppData\Local\Programs\Python\Python39\lib', 'C:\Users\KylePOG\AppData\Local\Programs\Python\Python39', 'C:\Users\KylePOG\AppData\Local\Programs\Python\Python39\lib\site-packages'] Server time: Tue, 31 Aug 2021 11:00:05 +0000

我期望的输出是:

是否可以在 SQL 查询中使用 JOIN 函数来获取 accountLink 和 accountTypes Match

您可以像这样简单地连接所有三个表:

SELECT t1.AccountNumber, t2.Description, t3.Credit, t3.Debit
FROM Accounts t1 
   INNER JOIN GLAccountTypes t2
      ON t1.AccountType = t2.AccountType
   INNER JOIN PostGL t3
      ON t1.AccountLink = t3.AccountLink