0

我有 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 个查询?谢谢

4

8 回答 8

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 ;
于 2013-09-04T11:31:17.037 回答
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
            )
    )
于 2013-09-04T10:48:30.453 回答
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
于 2013-09-04T11:13:01.840 回答
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 的所有用户的列表。

于 2013-09-04T11:12:28.040 回答
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
            )
      );
于 2013-09-04T10:48:41.540 回答
0

请给桌子

检查以下查询:

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
            )
    )
于 2013-09-04T10:52:14.887 回答
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
            )
    )
于 2013-09-04T10:50:02.403 回答
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
            )
    )
于 2013-09-04T12:45:10.190 回答