以前写的语句: SELECT 生成时间 FROM result WHERE `型号`='1' ORDER BY 生成时间 DESC LIMIT 1; SELECT 生成时间 FROM result WHERE `型号`='2' ORDER BY 生成时间 DESC LIMIT 1; SELECT 生成时间 FROM result WHERE `型号`='3' ORDER BY 生成时间 DESC LIMIT 1;
生成时间是 datatime 程序拿到结果后,再本地算法算出最小(时间最早的值) 请问如果在 Mysql 语句上,如何实现一句出结果呢? `型号`='3'很有可能没有存在。。。
select 型号,min(时间) from xx where 型号 in(1,2,3) group by 型号
someonedeng
2020-08-28 00:18:24 +08:00
SELECT * from (SELECT 生成时间,1 as 型号 FROM result WHERE `型号`='1' ORDER BY 生成时间 DESC LIMIT 1 ) t1 union all SELECT * from (SELECT 生成时间,2 as 型号 FROM result WHERE `型号`='2' ORDER BY 生成时间 DESC LIMIT 1 ) t2 union all SELECT * from (SELECT 生成时间,3 as 型号 FROM result WHERE `型号`='3' ORDER BY 生成时间 DESC LIMIT 1) t3;
SELECT 生成时间 FROM result WHERE `型号`='1' ORDER BY 生成时间 DESC LIMIT 1 union all SELECT 生成时间 FROM result WHERE `型号`='2' ORDER BY 生成时间 DESC LIMIT 1 union all SELECT 生成时间 FROM result WHERE `型号`='3' ORDER BY 生成时间 DESC LIMIT 1;