1宽菜、將查詢結(jié)果保存為一張新表
create table new_table as select * from old_talbe;
慎用create table as select,一定要注意默認(rèn)值的問題:
https://blog.csdn.net/evilcry2012/article/details/85337283
從一個(gè)表中復(fù)制所有的列插入到另一個(gè)已存在的表中:
INSERT INTO table2 SELECT * FROM table1;
2雅任、進(jìn)入mysql的控制臺(tái)后,使用source命令執(zhí)行
Mysql>source 【sql腳本文件的路徑全名】
3岂津、SQL語句中count(1)count()count(字段)用法的區(qū)別
一. count(1)和count()的區(qū)別
- count(1)和count(*)的作用:
都是檢索表中所有記錄行的數(shù)目,不論其是否包含null值。 - 區(qū)別:但是count(1)比count()效率更高
二 . count(字段)與count(1)和count()的區(qū)別
count(字段)的作用是檢索表中的這個(gè)字段的非空行數(shù)饺谬,不統(tǒng)計(jì)這個(gè)字段值為null的記錄
執(zhí)行效率分析:
l 列名為主鍵嫩海,count(列名)會(huì)比count(1)快
l 列名不為主鍵冬殃,count(1)會(huì)比count(列名)快
l 如果表多個(gè)列并且沒有主鍵,則 count(1) 的執(zhí)行效率優(yōu)于 count()
l 如果有主鍵出革,則 select count(主鍵)的執(zhí)行效率是最優(yōu)的
l 如果表只有一個(gè)字段造壮,則 select count()最優(yōu)。
4骂束、sql 語句NVL()用法
NVL函數(shù)是一個(gè)空值轉(zhuǎn)換函數(shù)耳璧,有兩種寫法:
(1) NVL(表達(dá)式1,表達(dá)式2)
如果表達(dá)式1為空值展箱,NVL返回值為表達(dá)式2的值旨枯,否則返回表達(dá)式1的值。
(2) NVL2(表達(dá)式1混驰,表達(dá)式2攀隔,表達(dá)式3)
如果表達(dá)式1為空值皂贩,返回值為表達(dá)式3的值。如果表達(dá)式1不為空昆汹,返回值為表達(dá)式2的值明刷。
5、What does SQL clause “GROUP BY 1” mean?
https://stackoverflow.com/questions/7392730/what-does-sql-clause-group-by-1-mean
6满粗、Hive執(zhí)行sql文件:hive -f t.sql
7辈末、# [用SQL命令查看MYSQL數(shù)據(jù)庫大小](不能用于Hive)(https://www.cnblogs.com/ggwudivs/p/9947616.html)
1)、進(jìn)入information_schema 數(shù)據(jù)庫(存放了其他的數(shù)據(jù)庫的信息)
use information_schema;
2)映皆、查詢所有數(shù)據(jù)的大屑菲浮:
select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables;
3)、查看指定數(shù)據(jù)庫的大型背埂:
比如查看數(shù)據(jù)庫home的大小
select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables where table_schema='home';
4)组去、查看指定數(shù)據(jù)庫的某個(gè)表的大小
比如查看數(shù)據(jù)庫home中 members 表的大小
select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables where table_schema='home' and table_name='members';
8、sql語句中將兩個(gè)字段的值連接
select database_name, table_name, concat(database_name, '.', table_name) as full_name from table_tmp;
9步淹、## SQL之case when then用法詳解
--簡單case函數(shù)
case sex
when '1' then '男'
when '2' then '女’
else '其他' end
--case搜索函數(shù)
case when sex = '1' then '男'
when sex = '2' then '女'
else '其他' end
10从隆、SQL中的cast()函數(shù)
語法:
CAST (expression AS data_type)
參數(shù)說明:
expression:任何有效的SQServer表達(dá)式。
AS:用于分隔兩個(gè)參數(shù)缭裆,在AS之前的是要處理的數(shù)據(jù)广料,在AS之后是要轉(zhuǎn)換的數(shù)據(jù)類型。
data_type:目標(biāo)系統(tǒng)所提供的數(shù)據(jù)類型幼驶,包括bigint和sql_variant艾杏,不能使用用戶定義的數(shù)據(jù)類型。
11盅藻、數(shù)據(jù)庫導(dǎo)出:
mysqldump -h 172.16.163.214 -u root -p --default-character-set=utf8 jcmdb > /root/mydb.sql
數(shù)據(jù)庫導(dǎo)入:
進(jìn)入mysql的控制臺(tái)后购桑,使用source命令執(zhí)行
Mysql>source 【sql腳本文件的路徑全名】
12、KUDU不能使用
delete from table_name where year_month='';
只能用
alter table table_name drop partition(year_month='');
13氏淑、# sql針對某一字段去重勃蜘,并且保留其他字段
今天客戶提了一個(gè)小需求,希望我能提供一條sql語句假残,幫助他對數(shù)據(jù)中 _field 這個(gè)字段的值去重缭贡,并且保留其他字段的數(shù)據(jù)。第一反應(yīng)是select distinct辉懒,但這種語句在對某個(gè)字段去重時(shí)阳惹,無法保留其他字段,所以select distinct不成立眶俩。因?yàn)橛脩魧θブ貨]有要求莹汤,字段值重復(fù)時(shí)保留任意一行就行,所以我想到當(dāng)字段值重復(fù)時(shí)颠印,選出對應(yīng)主鍵最大的那條數(shù)據(jù)作為保留數(shù)據(jù)纲岭,這樣可以實(shí)現(xiàn)用戶的去重需求抹竹。但是用戶的表中又沒有主鍵,沒辦法止潮,我們只好先使用窗口函數(shù)創(chuàng)建主鍵了腔丧。
因?yàn)槠綍r(shí)喜歡用hive on spark寫sql激捏,所以sql語句使用中間表的形式來寫谅辣,_field為去重字段浇坐,other_fields為原表table中_field外的其他字段
1.創(chuàng)建主鍵(存在主鍵則無需創(chuàng)建,窗口函數(shù)需要遍歷所有行數(shù)據(jù)仅偎,數(shù)據(jù)量大時(shí)會(huì)很慢)
TEMP table1 = select row_number() over (order by _field) as id, _field, other_fields from table
2.選出每個(gè)_field對應(yīng)的最大主鍵
TEMP table2 = select max(id) as max_id from table1 group by _field
3.找出選中的主鍵對應(yīng)的原表數(shù)據(jù)
TEMP table3 = select _field, other_fields from table2 left join table on table2.max_id = table1.id
14、判斷某一字段是否重復(fù):
select _field, count(1) from table_name
group by _field having count(1)>1;
15雳殊、SQL連接兩個(gè)字段
select concat(field1_name,field2_name) from table_name;
16橘沥、向動(dòng)態(tài)分區(qū)表插入數(shù)據(jù)
insert into t2 partition(etl_dt) select * from t1;
17、Hive復(fù)制表結(jié)構(gòu)新建表:
CREATE TABLE student2 LIKE student;
18夯秃、SQL類型轉(zhuǎn)換
cast(field as decimal(10,0))
19座咆、深入理解where 1=1的用處
https://www.imooc.com/article/41863
20、sqlite3 查看表結(jié)構(gòu)
select * from sqlite_master where type="table" and name="emperors";
select * from sqlite_master where type='table';
21仓洼、為什么Hive不支持CHAR/VARCHAR類型?
HIVE不是標(biāo)準(zhǔn)的SQL介陶,它實(shí)現(xiàn)的叫HQL, 其實(shí)就是一個(gè)Map-reduce的解釋能簡化JAVA編程色建,他的類型基本上是JAVA語言的類型了
22哺呜、SQL UNION 和 UNION ALL 操作符
https://www.w3school.com.cn/sql/sql_union.asp
UNION ALL 命令和 UNION 命令幾乎是等效的,不過 UNION ALL 命令會(huì)列出所有的值箕戳,UNION會(huì)去重某残。
23、# char陵吸,varchar和nvarchar有什么區(qū)別玻墅?
1、CHAR壮虫。CHAR存儲(chǔ)定長數(shù)據(jù)很方便澳厢。不論你存儲(chǔ)的數(shù)據(jù)是否達(dá)到了10個(gè)字節(jié),都要占去10個(gè)字節(jié)的空間
2囚似、VARCHAR剩拢。存儲(chǔ)變長數(shù)。
varchart(n):長度為 n 個(gè)字節(jié)的可變長度且非 Unicode 的字符數(shù)據(jù)饶唤。范圍1 ~ 8000
3裸扶、NVARCHAR。它表示存儲(chǔ)的是Unicode數(shù)據(jù)類型的字符搬素。所有的字符都用兩個(gè)字節(jié)表示呵晨,即英文字符也是用兩個(gè)字節(jié)表示魏保。
nvarchar(n):包含 n 個(gè)字符的可變長度 Unicode 字符數(shù)據(jù)。范圍1 ~ 4000
一般來說摸屠,如果含有中文字符谓罗,用nvarchar,如果純英文和數(shù)字季二,用char/varchar
例:
varchar(4) 可以輸入4個(gè)字節(jié)檩咱,也可以輸入兩個(gè)漢字
nvarchar(4) 可以輸四個(gè)漢字,也可以輸4個(gè)字母胯舷,但最多四個(gè)
24刻蚯、通過-f 參數(shù)來執(zhí)行查詢文件
impala-shell -f impala-shell.sql
25、hive高級應(yīng)用之不進(jìn)入交互桑嘶,執(zhí)行 hive sql
hive -e "select * from student;"
26炊汹、hive執(zhí)行sql文件:
hive -f t.sql
27、hive導(dǎo)入txt文件(https://www.cnblogs.com/xk-bench/p/9201578.html)
load data local inpath '/tmp/fun_user.txt' into table fun_user_external;
加載完后用Impala可能看不到逃顶,需要在Impala下執(zhí)行以下命令:
refresh 庫名.表名讨便;
28、hive動(dòng)態(tài)分區(qū)數(shù)據(jù)導(dǎo)入
https://blog.csdn.net/liubiaoxin/article/details/48931247
29以政、mysql如何實(shí)現(xiàn)多行查詢結(jié)果合并成一行(默認(rèn)是逗號分隔)
select group_concat(field_a) from table1;
30霸褒、用SQL刪除一列字段:
ALTER TABLE store
DROP COLUMN address
;
31、GROUP_CONCAT()盈蛮,1024這就是一般MySQL系統(tǒng)默認(rèn)的最大長度(http://www.reibang.com/p/011157cce605)
SET group_concat_max_len=102400;
32废菱、# sqoop 導(dǎo)入增量數(shù)據(jù)到hive
https://blog.csdn.net/leprovision/article/details/87698064
sqoop import --connect jdbc:mysql://192.168.1.10:3306/itcast --username root --password 123 --table trade_detail --hive-import --hive-overwrite --hive-table trade_detail --fields-terminated-by '\t'
33、【Hive】load數(shù)據(jù)抖誉,跳過第一行
在建表時(shí)昙啄,進(jìn)行如下參數(shù)設(shè)置。
tblproperties(
"skip.header.line.count"="n", --跳過文件行首n行
"skip.footer.line.count"="n" --跳過文件行尾n行
)
34寸五、創(chuàng)建自增字段
create table customers (
customer_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, first_name TEXT,
last_name TEXT
);
35梳凛、HiveSQL中沒有Left、Right函數(shù)
可以用substr(column,1,nchar)代替
substr在Mysql和HiveSQL中都可以使用
注意:substr從1開始梳杏,包頭包尾
left/right從0開始韧拒,包頭不包尾
36、MySQL_TEXT與BLOB字段類型的區(qū)別
https://blog.csdn.net/wanlixingzhe/article/details/95324243
TEXT與BLOB的主要差別就是BLOB保存二進(jìn)制數(shù)據(jù)十性,TEXT保存字符數(shù)據(jù)叛溢。目前幾乎所有博客內(nèi)容里的圖片都不是以二進(jìn)制存儲(chǔ)在數(shù)據(jù)庫的,而是把圖片上傳到服務(wù)器然后正文里使用標(biāo)簽引用劲适,這樣的博客就可以使用TEXT類型楷掉。而BLOB就可以把圖片換算成二進(jìn)制保存到數(shù)據(jù)庫中。
37霞势、# MySQL查詢一張表有多少個(gè)字段
select count(*) from information_schema.COLUMNS where TABLE_SCHEMA='數(shù)據(jù)庫名' and table_name='表名'
38烹植、MySQL的sql_mode解析與設(shè)置
https://blog.csdn.net/CCCCalculator/article/details/70432123
https://blog.csdn.net/wang_517766334/article/details/3152939
設(shè)置的sqlmode在退出或者重啟mysql后sqlmode即失效斑鸦,要讓sqlmode的設(shè)置永久有效,可以修改mysql的配置文件草雕。在mysql配置文件中添加sql_mode的字段巷屿,后面加上相應(yīng)的sqlmode模式,如下圖所示將sqlmode設(shè)置為ansi墩虹。
39嘱巾、解決“LOAD DATA local INFILE”時(shí)出現(xiàn)The used command is not allowed with this MySQL version問題
https://blog.csdn.net/ziyou434/article/details/82504130
https://www.cnblogs.com/xiaohuomiao/p/10646266.html
mysql -u Name -p --local-infile=1;
40、mysql 批量創(chuàng)建表及自動(dòng)分區(qū)
http://www.reibang.com/p/56bac8e455a5
41诫钓、mysqldump 數(shù)據(jù)遷移
mysqldump -uroot -ppassword --default-character-set=utf-8 --single-transaction --skip-opt -q [-d(t)] database_name --tables table_name1, table_name2, table_name3 > /data/s.sql
鎖表問題:
https://blog.csdn.net/weiliu1463/article/details/25305371
mysqldump的--skip-opt問題 :
http://blog.sina.com.cn/s/blog_504818d0010084lr.html
https://imysql.com/2013/01/17/mysql-faq-mysqldump-skip-opt.html
mysqldump --master-data=2 --single-transaction:
https://blog.csdn.net/linuxheik/article/details/71480882
查看被鎖的表:
select * from INFORMATION_SCHEMA.INNODB_TRX旬昭;
mysqldump如果不添加--single-transaction --skip-opt兩個(gè)參數(shù),則MySQL事務(wù)隔離級別為不可重復(fù)讀(read-committed)菌湃,如果添加則為可重復(fù)讀(repeatable-read)
http://www.reibang.com/p/50c389f00614
42问拘、看你的mysql當(dāng)前默認(rèn)的存儲(chǔ)引擎:
mysql> show variables like '%storage_engine%';
43、MySQL修改密碼
mysql> set password for root@localhost = password('123');
44慢味、Mysql優(yōu)化
https://www.cnblogs.com/sharpest/p/10390035.html
45、# MySQL自動(dòng)設(shè)置create_time和update_time
CREATE TABLE ProfitAndLossHistory
(
StockId
char(6) NOT NULL COMMENT '股票代碼',
StockName
varchar(10) NOT NULL COMMENT '股票名稱',
ProfitOrLoss
tinyint(1) NOT NULL COMMENT 'True為盈利墅冷,F(xiàn)alse為虧損',
ProfitOrLossFund
float NOT NULL COMMENT '累計(jì)盈虧資金',
PositionFund
int(11) NOT NULL COMMENT '持倉資金(單位W纯路,四舍五入)',
RecentRange
int(11) NOT NULL COMMENT '近期漲跌幅度(百分之)',
Hotspot
int(10) unsigned NOT NULL COMMENT '熱點(diǎn)天數(shù)(非熱點(diǎn)為0)',
BuyTimes
int(10) unsigned NOT NULL COMMENT '第幾次購買',
DaysOfPossession
int(11) NOT NULL COMMENT '持有天數(shù)(最低兩天)',
Weekdays
int(10) unsigned NOT NULL COMMENT '買入時(shí)周幾',
IsBuyCurrentHighPoint
tinyint(1) NOT NULL COMMENT '買入點(diǎn)仍是目前高點(diǎn)(1為是,0為否)',
TrendAfterSale
int(11) NOT NULL COMMENT '賣后走勢(0為賣后當(dāng)天上漲寞忿,1為1天后上漲驰唬,2為2天上漲,3為3天后上漲腔彰,4為4天后上漲叫编,5為5天即以上上張,-1為1天后下跌霹抛,-2為2天后下跌搓逾,-3為3天后下跌,-4為4天后下跌杯拐,-5為5天即以上下跌)',
InsertTime
timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
UpdateTime
timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='盈虧較大的交易歷史(波動(dòng)1000以上)';
46霞篡、SQLite Delete 刪除語句
DELETE FROM table_name WHERE [condition];
47、建視圖時(shí)字段起個(gè)別名的方法
https://blog.csdn.net/tianping168/article/details/3292636
48端逼、interval關(guān)鍵字用法
https://blog.csdn.net/arenzhj/article/details/16902141
49朗兵、create table as 和create table like的區(qū)別
https://www.cnblogs.com/bjx2020/p/10224811.html
50、LEFT JOIN關(guān)聯(lián)表中ON,WHERE后面跟條件的區(qū)別
https://blog.csdn.net/wqc19920906/article/details/79785424
51顶滩、## TRUNC函數(shù)的用法
截取今天:
SQL> select sysdate,trunc(sysdate,'dd') from dual;
SYSDATE TRUNC(SYSDATE,'DD')
2009-03-24 21:31:17 2009-03-24 00:00:00
截取本周第一天:
SQL> select sysdate,trunc(sysdate,'d') from dual;
SYSDATE TRUNC(SYSDATE,'D')
2009-03-24 21:29:32 2009-03-22 00:00:00
截取本月第一天:
SQL> select sysdate,trunc(sysdate,'mm') from dual;
SYSDATE TRUNC(SYSDATE,'MM')
2009-03-24 21:30:30 2009-03-01 00:00:00
52余掖、DATE_ADD()
https://blog.csdn.net/weixin_38750084/article/details/87874604
select DATE_ADD('2019-02-19 00:00:00',INTERVAL 1 DAY) from dual;
53、## sql語句中日期相減的操作
select datediff(month, 開始日期,結(jié)束日期); --兩日期間隔月
select datediff(day, 開始日期,結(jié)束日期); --兩日期間隔天
54礁鲁、# sql查詢字段個(gè)數(shù)
select count(*) from information_schema.COLUMNS where table_name='表名';
55盐欺、MySQL8中LOAD DATA INFILE出現(xiàn)The used command is not allowed with this MySQL version問題
https://blog.csdn.net/qinglingls/article/details/88981950
56赁豆、MySQL中外鍵的定義、作用找田、添加和刪除
https://blog.csdn.net/lk142500/article/details/83590862
57歌憨、MySQL加載txt文件
alter table table_name add column dw_etl_dt int default 20200401;
alter table table_name partition by LIST(dw_etl_dt)
(
partition p20200401 values in (20200401);
)
select count(*) from table_name where dw_etl_dt=20200401;
select count(*) from table_name partition(p20200401);
alter table table_name add partition(
partition p20200402 values in (20200402)
);
load data infile '' terminated by '|' lines (a,b,@dw_etl_dt) set dw_etl_dt=if(isnull(@dw_etl_dt),20200401,@dw_etl_dt)
http://blog.sina.com.cn/s/blog_4d398f210100x9nn.html
https://dev.mysql.com/doc/refman/5.7/en/load-data.html
58、mysql 的show processlist和show full processlist區(qū)別
https://www.pianshen.com/article/88711276723/
showprocesslist只能列出當(dāng)前100條墩衙。如果想全部列出务嫡,可以使用SHOW FULL PROCESSLIST命令
59、mysql執(zhí)行truncate drop 時(shí)卡死問題解決
https://blog.csdn.net/zoujian1991/article/details/111871735
show processList
找到狀態(tài)不正常的kill掉
60漆改、MySQL grant 語法的詳細(xì)解析
https://blog.csdn.net/yufengicd/article/details/9704865
61心铃、MYSQL獲取月份多少天
SELECT day(LAST_DAY('2012-09-01'))
62、## mysql int類型范圍
INT 4 -2147483648 2147483647
63挫剑、This inspection detects situations when list creation could be rewritten with list literal.(https://blog.csdn.net/u014418725/article/details/89145380)
point_collection = list()
point_collection.append(point_list)
64去扣、# Mysql中設(shè)置小數(shù)點(diǎn)用什么數(shù)據(jù)類型 decimal
Decimal(n,m)表示數(shù)值中共有n位數(shù),其中整數(shù)n-m位樊破,小數(shù)m位愉棱。例:decimal(10,6),數(shù)值中共有10位數(shù)哲戚,其中整數(shù)占4位奔滑,小數(shù)占6位。
65顺少、# 主鍵和候選鍵有什么區(qū)別朋其?
表格的每一行都由主鍵唯一標(biāo)識(shí),一個(gè)表只有一個(gè)主鍵。
主鍵也是候選鍵脆炎。按照慣例梅猿,候選鍵可以被指定為主鍵,并且可以用于任何外鍵
66秒裕、查看字段注釋(https://blog.csdn.net/weixin_31022063/article/details/113149728)
SHOW FULL COLUMNS FROM tbl_name [FROM db_name]//列出字段及詳情
67袱蚓、MySQL 轉(zhuǎn)成字符串(https://www.cnblogs.com/xing-nb/p/12218946.html)
SELECT CAST(123 AS CHAR)
68、mysql 字符串拼接的幾種方式(https://blog.csdn.net/syslbjjly/article/details/90640975)
SELECT CONCAT("name=","lich",NULL) AS test;
69几蜻、字段轉(zhuǎn)換為字符串
select cast(now() as char)
70癞松、Mysql修改字段名、修改字段類型(https://blog.csdn.net/u010002184/article/details/79354136)
alter table table1 change column1 column1 varchar(100) DEFAULT 1.2 COMMENT '注釋';
71入蛆、mysql添加字段(https://blog.51cto.com/victor2016/1868716)
alter table t1 add d1 char(10) before d2;
alter table t2 add d3 char(10) after d4
72响蓉、# MySQL 添加字段 修改字段 刪除字段
alter table users drop name;
73、mysql 如何獲取當(dāng)前時(shí)間 前哨毁、后一小時(shí)的時(shí)間枫甲?(https://blog.csdn.net/z13197919100/article/details/79236048)
前一個(gè)小時(shí):
select date_sub(now(), interval 1 hour);
74、mysql 1分鐘前_mysql 數(shù)據(jù)庫取前后幾秒 幾分鐘 幾小時(shí) 幾天的語句(https://blog.csdn.net/weixin_35870469/article/details/113150947)
select SUBDATE(now(),interval 60 second);
75、mysql Date類型與String類型轉(zhuǎn)換(https://www.cnblogs.com/zkwarrior/p/14872332.html)
DATE_FORMAT(now(),"%Y-%m-%d %T") 2015-09-01 17:10:52
76想幻、python連接MySQL并查詢(https://blog.csdn.net/yushupan/article/details/85061143)
results = cursor.fetchall()
for result in results:
print (result)
77粱栖、varchar與char有什么區(qū)別?(https://blog.csdn.net/qq_20264581/article/details/83755789)
區(qū)別一脏毯,定長和變長
char 表示定長闹究,長度固定,varchar表示變長食店,即長度可變渣淤。char如果插入的長度小于定義長度時(shí),則用空格填充吉嫩;varchar小于定義長度時(shí)价认,還是按實(shí)際長度存儲(chǔ),插入多長就存多長自娩。
78用踩、