MySQL - 在加入 2 table 时选择第二个 table 中的最后一个条目
MySQL - Selecting last entry in second table when joining 2 tables
我有 2 个 tables 事务和 trans_comments 当我进行内部连接时 returns 第一个条目,我想做的是显示地址和状态表交易 table 和最后输入的评论以及来自 trans_comments table
的日期
这是我得到的
SELECT t.address, t.status, c.time, c.comment
FROM transactions t
INNER JOIN trans_comments c ON c.transactionId = t.transactionId
WHERE STATUS LIKE '%Listing%'
GROUP BY t.address DESC
我错过了什么?
这是 table 的布局:
`transactions` (
`transactionId` int(11) NOT NULL AUTO_INCREMENT,
`address` varchar(45) NOT NULL,
`listing_agent` varchar(65) DEFAULT NULL,
`status` varchar(45) NOT NULL,
`notes` varchar(100) DEFAULT NULL,
`file_type` varchar(25) NOT NULL,
`system` varchar(25) DEFAULT NULL,
PRIMARY KEY (`transactionId`)
)ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=41 ;
`trans_comments` (
`commId` int(11) NOT NULL AUTO_INCREMENT,
`transactionId` int(11) NOT NULL,
`comment` blob NOT NULL,
`commentBy` varchar(45) NOT NULL,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`commId`),
KEY `transactionId` (`transactionId`)
)ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=101 ;
谢谢
SELECT t.address, t.status, c.time, c.comment
FROM transactions t
INNER JOIN
(SELECT c1.*
FROM trans_comments c1
LEFT JOIN trans_comments c2
ON c1.transactionId = c2.transactionId
AND c1.`time` < c2.`time`
WHERE c2.commId IS NULL
) c
ON c.transactionId = t.transactionId
WHERE STATUS LIKE '%Listing%'
我有 2 个 tables 事务和 trans_comments 当我进行内部连接时 returns 第一个条目,我想做的是显示地址和状态表交易 table 和最后输入的评论以及来自 trans_comments table
的日期这是我得到的
SELECT t.address, t.status, c.time, c.comment
FROM transactions t
INNER JOIN trans_comments c ON c.transactionId = t.transactionId
WHERE STATUS LIKE '%Listing%'
GROUP BY t.address DESC
我错过了什么?
这是 table 的布局:
`transactions` (
`transactionId` int(11) NOT NULL AUTO_INCREMENT,
`address` varchar(45) NOT NULL,
`listing_agent` varchar(65) DEFAULT NULL,
`status` varchar(45) NOT NULL,
`notes` varchar(100) DEFAULT NULL,
`file_type` varchar(25) NOT NULL,
`system` varchar(25) DEFAULT NULL,
PRIMARY KEY (`transactionId`)
)ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=41 ;
`trans_comments` (
`commId` int(11) NOT NULL AUTO_INCREMENT,
`transactionId` int(11) NOT NULL,
`comment` blob NOT NULL,
`commentBy` varchar(45) NOT NULL,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`commId`),
KEY `transactionId` (`transactionId`)
)ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=101 ;
谢谢
SELECT t.address, t.status, c.time, c.comment
FROM transactions t
INNER JOIN
(SELECT c1.*
FROM trans_comments c1
LEFT JOIN trans_comments c2
ON c1.transactionId = c2.transactionId
AND c1.`time` < c2.`time`
WHERE c2.commId IS NULL
) c
ON c.transactionId = t.transactionId
WHERE STATUS LIKE '%Listing%'