SQL --------------- between 和< >


between值 and 值 運算符用於選取介於兩個值之間的數據范圍內的值,常與where一塊使用
between運算符選擇給定范圍內的值。值可以是數字,文本或日期。

使用between的時候會與and 一塊連用,表示在啥啥之間,是不是讓我們想起來大於某個小於某個

注意:

  在某些數據庫中,BETWEEN 選取介於兩個值之間但不包括兩個測試值的字段。
  在某些數據庫中,BETWEEN 選取介於兩個值之間且包括兩個測試值的字段。
  在某些數據庫中,BETWEEN 選取介於兩個值之間且包括第一個測試值但不包括最后一個測試值的字段。

語法:

select * from 表名 where 字段 between 字段對應的值 and 字段對應的值

建個表弄點數據

 

 

 運算符 between   和  <  ,>,<=  ,>= 一塊使用

 注意:查詢數字的時候是左右都包含哦,這是sqlserver 數據庫

 

 

  運算符 not between   和!  <  ,!>一塊使用

語法:

select * from 表名 where not  字段 between 字段對應的值  and  字段對應的值

 

 

運算符between  和 in 一塊使用

 

 進行查詢日期格式的時候和查詢數字一樣,一般在實際的項目中用於查詢這個日期在這個區間中

tbCreateTimeEnd和tbCreateTimeStart 前段傳來的值
        {//時間范圍
            DateTime d1 = DateTime.Parse("1900-01-01 00:00:00");
            DateTime d2 = DateTime.Parse("2900-01-01 00:00:00");
            if (this.tbCreateTimeStart.Value != string.Empty)
            {
                try
                {
                    d1 = Convert.ToDateTime(this.tbCreateTimeStart.Value);
                }
                catch
                {
                    d1 = d2;
                }
            }
            if (this.tbCreateTimeEnd.Value != string.Empty)
            {
                try
                {
                    d2 = Convert.ToDateTime(this.tbCreateTimeEnd.Value);
                }
                catch
                {
                    d2 = d1;
                }
            }
            strSql += " and CreateTime between '" + d1.ToString("yyyy-MM-dd 00:00:00") + "' and '" + d2.ToString("yyyy-MM-dd 23:59:59") + "'";
        }

查詢文本和字符串的時候也可以查

 

 

 

-- between 字段對應值 and  字段對應值
--  語法: select * from 表名 where 字段 between 字段對應的值 and 字段對應的值
-- 查詢年齡在10 到20 之間的數據   左右都包含
select * from test where Age between 11 and 22
-- 查詢年齡在10 到20 之間的數據
select * from test where Age<=22  and Age >10


-- 查詢年齡不在10 到20 之間的數據  包括 11和20
select * from test where not Age between 12 and 22 
-- 使用運算符 !> 等 查詢查詢年齡不在10 到20 之間的數據
select * from test where Age !>11  or Age !<23

--  查詢年齡在11到22 之間 不包含 11和22
select * from test where (Age between 11 and 22) and not Age in (11,22)


-- 根據字母排序進行查詢  m 不包含  不會查詢漢字
select * from test where Name between 'b' and 'm'

-- 查詢漢字
select * from test where Name between '大喬' and '小喬'

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM