之前大規模檢修你的問題,我會建議:
SELECT 'COL1' AS `COL_NAMES`,(SELECT `c1` as `START_ROW` FROM `tbl` WHERE !ISNULL(`c1`) ORDER BY c1 ASC LIMIT 1) AS `ROW_START`,(SELECT `c1` as `END_ROW` FROM `tbl` WHERE !ISNULL(`c1`) ORDER BY c1 DESC LIMIT 1) AS `ROW_END`
UNION ALL
SELECT 'COL2' AS `COL_NAMES`,(SELECT `c2` as `START_ROW` FROM `tbl` WHERE !ISNULL(`c2`) ORDER BY c2 ASC LIMIT 1) AS `ROW_START`,(SELECT `c2` as `END_ROW` FROM `tbl` WHERE !ISNULL(`c2`) ORDER BY c2 DESC LIMIT 1) AS `ROW_END`
UNION ALL
SELECT 'COL3' AS `COL_NAMES`,(SELECT `c3` as `START_ROW` FROM `tbl` WHERE !ISNULL(`c3`) ORDER BY c3 ASC LIMIT 1) AS `ROW_START`,(SELECT `c3` as `END_ROW` FROM `tbl` WHERE !ISNULL(`c3`) ORDER BY c3 DESC LIMIT 1) AS `ROW_END`
UNION ALL
SELECT 'COL4' AS `COL_NAMES`,(SELECT `c4` as `START_ROW` FROM `tbl` WHERE !ISNULL(`c4`) ORDER BY c4 ASC LIMIT 1) AS `ROW_START`,(SELECT `c4` as `END_ROW` FROM `tbl` WHERE !ISNULL(`c4`) ORDER BY c4 DESC LIMIT 1) AS `ROW_END`
假設如表:
CREATE TABLE IF NOT EXISTS `tbl` (
`c1` int(11) DEFAULT NULL,
`c2` int(11) DEFAULT NULL,
`c3` int(11) DEFAULT NULL,
`c4` int(11) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
INSERT INTO `tbl` (`c1`, `c2`, `c3`, `c4`) VALUES
(1, 30, 89, 34),
(2, 49, 76, 44),
(NULL, 52, 90, NULL),
(NULL, NULL, 16, NULL);
我不能給你現在正在用你的新數據得到一個確切的答案,但是我的答案在這裏會給你帶來很大的幫助,你只需要修復一些東西,比如列名,表名。如果您正在測試包含空字符串的第一個和最後一個非NULL
值,則我的!ISNULL(...)
邏輯正常。如果你正在爲空字符串,而不是測試,使用LENGTH(...)=0
代替,或者兩者都是可能的組合:(!ISNULL(...) AND LENGTH(...)>0)
是這個函數真的在mysql中工作嗎?如果是的話,那麼在哪個版本的MySQL? –
以上將在SQL中工作。如果你想要MySQL SELECT column_name FROM table_name ORDER BY column_name ASC LIMIT 1; –
好吧,所以請編輯您的帖子相應 –