56,677
社区成员
发帖
与我相关
我的任务
分享
--测试数据
if not object_id(N'Tempdb..#A') is null
drop table #A
Go
Create table #A([code] int,[detail] int)
Insert #A
select 1,2 union all
select 2,3 union all
select 3,4
GO
if not object_id(N'Tempdb..#B') is null
drop table #B
Go
Create table #B([code] int,[time] nvarchar(24))
Insert #B
select 1,N'9-12' union all
select 1,N'9-15' union all
select 1,N'9-18' union all
select 2,N'9-12' union all
select 2,N'9-13' union all
select 2,N'9-14'
Go
--测试数据结束
SELECT #A.code ,
MAX(time) AS time
FROM #A
JOIN #B ON #B.code = #A.code
GROUP BY #A.code
USE test;
DROP TABLE
IF EXISTS `a`;
CREATE TABLE `a` (
`code` tinyint DEFAULT 0,
`detail` tinyint DEFAULT 0
) ENGINE = INNODB DEFAULT CHARSET = utf8;
INSERT INTO `a`
VALUES
(
1,
2
),
(
2,
3
),
(
3,
4
);
DROP TABLE
IF EXISTS `b`;
CREATE TABLE `b` (
`code` tinyint DEFAULT 0,
`time` varchar(20) DEFAULT ''
) ENGINE = INNODB DEFAULT CHARSET = utf8;
INSERT INTO `b`
VALUES
(
1,
'9-12'
),
(
1,
'9-15'
),
(
1,
'9-18'
),
(
2,
'9-12'
),
(
2,
'9-13'
),
(
2,
'9-14'
);
select a.`code`,`detail`,`time_max` from `a` inner join (SELECT
`code`,
max(`time`) `time_max`
FROM
`b` group by `b`.code) as kkk on `a`.code=kkk.code;