mysql 多表查询 比较两个字段最大、最小值,并显示对应字段
伊谢尔伦
伊谢尔伦 2017-04-17 14:56:16
0
2
732

有两个表,表A和表B,结构相同,但是具体字段不同,在表A.date = B.date条件下,查询出以下结果:MAX(A.ticker_buy-B.ticker_sell) 和MIN(A.ticker_buy-B.ticker_sell) ,即同一时间下两个表不同字段的差值的最大值和最小值,并显示对应最大值、最小值对应的date字段,我尝试用sql语句写了下,但是结果不对(用excel大致比较过)。我的语句如下:

select max(okcomfuturetickerquarter.ticker_buy-okcomfuturetickernextweek.ticker_sell) as "最大差价",min(okcomfuturetickerquarter.ticker_buy-okcomfuturetickernextweek.ticker_sell) as "最小差价",okcomfuturetickerquarter.date as "时间" from okcomfuturetickerquarter,okcomfuturetickernextweek where okcomfuturetickerquarter.date=okcomfuturetickernextweek.date and okcomfuturetickerquarter.ticker_buy is not null and okcomfuturetickernextweek.ticker_sell is not null ,

请各位大神帮助,写出正确查询语句。

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

Antworte allen(2)
迷茫

先吐槽一下很长的表名……

SELECT a.date as "时间", max(a.ticker_buy-b.ticker_sell) AS "最大差价",min(a.ticker_buy-b.ticker_sell) AS "最小差价" FROM a,b 
WHERE a.date = b.date 
AND a.ticker_buy IS NOT NULL
AND b.ticker_sell IS NOT NULL
GROUP BY a.date;
伊谢尔伦

max的参数应该是column名,先将每一行ticker_buy和ticker_sell的差值算出来,然后用order by来排序,取第一个即可
select (a.ticker_buy-b.ticker_sell) as ticker from a,b where a.date = b.date GROUP BY a.date order by ticker;

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!