mysql怎么查詢數(shù)值 mysql如何查詢語句

mysql 一個表自連查詢數(shù)據(jù)?

建表和視圖語句:

成都網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計、重慶網(wǎng)站建設(shè)、微信開發(fā)、成都小程序開發(fā)、集團(tuán)成都企業(yè)網(wǎng)站定制等服務(wù)項目。核心團(tuán)隊均擁有互聯(lián)網(wǎng)行業(yè)多年經(jīng)驗,服務(wù)眾多知名企業(yè)客戶;涵蓋的客戶類型包括:成都被動防護(hù)網(wǎng)等眾多領(lǐng)域,積累了大量豐富的經(jīng)驗,同時也獲得了客戶的一致夸獎!

DROP TABLE IF EXISTS `tab`;

CREATE TABLE `tab`? (

`id` int(11) NOT NULL AUTO_INCREMENT,

`userid` int(11) NULL DEFAULT NULL,

`date` datetime NULL DEFAULT NULL,

`instructions` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,

`amount` decimal(18, 2) NULL DEFAULT NULL,

`type` tinyint(1) NULL DEFAULT NULL,

PRIMARY KEY (`id`) USING BTREE

) ENGINE = MyISAM AUTO_INCREMENT = 9 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

create view tab_v? as

select

`tab`.`userid` AS `userid`,

date_format( `tab`.`date`, '%Y-%m' ) AS `ym`,

`tab`.`instructions` AS `instructions`,

`tab`.`amount` AS `amount`,

`tab`.`type` AS `type`

from

`tab`

查詢語句:

select t0.userid? ? ? ?用戶ID,

?t0.ym? ? ? ? ? ?年月,

?t1.cnt? ? ? ? ? 本月收入筆數(shù),

?t2.instructions 本月最大收入項目,

?t2.amount? ? ? ?本月最大收入金額,

?t3.instructions 本月最小收入項目,

?t3.amount? ? ? ?本月最小收入金額,

?t4.cnt? ? ? ? ? 本月支出筆數(shù),

?t5.instructions 本月最大支出項目,

?t5.amount? ? ? ?本月最大支出金額,

?t6.instructions 本月最小支出項目,

?t6.amount? ? ? ?本月最小支出金額,

?t7.cnt? ? ? ? ? 本月結(jié)余

from (select distinct a.userid,

? ? ? ? ? ? ? ? ? a.ym

? ? from tab_v a) t0

left join (select a.userid,

? ? ? ? ? ? ? a.ym,

? ? ? ? ? ? ? count(1) cnt

? ? ? ? ?from tab_v a

? ? ? ? where a.type = 2

? ? ? ? group by a.userid,

? ? ? ? ? ? ? ? ?a.ym) t1

on t0.userid = t1.userid

and t0.ym = t1.ym

left join (select a.userid,

? ? ? ? ? ? ? a.ym,

? ? ? ? ? ? ? a.amount,

? ? ? ? ? ? ? group_concat(b.instructions) instructions

? ? ? ? ?from (select a.userid,

? ? ? ? ? ? ? ? ? ? ? a.ym,

? ? ? ? ? ? ? ? ? ? ? max(a.amount) amount

? ? ? ? ? ? ? ? ?from tab_v a

? ? ? ? ? ? ? ? where a.type = 2

? ? ? ? ? ? ? ? group by a.userid,

? ? ? ? ? ? ? ? ? ? ? ? ?a.ym) a,

? ? ? ? ? ? ? tab_v b

? ? ? ? where a.userid = b.userid

? ? ? ? ? and a.ym = b.ym

? ? ? ? ? and a.amount = b.amount

? ? ? ? group by a.userid,

? ? ? ? ? ? ? ? ?a.ym,

? ? ? ? ? ? ? ? ?a.amount) t2

on t0.userid = t2.userid

and t0.ym = t2.ym

left join (select a.userid,

? ? ? ? ? ? ? a.ym,

? ? ? ? ? ? ? a.amount,

? ? ? ? ? ? ? group_concat(b.instructions) instructions

? ? ? ? ?from (select a.userid,

? ? ? ? ? ? ? ? ? ? ? a.ym,

? ? ? ? ? ? ? ? ? ? ? min(a.amount) amount

? ? ? ? ? ? ? ? ?from tab_v a

? ? ? ? ? ? ? ? where a.type = 2

? ? ? ? ? ? ? ? group by a.userid,

? ? ? ? ? ? ? ? ? ? ? ? ?a.ym) a,

? ? ? ? ? ? ? tab_v b

? ? ? ? where a.userid = b.userid

? ? ? ? ? and a.ym = b.ym

? ? ? ? ? and a.amount = b.amount

? ? ? ? group by a.userid,

? ? ? ? ? ? ? ? ?a.ym,

? ? ? ? ? ? ? ? ?a.amount) t3

on t0.userid = t3.userid

and t0.ym = t3.ym

