Home Database Mysql Tutorial 最常用的SQL语句

最常用的SQL语句

Jun 07, 2016 pm 05:59 PM
sql statement

最常用的SQL语句,大家可以看下,都是在开发过程中,比较常用的语句。

(1)数据记录筛选:
sql="select*from数据表where字段名=字段值orderby字段名[desc]"
sql="select*from数据表where字段名like'%字段值%'orderby字段名[desc]"
sql="selecttop10*from数据表where字段名orderby字段名[desc]"
sql="select*from数据表where字段名in('值1','值2','值3')"
sql="select*from数据表where字段名between值1and值2"

(2)更新数据记录:
sql="update数据表set字段名=字段值where条件表达式"
sql="update数据表set字段1=值1,字段2=值2……字段n=值nwhere条件表达式"

(3)删除数据记录:
sql="deletefrom数据表where条件表达式"
sql="deletefrom数据表"(将数据表所有记录删除)

(4)添加数据记录:
sql="insertinto数据表(字段1,字段2,字段3…)values(值1,值2,值3…)"
sql="insertinto目标数据表select*from源数据表"(把源数据表的记录添加到目标数据表)

(5)数据记录统计函数:
AVG(字段名)得出一个表格栏平均值
COUNT(*|字段名)对数据行数的统计或对某一栏有值的数据行数统计
MAX(字段名)取得一个表格栏最大的值
MIN(字段名)取得一个表格栏最小的值
SUM(字段名)把数据栏的值相加
引用以上函数的方法:
sql="selectsum(字段名)as别名from数据表where条件表达式"
setrs=conn.excute(sql)
用rs("别名")获取统的计值,其它函数运用同上。

(5)数据表的建立和删除:
CREATETABLE数据表名称(字段1类型1(长度),字段2类型2(长度)……)
例:CREATETABLEtab01 (namevarchar (50), datetimedefaultnow ())
DROPTABLE数据表名称(永久性删除一个数据表)

4.记录集对象的方法:
rs.movenext将记录指针从当前的位置向下移一行
rs.moveprevious将记录指针从当前的位置向上移一行
rs.movefirst将记录指针移到数据表第一行
rs.movelast将记录指针移到数据表最后一行
rs.absoluteposition=N将记录指针移到数据表第N行
rs.absolutepage=N将记录指针移到第N页的第一行
rs.pagesize=N设置每页为N条记录
rs.pagecount根据pagesize的设置返回总页数
rs.recordcount返回记录总数
rs.bof返回记录指针是否超出数据表首端,true表示是,false为否
rs.eof返回记录指针是否超出数据表末端,true表示是,false为否
rs.delete删除当前记录,但记录指针不会向下移动
rs.addnew添加记录到数据表末端
rs.update更新数据表记录
Sql = "Select Distinct 字段名 From 数据表"
Distinct函数,查询数据库存表内不重复的记录
Sql = "Select Count(*) From 数据表 where 字段名1>#18:0:0# and 字段名1count函数,查询数库表内有多少条记录,“字段名1”是指同一字段
例:
set rs=conn.execute("select count(id) as idnum from news")
response.write rs("idnum")
sql="select * from 数据表 where 字段名 between 值1 and 值2"
Sql="select * from 数据表 where 字段名 between #2003-8-10# and #2003-8-12#"
在日期类数值为2003-8-10 19:55:08 的字段里查找2003-8-10至2003-8-12的所有记录,而不管是几点几分。
select * from tb_name where datetime between #2003-8-10# and #2003-8-12#
字段里面的数据格式为:2003-8-10 19:55:08,通过sql查出2003-8-10至2003-8-12的所有纪录,而不管是几点几分。

