摘要:以下是工作中常用的語(yǔ)句查詢(xún)時(shí)間,友好提示日期類(lèi)型時(shí)間戳類(lèi)型一個(gè)返回多個(gè)總數(shù)同上。
以下是工作中常用的SQL語(yǔ)句 :
查詢(xún)時(shí)間,友好提示
timestamp 日期類(lèi)型
$sql = "select date_format(create_time, "%Y-%m-%d") as day from table_name";
int 時(shí)間戳類(lèi)型
$sql = "select from_unixtime(create_time, "%Y-%m-%d") as day from table_name";
一個(gè)sql 返回多個(gè)總數(shù)
$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 同上。
替換某字段的內(nèi)容的語(yǔ)句
$sql = "update table_name set content = REPLACE(content, "aaa", "bbb") "; $sql .= " where (content like "%aaa%")";
獲取表中某字段包含某字符串的數(shù)據(jù)
$sql = "SELECT * FROM `表名` WHERE LOCATE("關(guān)鍵字", 字段名) ";
獲取字段中的前4位
$sql = "SELECT SUBSTRING(字段名,1,4) FROM 表名 ";
查找表中多余的重復(fù)記錄
單個(gè)字段
$sql = "select * from 表名 where 字段名 in ";
$sql .= "(select 字段名 from 表名 group by 字段名 having count(字段名) > 1 )";
多個(gè)字段
$sql = "select * from 表名 別名 where (別名.字段1,別名.字段2) in ";
$sql .= "(select 字段1,字段2 from 表名 group by 字段1,字段2 having count(*) > 1 )";
刪除表中多余的重復(fù)記錄(留id最小)
單個(gè)字段
$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) ";
多個(gè)字段
$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) ";
Thanks ~
系統(tǒng)的講解 - SSO 單點(diǎn)登錄
系統(tǒng)的講解 - PHP WEB 安全防御
系統(tǒng)的講解 - PHP 緩存技術(shù)
系統(tǒng)的講解 - PHP 接口簽名驗(yàn)證
系統(tǒng)的講解 - PHP 浮點(diǎn)數(shù)高精度運(yùn)算
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/21885.html
摘要:基礎(chǔ)篇查詢(xún)時(shí)間,友好提示時(shí)間戳類(lèi)型一個(gè)返回多個(gè)總數(shù)同上。根據(jù)上面的數(shù)據(jù),應(yīng)該得到的范圍。 基礎(chǔ)篇 //查詢(xún)時(shí)間,友好提示 $sql = select date_format(create_time, %Y-%m-%d) as day from table_name; //int 時(shí)間戳類(lèi)型 $sql = select from_unixtime(create_time, %Y-%m-%...
閱讀 2579·2021-11-22 09:34
閱讀 947·2021-11-19 11:34
閱讀 2807·2021-10-14 09:42
閱讀 1487·2021-09-22 15:27
閱讀 2391·2021-09-07 09:59
閱讀 1741·2021-08-27 13:13
閱讀 3437·2019-08-30 11:21
閱讀 779·2019-08-29 18:35