0x00 SELECT简介
简单的来说SELECT 语句用于从表中选取数据。
0x01 select的用法
1.1 检索数据表内所有列数据
SELECT * FROM city;
ID | Name | CountryCode | District | Population |
1 | Kabul | AFG | Kabol | 1780000 |
2 | Qandahar | AFG | Qandahar | 237500 |
3 | Herat | AFG | Herat | 186800 |
4 | Mazar-e-Sharif | AFG | Balkh | 127800 |
1.2 检索数据表内单列数据
SELECT Name FROM city;
Name |
Kabul |
Qandahar |
Herat |
Mazar-e-Sharif |
Amsterdam |
Rotterdam |
1.3 检索数据表内多列数据
SELECT ID, Name, CountryCode FROM city;
ID | Name | CountryCode |
1 | Kabul | AFG |
2 | Qandahar | AFG |
3 | Herat | AFG |
4 | Mazar-e-Sharif | AFG |
5 | Amsterdam | NLD |
6 | Rotterdam | NLD |
1.4 过滤检索出来的数据
SELECT *
FROM city
WHERE CountryCode = 'AFG';
ID | Name | CountryCode | District | Population |
1 | Kabul | AFG | Kabol | 1780000 |
2 | Qandahar | AFG | Qandahar | 237500 |
3 | Herat | AFG | Herat | 186800 |
4 | Mazar-e-Sharif | AFG | Balkh | 127800 |
0x02 总结
2.1 在这里说一下测试语句的编码习惯,sql语法用大写,表还是按照数据库表名大小写来编写。当时听老东家的DBA说,异库这样的语句方便移植。
这里举个例子吧。SELECT * FROM TAB_NAME;
UPDATE TAG_NAME SET ROW_NAME = 'XXXX' WHERE ROW_NAME = 'XXX';
2.2 .写语句要注意分层,这样语句明朗清晰,方便二次维护和更新
来个多表联查的例子:
SELECT *
FROM (
SELECT ROW_ID, ROW_NAME
FROM TAB_INFO
WHERE TAB_INFO.ROW_NAME = 'XXXX' )TAB_NAME_A
LEFT JOIN (
SELECT ROW_ID, ROW_ID
FROM TAB_INFO_A
WHERE TAB_INFO_A.ROW_NAME = 'XXXX')TAB_NAME_B
ON TAB_NAME_A.ROW_NAME = TAB_NAME_B.ROW_NAME
这里重点说说一下,一定不要乱写!!!!!!!我也经历过一期项目开发周期短,任务重。但挖这种sql坑真的很恐怖,
因为一期项目数据量要么就是很小,要么就只是测试数据,肯定怎么写都可以,等量级上来了,极端可怕。尽量写的阅读性好一点!