我有3个查询共享同一个表,查询1如下:
SELECT MAX(TranDate) AS MaxDate
FROM tblTransaction
WHERE AccNo = 12345
在获得帐户12345的最大交易日期后,我想使用最大日期在查询2中查找最大交易ID,如下所示:
SELECT MAX(TransactionID) AS MaxTran
FROM tblTransaction
WHERE AccNo = 12345
AND TranDate = 'pass max tran date here'
之后,我想将查询2中的最大事务ID传递给查询3,如下所示:
SELECT Commission
FROM tblTransaction
WHERE AccNo = 12345
AND TransactionID = 'pass max tranID here'
如何合并这3个查询?感谢
答案 0 :(得分:3)
假设tblTransaction
有TransactionID
作为主键,您可以使用更简单的 MySQL :
SELECT Commission
FROM tblTransaction
WHERE AccNo = 12345
ORDER BY TranDate DESC,
TransactionID DESC
LIMIT 1 ;
和 SQL-Server :
SELECT TOP (1) Commission
FROM tblTransaction
WHERE AccNo = 12345
ORDER BY TranDate DESC,
TransactionID DESC ;
答案 1 :(得分:2)
SELECT Commission
FROM tblTransaction
WHERE AccNo = 12345
AND TransactionID = (
SELECT MAX(TransactionID) AS MaxTran
FROM tblTransaction
WHERE AccNo = 12345
AND TranDate = (
SELECT MAX(TranDate) AS MaxDate
FROM tblTransaction
WHERE AccNo = 12345
)
)
答案 2 :(得分:0)
使用此查询:
SELECT Commission
FROM tblTransaction
WHERE AccNo = 12345
AND TransactionID = (
SELECT MAX(TransactionID) AS MaxTran
FROM tblTransaction
WHERE AccNo = 12345
AND TranDate = (
SELECT MAX(TranDate) AS MaxDate
FROM tblTransaction
WHERE AccNo = 12345
)
);
答案 3 :(得分:0)
就像这样:
SELECT Commission
FROM tblTransaction
WHERE AccNo = 12345
AND TransactionID = (
SELECT MAX(TransactionID) AS MaxTran
FROM tblTransaction
WHERE AccNo = 12345
AND TranDate = (
SELECT MAX(TranDate) AS MaxDate
FROM tblTransaction
WHERE AccNo = 12345
)
)
答案 4 :(得分:0)
请求表alise
检查以下查询:
SELECT commission
FROM tbltransaction
WHERE accno = 12345
AND TransactionID = (
SELECT MAX(tbl1.TransactionID) AS MaxTran
FROM tbltransaction AS tbl1
WHERE tbl1.accno = 12345
AND tbl1.trandate = (
SELECT MAX(tbl2.trandate) AS MaxDate
FROM tbltransaction AS tbl2
WHERE tbl2.accno = 12345
)
)
答案 5 :(得分:0)
就个人而言,我偏爱内部查询问题的WITH语法。
(这个解决方案适用于sql-server,我甚至不知道mysql是否支持这个)
;WITH
MaxDates AS
(
SELECT
tblTransaction.AccNo,
MAX(tblTransaction.TranDate) AS TranDate
FROM
tblTransaction
GROUP BY
tblTransaction.AccNo
),
MaxTransactionIDs AS
(
SELECT
tblTransaction.AccNo,
MAX(tblTransaction.TransactionID) AS TransactionID
FROM
tblTransaction
INNER JOIN MaxDates
ON MaxDates.AccNo = tblTransaction.AccNo
AND MaxDates.TranDate = tblTransaction.TranDate
GROUP BY
tblTransaction.AccNo, tblTransaction.TranDate
)
SELECT
tblTransaction.*
FROM
tblTransaction
INNER JOIN MaxTransactionIDs
ON MaxTransactionIDs.TransactionID = tblTransaction.TransactionID
WHERE
tblTransaction.AccNo = 12345
显然,这比上面的解决方案更冗长,但你可以在函数中放置这种查询(不包括WHERE)并根据需要更改条件。例如,您可以使用这样的函数来生成其最后交易具有佣金的所有用户的列表> 100。
答案 6 :(得分:0)
以下是您问题的不同方法
SELECT
t1.COMISSION,
t2.MaxTran,
t3.MaxDate
FROM tblTransaction t1
INNER JOIN (SELECT MAX(TransactionID) as MaxTran FROM tblTransaction) t2
ON t1.TransactionID = t2.MaxTran
INNER JOIN (SELECT MAX(TranDate) as MaxDate FROM tblTransaction) t3
ON t1.TranDate = t3.MaxDate
WHERE
1 = 1
AND t1.ACCNO = 12345
答案 7 :(得分:0)
SELECT t3.Commission
FROM tblTransaction t3
WHERE t3.AccNo = 12345
AND t3.TransactionID = (
SELECT MAX(t2.TransactionID)
FROM tblTransaction t2
WHERE t2.AccNo = 12345
AND t2.TranDate = (
SELECT MAX(t1.TranDate)
FROM tblTransaction t1
WHERE t1.AccNo = 12345
)
)