多表的MySQL问题

时间:2010-11-27 04:31:48

标签: sql mysql

那里有新问题。在下面有MySQL数据库/表结构:

CREATE DATABASE `dbTest`

CREATE  TABLE IF NOT EXISTS `dbTest`.`tbUser` (

  `UserId` INT UNSIGNED NOT NULL AUTO_INCREMENT ,

  `Username` VARCHAR(45) NOT NULL ,

  PRIMARY KEY (`UserId`) )

ENGINE = InnoDB

CREATE  TABLE IF NOT EXISTS `dbTest`.`tbTransactionType` (

  `TypeId` INT UNSIGNED NOT NULL AUTO_INCREMENT ,

  `Name` VARCHAR(45) NOT NULL ,

  PRIMARY KEY (`TypeId`) )

ENGINE = InnoDB

CREATE  TABLE IF NOT EXISTS `dbTest`.`tbTransaction` (

  `TransactionId` INT NOT NULL AUTO_INCREMENT ,

  `UserId` INT UNSIGNED NOT NULL ,

  `TransactionType` INT UNSIGNED NOT NULL ,

  `Balance` DOUBLE NOT NULL ,

  `Date` DATETIME NOT NULL ,

  PRIMARY KEY (`TransactionId`) ,

  INDEX `FK_tbTransaction_tbUser_UserId` (`UserId` ASC) ,

  INDEX `FK_tbTransaction_tbTransactionType_TransactionId` (`TransactionType` ASC) ,

  CONSTRAINT `FK_tbTransaction_tbUser_UserId`

    FOREIGN KEY (`UserId` )

    REFERENCES `dbTest`.`tbUser` (`UserId` )

    ON DELETE NO ACTION

    ON UPDATE NO ACTION,

  CONSTRAINT `FK_tbTransaction_tbTransactionType_TransactionId`

    FOREIGN KEY (`TransactionType` )

    REFERENCES `dbTest`.`tbTransactionType` (`TypeId` )

    ON DELETE NO ACTION

    ON UPDATE NO ACTION)

ENGINE = InnoDB

INSERT INTO `dbTest`.`tbUser` (`UserId`, `Username`) VALUES ('1', 'User1');
INSERT INTO `dbTest`.`tbTransactionType` (`TypeId`, `Name`) VALUES ('1', 'Deposite');
INSERT INTO `dbTest`.`tbTransactionType` (`TypeId`, `Name`) VALUES ('2', 'Withdraw');

INSERT INTO `dbTest`.`tbTransaction` (`TransactionId`, `UserId`, `TransactionType`, `Balance`, `Date`) VALUES (NULL, '1', '1', '200', NOW())
INSERT INTO `dbTest`.`tbTransaction` (`TransactionId`, `UserId`, `TransactionType`, `Balance`, `Date`) VALUES (NULL, '1', '2', '100', NOW())
INSERT INTO `dbTest`.`tbTransaction` (`TransactionId`, `UserId`, `TransactionType`, `Balance`, `Date`) VALUES (NULL, '1', '1', '20', NOW())
INSERT INTO `dbTest`.`tbTransaction` (`TransactionId`, `UserId`, `TransactionType`, `Balance`, `Date`) VALUES (NULL, '1', '2', '10', NOW())

我想获得UserId 1的每种类型的最后一个条目

这是我的SQL查询:

SELECT U.Username, TT.Name, T.Balance, T.Date
FROM tbUser U
INNER JOIN tbTransaction T ON T.UserId = U.UserId
INNER JOIN tbTransactionType TT ON TT.TypeId = T.TransactionType
WHERE U.UserId = 1

结果是:

Username    Name        Balance Date
User1   Deposite    200     2010-11-26 23:11:40
User1   Deposite    20      2010-11-26 23:14:56
User1   Withdraw    100     2010-11-26 23:11:58
User1   Withdraw    10      2010-11-26 23:14:56

当我想得到类似的东西时:

Username    Name        Balance Date
User1       Deposite    20      2010-11-26 23:14:56
User1       Withdraw    10      2010-11-26 23:14:56

我确信解决方案并不那么难,但我现在无法理解......

我也试过GROUP BY TT.TypeId但没有成功。

谢谢!

3 个答案:

答案 0 :(得分:2)

我认为应该这样做,如果你添加更多的交易类型,它就不会成为问题。但是,我还没有完全测试过bc我没有完全具有“同构”(可能不是严格的正确术语)的一组表格,方便测试。即使你已经解决了,也请告诉我,我很好奇。

SELECT U.Username, TT.Name, T.Balance, T.Date
FROM tbUser U
INNER JOIN tbTransaction T ON T.UserId = U.UserId
INNER JOIN tbTransactionType TT ON TT.TypeId = T.TransactionType
WHERE U.UserId = 1 AND 
       T2.Date = (SELECT MAX(T2.date) 
                 FROM tbTransaction T2 WHERE 
                 T2.UserID = U.UserId, T2.TransactionType = T.TransactionType)

答案 1 :(得分:1)

这样的事情

SELECT  lt.Username, 
        lt.Name, 
        T.Balance, 
        T.Date 
FROM    (
            SELECT  u.UserID,
                    u.UserName,
                    T.TransactionType,
                    TT.Name,
                    MAX(t.Date) LastDate
            FROM    tbUser U INNER JOIN 
                    tbTransaction T ON T.UserId = U.UserId INNER JOIN 
                    tbTransactionType TT ON TT.TypeId = T.TransactionType
            GROUP BY    u.UserID,
                        u.UserName,
                        T.TransactionType,
                        TT.Name
        ) lt INNER JOIN 
        tbTransaction T ON  T.UserId = lt.UserId
                        AND t.TransactionType = lt.TransactionType
                        AND t.Date = lt.LastDate 
WHERE   lt.UserId = 1

第一个子选择将返回所有用户和交易类型的列表,以及每个的最后一个交易日期。

然后,您可以使用此信息返回到“交易”表以检索余额。

答案 2 :(得分:0)

  

我想得到最后两个条目   每种类型的交易   UserId 1

如果这是你想要的,那么你的问题确实存在,除非我误解了这个问题。

您希望每个TransactionType的最后两个条目:

-Deposite:你有Transaction $ 200和$ 20

-Withdraw:你有100美元和10美元的Transaction

你究竟想做什么?