left join (select a.userid,

? ? ? ? ? ? ? a.ym,

? ? ? ? ? ? ? count(1) cnt

? ? ? ? ?from tab_v a

? ? ? ? where a.type = 1

? ? ? ? group by a.userid,

? ? ? ? ? ? ? ? ?a.ym) t4

on t0.userid = t4.userid

and t0.ym = t4.ym

left join (select a.userid,

? ? ? ? ? ? ? a.ym,

? ? ? ? ? ? ? a.amount,

? ? ? ? ? ? ? group_concat(b.instructions) instructions

? ? ? ? ?from (select a.userid,

? ? ? ? ? ? ? ? ? ? ? a.ym,

? ? ? ? ? ? ? ? ? ? ? max(a.amount) amount

? ? ? ? ? ? ? ? ?from tab_v a

? ? ? ? ? ? ? ? where a.type = 1

? ? ? ? ? ? ? ? group by a.userid,

? ? ? ? ? ? ? ? ? ? ? ? ?a.ym) a,

? ? ? ? ? ? ? tab_v b

? ? ? ? where a.userid = b.userid

? ? ? ? ? and a.ym = b.ym

? ? ? ? ? and a.amount = b.amount

? ? ? ? group by a.userid,

? ? ? ? ? ? ? ? ?a.ym,

? ? ? ? ? ? ? ? ?a.amount) t5

on t0.userid = t5.userid

and t0.ym = t5.ym

left join (select a.userid,

? ? ? ? ? ? ? a.ym,

? ? ? ? ? ? ? a.amount,

? ? ? ? ? ? ? group_concat(b.instructions) instructions

? ? ? ? ?from (select a.userid,

? ? ? ? ? ? ? ? ? ? ? a.ym,

? ? ? ? ? ? ? ? ? ? ? min(a.amount) amount

? ? ? ? ? ? ? ? ?from tab_v a

? ? ? ? ? ? ? ? where a.type = 1

? ? ? ? ? ? ? ? group by a.userid,

? ? ? ? ? ? ? ? ? ? ? ? ?a.ym) a,

? ? ? ? ? ? ? tab_v b

? ? ? ? where a.userid = b.userid

? ? ? ? ? and a.ym = b.ym

? ? ? ? ? and a.amount = b.amount

? ? ? ? group by a.userid,

? ? ? ? ? ? ? ? ?a.ym,

? ? ? ? ? ? ? ? ?a.amount) t6

on t0.userid = t6.userid

and t0.ym = t6.ym

left join (select a.userid,

? ? ? ? ? ? ? a.ym,

? ? ? ? ? ? ? sum(case

? ? ? ? ? ? ? ? ? ? when type = 1 then

? ? ? ? ? ? ? ? ? ? ?-1 * a.amount

? ? ? ? ? ? ? ? ? ? when 2 then

? ? ? ? ? ? ? ? ? ? ?a.amount

? ? ? ? ? ? ? ? ? end) cnt

? ? ? ? ?from tab_v a

? ? ? ? group by a.userid,

? ? ? ? ? ? ? ? ?a.ym) t7

on t0.userid = t7.userid

and t0.ym = t7.ym

只能做到這個效果了

mysql 怎么查詢一個字段值的長度

1、一般查詢語句:SELECT `lcontent` FROM `caiji_ym_liuyan`

查詢數(shù)據(jù):

2、有些時候需要查詢某個字段的長度為多少時候才顯示數(shù)據(jù):

SQL語句:SELECT `lcontent` FROM `caiji_ym_liuyan` where

length(lcontent)=40

PS:在mysql中一個漢字等于3個字節(jié),所以查詢的時候需要轉(zhuǎn)換一下

特別要注意的就時候?qū)τ谧止?jié)的轉(zhuǎn)換!

mysql 查詢 一定數(shù)值范圍數(shù)據(jù)

先創(chuàng)建一個函數(shù)

如下

CREATE?FUNCTION?isnum(

p_string?VARCHAR(32)?

)

RETURNS?int(4)

NOT?DETERMINISTIC

SQL?SECURITY?DEFINER

COMMENT?'檢查字符串是否為純數(shù)字'

BEGIN

/*檢查字符串是否為純數(shù)字*/

/*返回值:1-為純數(shù)字?0-非純數(shù)字*/

DECLARE?iResult?INT?DEFAULT?0;

SELECT?p_string?REGEXP?'^[0-9]*$'?INTO?iResult;

IF?iResult?=?1?THEN

RETURN?1;

ELSE

RETURN?0;

END?IF;

END;

然后

select?*?from?表名?where?isnum(字段名)?0?and?cast(字段名?as?DECIMAL)?1000?and?cast(字段名?as?DECIMAL)2000

這句的表名和字段名你替換成你自己的

當(dāng)前標(biāo)題:mysql怎么查詢數(shù)值 mysql如何查詢語句
文章轉(zhuǎn)載:http://bm7419.com/article26/dohpocg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、網(wǎng)頁設(shè)計公司、微信小程序、App開發(fā)、電子商務(wù)、ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計