您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關Mysql 中有哪些常用的SQL語句,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
查詢時間,友好提示
timestamp 日期類型
$sql = "select date_format(create_time, '%Y-%m-%d') as day from table_name";
int 時間戳類型
$sql = "select from_unixtime(create_time, '%Y-%m-%d') as day from table_name";
一個sql 返回多個總數
$sql = "select count(*) all, " ;
$sql .= " count(case when status = 1 then status end) status_1_num, ";
$sql .= " count(case when status = 2 then status end) status_2_num ";
$sql .= " from table_name";
Update Join / Delete Join
$sql = "update table_name_1 ";
$sql .= " inner join table_name_2 on table_name_1.id = table_name_2.uid ";
$sql .= " inner join table_name_3 on table_name_3.id = table_name_1.tid ";
$sql .= " set *** = *** ";
$sql .= " where *** ";
//delete join 同上。
替換某字段的內容的語句
$sql = "update table_name set content = REPLACE(content, 'aaa', 'bbb') ";
$sql .= " where (content like '%aaa%')";
獲取表中某字段包含某字符串的數據
$sql = "SELECT * FROM `表名` WHERE LOCATE('關鍵字', 字段名) ";
查找表中多余的重復記錄
單個字段
$sql = "select * from 表名 where 字段名 in ";
$sql .= "(select 字段名 from 表名 group by 字段名 having count(字段名) > 1 )";
多個字段
$sql = "select * from 表名 別名 where (別名.字段1,別名.字段2) in ";
$sql .= "(select 字段1,字段2 from 表名 group by 字段1,字段2 having count(*) > 1 )";
刪除表中多余的重復記錄(留id最小)
單個字段
$sql = "delete from 表名 where 字段名 in ";
$sql .= "(select 字段名 from 表名 group by 字段名 having count(字段名) > 1) ";
$sql .= "and 主鍵ID not in ";
$sql .= "(select min(主鍵ID) from 表名 group by 字段名 having count(字段名 )>1) ";
多個字段
$sql = "delete from 表名 別名 where (別名.字段1,別名.字段2) in ";
$sql .= "(select 字段1,字段2 from 表名 group by 字段1,字段2 having count(*) > 1) ";
$sql .= "and 主鍵ID not in ";
$sql .= "(select min(主鍵ID) from 表名 group by 字段1,字段2 having count(*)>1) ";
看完上述內容,你們對Mysql 中有哪些常用的SQL語句有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。