Sql="select * from 数据表 where 字段名=字段值 order by 字段名 [desc]"
Sql="select * from 数据表 where 字段名 like '%字段值%' order by 字段名 [desc]"
模糊查询
Sql="select top 10 * from 数据表 where 字段名 order by 字段名 [desc]"
查找数据库中前10记录
Sql="select top n * form 数据表 order by newid()"
随机取出数据库中的若干条记录的方法
top n,n就是要取出的记录数
Sql="select * from 数据表 where 字段名 in ('值1','值2','值3')"

删除某个表的一个列
alter table bankbill drop column zsl
alter table xx alter/add/drop column xx
复制数据库的表
select * into 新表名 from 要复制的表的表名 where 1=2

要完全复制把where 1=2 去了就可以了
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can I install mysql on Windows 7 Can I install mysql on Windows 7 Apr 08, 2025 pm 03:21 PM

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

How to use mysql after installation How to use mysql after installation Apr 08, 2025 am 11:48 AM

The article introduces the operation of MySQL database. First, you need to install a MySQL client, such as MySQLWorkbench or command line client. 1. Use the mysql-uroot-p command to connect to the server and log in with the root account password; 2. Use CREATEDATABASE to create a database, and USE select a database; 3. Use CREATETABLE to create a table, define fields and data types; 4. Use INSERTINTO to insert data, query data, update data by UPDATE, and delete data by DELETE. Only by mastering these steps, learning to deal with common problems and optimizing database performance can you use MySQL efficiently.

Can mysql handle multiple connections Can mysql handle multiple connections Apr 08, 2025 pm 03:51 PM

MySQL can handle multiple concurrent connections and use multi-threading/multi-processing to assign independent execution environments to each client request to ensure that they are not disturbed. However, the number of concurrent connections is affected by system resources, MySQL configuration, query performance, storage engine and network environment. Optimization requires consideration of many factors such as code level (writing efficient SQL), configuration level (adjusting max_connections), hardware level (improving server configuration).

How to create tables with sql server using sql statement How to create tables with sql server using sql statement Apr 09, 2025 pm 03:48 PM

How to create tables using SQL statements in SQL Server: Open SQL Server Management Studio and connect to the database server. Select the database to create the table. Enter the CREATE TABLE statement to specify the table name, column name, data type, and constraints. Click the Execute button to create the table.

How to judge SQL injection How to judge SQL injection Apr 09, 2025 pm 04:18 PM

Methods to judge SQL injection include: detecting suspicious input, viewing original SQL statements, using detection tools, viewing database logs, and performing penetration testing. After the injection is detected, take measures to patch vulnerabilities, verify patches, monitor regularly, and improve developer awareness.

Do mysql need to pay Do mysql need to pay Apr 08, 2025 pm 05:36 PM

MySQL has a free community version and a paid enterprise version. The community version can be used and modified for free, but the support is limited and is suitable for applications with low stability requirements and strong technical capabilities. The Enterprise Edition provides comprehensive commercial support for applications that require a stable, reliable, high-performance database and willing to pay for support. Factors considered when choosing a version include application criticality, budgeting, and technical skills. There is no perfect option, only the most suitable option, and you need to choose carefully according to the specific situation.

Does mysql optimize lock tables Does mysql optimize lock tables Apr 08, 2025 pm 01:51 PM

MySQL uses shared locks and exclusive locks to manage concurrency, providing three lock types: table locks, row locks and page locks. Row locks can improve concurrency, and use the FOR UPDATE statement to add exclusive locks to rows. Pessimistic locks assume conflicts, and optimistic locks judge the data through the version number. Common lock table problems manifest as slow querying, use the SHOW PROCESSLIST command to view the queries held by the lock. Optimization measures include selecting appropriate indexes, reducing transaction scope, batch operations, and optimizing SQL statements.

How to check SQL statements How to check SQL statements Apr 09, 2025 pm 04:36 PM

The methods to check SQL statements are: Syntax checking: Use the SQL editor or IDE. Logical check: Verify table name, column name, condition, and data type. Performance Check: Use EXPLAIN or ANALYZE to check indexes and optimize queries. Other checks: Check variables, permissions, and test queries.

See all articles