国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

mysql 多表查詢 比較兩個字段最大、最小值,并顯示對應字段

瀏覽:101日期:2022-06-21 09:03:49

問題描述

有兩個表,表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 ,

請各位大神幫助,寫出正確查詢語句。mysql 多表查詢 比較兩個字段最大、最小值,并顯示對應字段

mysql 多表查詢 比較兩個字段最大、最小值,并顯示對應字段

問題解答

回答1:

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

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 NULLAND b.ticker_sell IS NOT NULLGROUP BY a.date;回答2:

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;

主站蜘蛛池模板: 启东市| 临安市| 洞口县| 建宁县| 白银市| 芮城县| 荔浦县| 台州市| 南溪县| 高青县| 连州市| 吴堡县| 浏阳市| 博湖县| 咸阳市| 历史| 正宁县| 德化县| 丰都县| 冕宁县| 沁阳市| 辽宁省| 布尔津县| 营山县| 厦门市| 安化县| 张掖市| 肃北| 崇义县| 顺昌县| 凤翔县| 渑池县| 望奎县| 武汉市| 汝城县| 涿鹿县| 榕江县| 临泉县| 高邑县| 余江县| 论坛|