当前位置: 首页 > news >正文

SQL学习-unit1-2(基础查询语句)

大小写都可以;分号终止;换行符 大间隔 和 tab 在执行SQL语句时都没有作用;

1-选择语句|the select statement(在单一表格中检索数据)

USE sql_store; SELECT * -- 选择所有列 -- from customers #从customers表中 -- where customer_id = 1 #返回一行 -- order by first_name #根据first_name给表排序

一般两条子句就可以选择;

子句的顺序是有影响的,先有select,然后from,再然后是where,最后是order by,不能改变这些子句的顺序,不然会出现语法错误;

from,where,order by 都能进行选择;

2-选择子句|the select clause

SELECT last_name, first_name, points, (points+10)*100 AS 'discount factor' -- as取名字,‘’或“”把名字括起来之后才能空一格 FROM customers

我们可以用*返回全部列或者,我们指定想要返回的列;

我们可以用算术表达式,可以选择给每列和结果集一个别名;

SELECT distinct state FROM customers

distinct在选择时可以去掉重复值;

3-where 子句|the where clause

SELECT * from customers -- where points > 3000 -- where state <>'VA' where birth_date > '1990-01-01'

比较运算符:>,>=,<,<=,=,!=/<>;

4-and,or,not运算符|the AND,OR and NOT operators

SELECT * from customers where birth_date > '1990-01-01' or points > 1000 and state = 'VA'

and有优先级;

SELECT * from customers where not (birth_date > '1990-01-01' or points > 1000)

5-in 运算符|the IN operator

SELECT * from customers -- where state = 'VA' or state = 'GA' or state = 'FL' -- where state in ('VA','GA','FL') where state not in ('VA','GA','FL')

当你想要同一系列值比较一个属性,就可以用in运算符;

6-between运算符|the between operator

SELECT * from customers -- where points >=1000 and points <= 3000 where points between 1000 and 3000

7-like 运算符|the like operator

如何检索遵循特定字符串模式的行;

SELECT * from customers -- where last_name like 'b%' -- where last_name like '%b%' -- where last_name like '%y' -- where last_name like '_____y' where last_name like 'b____y'

'_'表示一个单字符,'%'表示任意字符数;

8-regexp运算符|the REGEXP operator

是正则表达式(regular expression)的缩写;

SELECT * from customers -- where last_name like '%field%' -- where last_name regexp 'field' -- where last_name regexp '^field' -- where last_name regexp 'field$' -- where last_name regexp 'field$|mac|rose' -- where last_name regexp '[gim]e' -- where last_name regexp 'e[fmp]' where last_name regexp '[a-h]e'

"^"符号表示字符串开头;

"$"符号表示字符串末尾;

"|"符号表示逻辑或;

[abc]:匹配 a /b/c 任意单个指定字符;

[a-d]:匹配 a~d 范围内任意单个字符;

9-is null运算符|the IS NULL operator

如何搜索缺失了属性的记录;

SELECT * from customers -- where phone is null where phone is not null

10-order by子句|the ORDER BY clause

为数据排序;

SELECT first_name,last_name,10 as points from customers -- order by state desc,first_name -- order by birth_date -- order by points,first_name order by 1,2

可多字段排序:先排前面的字段,相同再排后面的字段;

可根据没被选择的列排序;

可用字母代替排序;

desc降序,asc升序;

11-limit子句|the LIMIT clause

如何限制查询返回的记录;

limit子句永远放在最后;

SELECT * from customers limit 6,3 -- 跳过前6条记录,然后获取3条记录 -- page 1:1-3 -- page 2:4-6 -- page 3:7-9
http://www.jsqmd.com/news/695904/

相关文章:

  • Phi-4-mini-flash-reasoning生产环境:多任务并行推理与显存优化部署
  • 2026四氟密封件技术全解:四氟密封圈/定制密封件/定制密封圈/气缸密封圈/氟胶密封件/油缸密封件/油缸密封圈/选择指南 - 优质品牌商家
  • 长芯微LMD7617完全P2P替代AD7617,16 个通道进行双路同步采样的 14 位 DAS
  • 2026年3月知名的咸蛋黄实力厂家推荐,咸蛋黄咸香与甜点搭配 - 品牌推荐师
  • 告别繁琐!pipreqs输出格式定制:savepath与print参数终极应用指南
  • 从绍兴小镇到AI浪潮之巅:那个“一天不编程就难受”的唐文斌,和他身后的两个传奇时代
  • 2025_NIPS_UI-Genie: A Self-Improving Approach for Iteratively Boosting MLLM-based Mobile GUI Agents
  • Qwen1.5-1.8B-Chat-GPTQ-Int4开源镜像:滑动窗口注意力对长文本中文理解能力影响
  • 别让PICkit3.5+吃灰了!手把手教你激活硬件仿真,搞定485通讯调试难题
  • Scikit-learn梯度提升超快
  • Spring Boot 3.x 开发中消息积压时的消费速率控制问题详解
  • 2026食品农业检测机构推荐指南:农药第三方检测/医药第三方检测/土壤检测/宠物食品检测/检测机构实验室/水质检测/选择指南 - 优质品牌商家
  • React18极客园
  • 如何用Red Panda C++开发环境解锁高效编程体验?
  • 范浩强:从IOI金牌到AI创业者的十四年征程
  • (UPDATING)LLM微调之实战,SFTTrainer官方案例、LoRA/QloRA微调案例、Unsloth、分布式训练、LLaMA Factory
  • LinkSwift网盘直链下载助手:告别限速的终极解决方案
  • Flux2-Klein-9B-True-V2保姆级教程:supervisor.conf配置文件深度解析
  • 深入SOEM源码:SDO读写函数背后的EtherCAT邮箱通信机制与性能调优
  • Voxtral-4B-TTS-2603效果实测:同一音色下不同语言(英/法/西)韵律节奏差异
  • 第四章-09-练习案例:有几个偶数
  • 杨沐:那个从福州三中走出的IOI金牌少年,和他旷视传奇
  • AI Agent大厂实习vs创业公司:哪个更值得去
  • C语言二维数组
  • HTML函数运行慢是硬件问题吗_HTML函数卡顿原因排查技巧【详解】
  • 安卓应用开发中协程作用域未正确取消问题详解
  • Qwen3-ASR-0.6B多场景落地指南:从边缘设备到云端集群部署
  • Qwen3.5-27B工业设计辅助:CAD截图理解+技术参数补全效果展示
  • 西门子TIA Portal V17实战:手把手教你用EnTalk PCIe板卡打通PROFINET与Modbus RTU
  • <iostream>