sql查詢資料庫總結
1.查詢所有資料
select * from table_name;
2.as查詢某一欄位並修改輸出欄位名稱
select field_name as field_name from table_name;
3.where查詢某一欄位條件為field_name 不為null或者等於某一個或兩個值的資料
select field_name from table_name where field_name is not null;
select field_name from table_name where (field_name=‘name1’ or field_name=‘name1’…);
4.groud by查詢某一欄位的資料並對該欄位的資料進行分組(一樣的資料進行整合)
select field_name from table_name groud by field_name;
select field_name1,field_name2 from table_name groud by field_name1,field_name2;
–select後查詢的資料,如果有groud by後面沒有包含的資料量的欄位,則無法執行。需要在select後面進行sum一下。
意思就是:select後的欄位不能比groud by後的欄位多,也不能出現groud by 後面沒有的欄位
5.DESC降序、ASC升序查詢某一欄位的資料並對輸出的資料進行排序,
預設輸出排序是升序,後面新增DESC引數後變為降序
select field_name from table_name order by field_name DESC ;
select field_name from table_name order by field_name ASC ;
6.limit查詢某一欄位,並輸出該欄位的前n條資料,limit後引數也可以是兩個
select field_name from table_name limit n;
7.length
select field_name from table_name where length(field_name)>=4;
8.sum查詢某一欄位,並對該欄位求和
select sum(field_name) from table_name
9.insert into查詢一個表中的資料,並匯入到另一個表中,其中在查詢表2的時候可以對錶2的資料做一些處理
insert into table_name1(field_name1,field_name2) select fiels_name1,field_name2 from table_name2
10.left查詢某一個欄位的資料,其中只取該欄位資料的從左到右的前n個字元
select LEFT( field_name , n ) from table_name;
例子:去除title為null的資料,獲取以value排序前十的資料
select title,sum(comment_count+support_sum) as value from hotspot_data where title is not null GROUP BY title ORDER BY value DESC limit 10