SQL语句示例
SQL的意思是结构化查询语言,其主要功能是同各种数据库建立联系,进行沟通.查询指的是对存储于SQL的数据的请求。查询要完成的任务是:将 Select 语句的结果集提供给用户。Select 语句从 SQL 中检索出数据,然后以一个或多个结果集的形式将其返回给用户。
==========================================================
Select 基本语法结构
==========================================================
Select[predicate]{*|table.*|[table.]]field [,[table.]field2[,...]}
[AS alias1 [,alias2[,...]]]
[INTO new_table_name]
FROM tableexpression [, ...]
[Where...]
[GROUP BY...]
[ORDER BY...][ASC | DESC] ]
predicate-->指定返回记录(行)的数量,可选:ALL,TOP
* --------->指定表中所有字段(列).
table ----->指定表的名称.
field ----->指定表中字段(列)的名称
[AS alias] -替代表中实际字段(列)名称的化名.
[INTO new_table_name]-->创建新表及名称.
tableexpression---->表的名称.
[GROUP BY...]表示以该字段的值分组
[ORDER BY...]表示按升序排列,降序选 DESC;
------------------------------------------------------------
1 选择列
------------------------------------------------------------
sql语句在access中的输入方法
(1)选择"查询"-->新建-->默认设计视图-->点击确定
(2)关闭"显示表对话框"
(3)在菜单拦选择"视图"--->SQL视图 ,就可以输入SQL语句了
示例1_1_选择所有字段
Select *
FROM useres;
示例1_2_选择部分字段
Select user_name,real_name,submit_date
FROM useres;
示例1_3查询两个表中的字段
Select 图书信息表.图书条码, 借书信息表.图书条码
FROM 图书信息表, 借书信息表;
示例解读:
通过上面简单示例我们体会到
(1) Select 子句选择列表,它指出查询结果集所包含的字段(列)及其属性,选择所有列时用通配府*,选择部分列时要用逗号隔开
(2) FROM 子句指出查询的表名,要指定多个表时中间用逗号隔开
------------------------------------------------------------
2 TOP指定返回记录数量
------------------------------------------------------------
示例1_4_返回记录数量
Select TOP 3 *
FROM useres;
-------------------------------------------------------------
3 AS派生新字段
-------------------------------------------------------------
示例1_5_派生新字段
Select user_name,(submit_date+30) AS new_date
FROM useres;
------------------------------------------------------------
4 Where指定条件进行筛选
------------------------------------------------------------
示例1_6等号查找指定记录
Select *
FROM useres
Where useres.real_name="红红";
示例1_7年龄大于30的人
Select *
FROM useres
Where age>30
从上面可以看出,在根据条件进行筛选时,要用到运算符,常见的运算符如下所示:
1 比较运算符
= 等于
不等于
> 大于
>= 大于等于
2 逻辑运算符
ALL 所有条件都为true则返回true
AND 两个条件都为true则返回true
OR 有一个条件为true则返回true
NOT 对值取反
ANY 所有条件中只要有一个为true则返回true
BETWEEN 只要操作数在指定的范围内,则返回true
IN 只要操作数等于表达式中的一个,则返回true
LIKE 如果操作数与模式相匹配,则返回true
SOME 在一系列的比较中,有些为true则返回true
示例1_8_某日以前注册用户
Select *
FROM useres
Where submit_date示例1_9_某时间段注册用户
Select *
FROM useres
Where submit_date BETWEEN #2004-1-1# AND #2005-5-1#
示例1_10_按关键字查找
Select *
FROM useres
Where useres.real_name LIKE "*李*"
------------------
IN与OR的区别
-----------------
示例_IN筛选字段中的记录
Select real_name,submit_date
FROM useres
Where real_name In("小李","小张")
示例_OR筛选字段中的记录
Select real_name,submit_date
FROM useres
Where real_name="小李" or real_name="小张"
------------------------------------------------------------
5 GROUP BY分组结果集
------------------------------------------------------------
示例1_12_GROUPBY分组结果集
Select sex, SUM(age) AS age之SUM
FROM useres
GROUP BY useres.sex
ORDER BY SUM(age) DESC;
示例解读:
按字段"sex"下的记录对新"字段"age之SUM"进行分组.
ORDER BY...DESC用来指定按降序排列
本例中的sum为SQL中的聚合函数(对一组值进行操作,返回单一的汇总值),下面是常用的几个聚合函数:
1 SUM 求总和函数
格式:
SUM([ALL|DISTINCT] expression)
参数:
ALL 对所有值求总和,默认为ALL
DISTINCT 求总和时排除重复项
expression 值或表达式,可以是变量,字段,函数等
2 AVG 求平均值函数
格式:
AVG([ALL|DISTINCT] expression)
参数:
ALL 对所有值求平均,默认为ALL
DISTINCT 求平均时排除重复项
expression 值或表达式,可以是变量,字段,函数等
3 MIN和MAX函数分别为求最小值和最大值,格式和上面类似.
4 COUNT 行计数函数
格式:
COUNT({[ALL|DISTINCT] expression|*})
ALL 表示计算除了NULL以外的其他项,为默认选项
DISTINCT 表示COUNT返回唯一非空值的数量
expression 为表达式,不能是txte,image,ntxt和uniqueidentifier类型的数据.
示例1_13_AVG求平均值函数
Select sex, AVG(age) AS age之AVG
FROM useres
GROUP BY useres.sex
ORDER BY AVG(age) DESC;
示例1_14_COUNT返回记录数量
Select COUNT(*)
FROM useres
示例1_15_按性别分组记录数量
Select sex, COUNT(*)
FROM useres
GROUP BY sex;
------------------------------------------------------------
6 DISTINCT从尾部除去重复记录
------------------------------------------------------------
Select DISTINCT real_name
FROM useres
------------------------------------------------------------
7 组合查询
------------------------------------------------------------
当需要从多个表中查询时,可以使用组合查询
Select useres.real_name, logtime.log_time
FROM useres, logtime
Where (((useres.real_name)=[logtime].[real_name]));

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.
