mysql語法


1.時間類的語法

//這里是將日期格式化  ,其作用可以按這個時間分組。從而統計每天的信息
DATE_FORMAT(t1.end_time,'%Y-%m-%d %h:%i:%s') days,

2.將字符串類型結果轉為數值型

//mysql查詢結果   字符串轉int   select 字段+0    例如 
 select MAX(menu_id+0) MID FROM sys_menu

3.如果為空則用其他值代替

//如果為空的時候就用1代替
IFNULL(t1.starte,'1') `starte`,

4.mysql中如果使用到關鍵字報錯;可以加上 `` 這個符號,這不是單引號。是鍵盤左上角數字鍵1前面的那個符號

5.mybatis的mapper文件中,如果直接使用大於小於號會報錯;可以使用轉義

//大於符號案例
and t1.entry_time >=#{pd.lastLoginStart}
//小於符號案例
and t1.entry_time <=#{pd.lastLoginEnd}

 6.將內容整合在一起的語法。用於模糊查詢

//將兩邊的值合並在一起
and t1.name like concat('%',#{pd.name},'%')

 7.mysql  添加表注釋

alter table test1 comment '修改后的表的注釋';

8.mysql  添加字段注釋

alter table tb_coupon_pool modify column `id` varchar(32) NOT NULL comment '主鍵id'; 
//注意字段原有的類型加上

 9.mysql  查詢判空

<if test="pd.ewm !=null and pd.ewm !='' and pd.ewm == 1 ">
            and t1.positive_ocde_url is not null
            </if>
            
            <if test="pd.ewm !=null and pd.ewm !='' and pd.ewm ==2 ">
            and t1.positive_ocde_url is null
            </if>

 10.查詢結果整合到一起(默認會以逗號分隔)

select t1.id  ,    GROUP_CONCAT(t1.id) marks

from tb_driver t1 
GROUP BY t1.id

 11、根據時間查詢案例(處理起始時間和結束時間為同一天查詢為空的問題)

            <if test="pd.endtime !=null and pd.endtime !='' ">
                and t1.create_time &lt;=concat('','${pd.endtime}',' 23:59:59')
            </if>

 12.獲取最小值、最大值,進行分組

 <!--根據起點終點分組,分別查詢價格最低的兩個站點  -->
  <select id="getLineShiftsMoneyMin" parameterType="pd" resultType="pd">
    select min(t1.money) money from tb_shifts t1
    where t1.lineId =#{lineId} GROUP BY t1.type
  </select>

 13 mysql運算保留兩位小數

       format(SUM(t1.platmoney/100),2) platmoney,

 

  15、 mysql設置時間默認值

設置數據創建時間默認值,mysql會自動填充當前時間,不用代碼處理          

             CURRENT_TIMESTAMP

 數據最后修改時間、勾選    根據當前時間戳更新即可,mysql會在數據修改時根據當前時間修改

16、合並結果、合並多張表的結果

SELECT t1.id,t1.`name` from sys_admin t1 
UNION
SELECT t2.id,t2.`name` from sys_role t2 

 17   if用法

SELECT 
  id ,
    if(phone ='' , 'a',phone) b
    FROM sys_admin 

條件成立顯示前面代替的字符串,條件不成立 ,顯示后面的

 

 

 

18、刪除字段

ALTER  TABLE  tb_quality_inspect_day  DROP    content;

 19、刪除記錄、刪除數據

DELETE FROM tb_quality_inspect_day where is_deleted !='0'

 20、修改字段名、修改字段類型、修改字段注釋

alter table tb_quality_inspect_day change floor_num floor_num VARCHAR(50) comment "樓層";

 21、添加字段語法

ALTER TABLE tb_quality_base_beton ADD COLUMN `all_those_mark` varchar(1000) DEFAULT NULL COMMENT '舉牌描述';

 


免責聲明!

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



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