我正在进行MySQL连接,我只想获取LMSCDonationLetterNeeded值为1的记录。然而,发生的事情是我得到的是7行而不是3行。此外,执行查询时当不准确时,调用LMSCDonationLetterNeeded的列的值都为1。
我有3个表格如下:
Registristration
-------------------
SwimmerID RegNumber TransactionID LMSCDonationLetterNeeded RegistrationDate
HYDR0 3881-HYDR0 0123-0001 0 11/25/2015
HYDR0 3882-HYDR0 0123-0048 1 04/15/2018
97SSN 3880-97SSN 0124-0022 0 01/01/2016
97SSN 3881-97SSN 0124-0068 0 03/20/2017
97SSN 3882-97SSN 0124-0084 1 04/02/2018
06HM5 388K-06HM5 0126-0011 0 02/02/2015
06HM5 388J-06HM5 0126-0056 1 03/02/2018
People
-------------------
SwimmerID FirstName LastName
HYDR0 John Smith
97SSN Jim Johnson
06HM5 Susan Korver
FinTransactions
--------------------
SwimmerID RegistrationNum TransactionID LMSCDonateAmt FinTransactions
HYDR0 3881-HYDR0 0123-0001 10.00 11/25/2015
HYDR0 3882-HYDR0 0123-0048 15.00 04/15/2018
97SSN 3880-97SSN 0124-0022 05.00 01/01/2016
97SSN 3881-97SSN 0124-0068 25.00 03/20/2017
97SSN 3881-97SSN 0124-0084 10.00 04/02/2018
06HM5 388K-06HM5 0126-0011 05.00 02/02/2015
06HM5 388J-06HM5 0126-0056 35.00 03/02/2018
我的返回太多行的查询如下:
SELECT Registration.LMSCDonationLetterNeeded,
FinTransactions.LMSCDonateAmt,
Registration.RegNumber,
Registration.SwimmerID,
People.FirstName,
People.MI,
People.LastName,
People.Suffix,
People.Address1,
People.City,
People.StateAbbr,
People.Zip,
People.Country AS CountryCode,
Countries.Country,
DATE_FORMAT(FinTransactions.FinTrxDateTime, '%m/%d/%Y') AS DonationDate,
LMSCs.Name AS LMSCName,
Registration.LMSCID,
LMSCOfficers.FirstName AS RegistrarFirstName,
LMSCOfficers.LastName AS RegistrarLastName,
Aliases.EMailAlias AS RegistrarEMail
FROM
Registration
LEFT JOIN People USING (SwimmerID)
LEFT JOIN FinTransactions ON FinTransactions.SwimmerID = Registration.SwimmerID
LEFT JOIN LMSCs ON LMSCs.LMSCID = Registration.LMSCID
LEFT JOIN LMSCOfficers ON LMSCOfficers.LMSCID = Registration.LMSCID AND LMSCOfficers.OfficeID = 5
LEFT JOIN Aliases ON LMSCOfficers.AliasID = Aliases.AliasID
LEFT JOIN Countries ON People.Country = Countries.CountryCode
WHERE
Registration.LMSCDonationLetterNeeded = 1
AND Registration.LMSCID = 38
AND Registration.RegNumber IN ("3881-HYDR0", "3880-97SSN", "388K-06HM5")
ORDER BY
People.LastName,
People.FirstName,
People.MI
对此的任何帮助都将非常感激。
答案 0 :(得分:0)
试试这个解决方案:
SELECT Registration.LMSCDonationLetterNeeded,
FinTransactions.LMSCDonateAmt,
Registration.RegNumber,
Registration.SwimmerID,
People.FirstName,
People.MI,
People.LastName,
People.Suffix,
People.Address1,
People.City,
People.StateAbbr,
People.Zip,
People.Country AS CountryCode,
Countries.Country,
DATE_FORMAT(FinTransactions.FinTrxDateTime, '%m/%d/%Y') AS DonationDate,
LMSCs.Name AS LMSCName,
Registration.LMSCID,
LMSCOfficers.FirstName AS RegistrarFirstName,
LMSCOfficers.LastName AS RegistrarLastName,
Aliases.EMailAlias AS RegistrarEMail
FROM
Registration
LEFT JOIN People
USING (SwimmerID)
LEFT JOIN FinTransactions
ON FinTransactions.SwimmerID = Registration.SwimmerID
AND FinTransactions.RegistrationNum = Registration.RegNumber -- Added this condition
LEFT JOIN LMSCs
ON LMSCs.LMSCID = Registration.LMSCID
LEFT JOIN LMSCOfficers
ON LMSCOfficers.LMSCID = Registration.LMSCID
AND LMSCOfficers.OfficeID = 5
LEFT JOIN Aliases
ON LMSCOfficers.AliasID = Aliases.AliasID
LEFT JOIN Countries
ON People.Country = Countries.CountryCode
WHERE Registration.LMSCDonationLetterNeeded = 1
AND Registration.LMSCID = 38
AND Registration.RegNumber IN ("3881-HYDR0", "3880-97SSN", "388K-06HM5")
ORDER BY People.LastName,
People.FirstName,
People.MI
说明:
您在Registristration
条件FinTransactions
条件中加入表格ON
和FinTransactions.RegistrationNum = Registration.RegNumber
时未提及一个条件。