Mysql中的DQL查询语句
欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 Mysql中的DQL查询语句 1、查询所有列 --查询 学生 表所有记录(行) select *from 学生 --带条件的查询 select *from 学生 where 年龄19 2、查询指定的列 --查询 所有人的姓名和性别 select 姓名,性
欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入
Mysql中的DQL查询语句
1、查询所有列 --查询 学生 表所有记录(行) select *from 学生
--带条件的查询 select *from 学生 where 年龄>19
2、查询指定的列 --查询 所有人的姓名和性别 select 姓名,性别 from 学生
--查询 所有 年龄>19 的学生的 姓名 select 姓名,地址 from 学生 where 年龄>19
/*比较运算符 = > = 不等于 !>不大于 !
3、给列取别名 --方式一 select 姓名,地址=家乡 from 学生
--方式二 select 姓名,地址as家乡 from 学生
4、消除重复 --查询该表有哪些家乡 select distinct 地址 from 学生
5、top n(查询前N条) select top 3 * from 学生 --查询前3条记录
6、排序 select *from 学生 order by 年龄 asc --按年龄进行升序排列 --desc降序 --asc升序
select * from 学生 order by 年龄 desc ,编号 asc ---按年龄降序 --先按年龄进行降序,在出现相同年龄的时候,把这些相同的学生 再按照 学号 升序排列
--例:查询 学生 表中,年龄最大的三个学生的 年龄、姓名、编号 select top 3 年龄,姓名,编号 from 学生 order by 年龄 desc
7、 and(并且)、or(或者) select *from 学生 where 年龄=20 and 姓名='张三'
--例如:查询 性别为男的 或 专 地址为武汉 select *from 学生 where 性别='男' or 地址='武汉'
8、between ... and(介于...之间) --例:查询年龄为20-30之间的所有人 select *from 学生 where 年龄 between 20 and 30
9、in 的用法 select * from 学生 where 年龄 in(20,19,18)
10、top N 与 order by 同时使用
--例:查询年龄最大的一人 select top 1 with ties * from 学生 --加了with ties 后 如有并列第一的就全都显示 order by 年龄 desc
11、case替换查询结果 --查询所有人信息,如果年龄>=40岁,就显示"中年人", -- 如果年龄 介于30-39 ,就显示“青年” -- 如果年龄 介于20-29 ,就显示“青少年” -- 如果年龄 小于20 , 就显示“少年”
select 学号,姓名,性别, 年龄=case when 年龄>=40 then '中年人' when 年龄 between 30 and 39 then '青年' when 年龄 between 20 and 29 then '青少年' else '少年' --else表示不满足以上条件时,就全部 end ,住址 from 学生
12、模糊查找 使用like子句进行模糊查询 like子句与通配符配合使.Sql server提供4种通配符 1.%:表示任意字符 2. _:表示单个任意字符 3.[ ]:表示方括号里列出的任意一个字符. 4.[^]:表示任意一个没有在方括号里列出的字符.
--例:查找姓周的所有人信息 select * from 学生 where 姓名 like '周%' --%可以代替任意几个字符
select * from 学生 where 姓名 like '周_' --_表示可以代替一个字符
--例:查找姓名的第二个字包含 ’心‘ 或者 ’三‘ 的人 select * from 学生 where 姓名 like '_[星,三]_'
--嵌套查询(一般嵌套请不要超过3层,即不要出现超过3个select) select * from 学生 where 年龄
--例如:查询所有比 中文系所有学生年龄 大的学生 select * from xs where 年龄> ( select top 1 年龄 from xs where 所在系='中文' order by 年龄 desc )
/*运算符 all some any */
/* all:指定表达式要与子查询结果集中的每个值都进行比较,当表达式与每个值都满足比较的关系时,才返回true,否则返回false;
Some和any:表示表达式只要与子查询结果集中的某个值满足比较的关系时, 就返回true,否则返回false.
*/
select * from xs where 年龄>all ( select 年龄 from xs where 所在系='中文' )
----------------------------------表的复制 /*把所有计算机系的学生拉出来单独创建一个表*/
create table xs_jisuanji --创建一个新表 ( 学号 int, 姓名 varchar(50), 性别 char(10), 年龄 int )
insert into xs_jisuanji --查询内容 并复制内容到新建的表 select 学号 ,姓名, 性别, 年龄 from xs where 所在系='计算机'H
/*复制方式二*/ --创建中文系的表 select 学号,姓名,性别 ,年龄 into xs_zhongwen from xs where 所在系='中文'
---跨数据库表的复制(需要在 表名前加数据库名) select * into test.dbo.xs from n2d09003
---- --聚合函数
--求学生总分 select sum(成绩) as 总分数 from xs
--求分数最高分 select max(成绩) as 最高分 from xs
-- 求最低分 select min(成绩) as 最低分 from xs
--求平均分 select avg(成绩) as 平均分 from xs
--统计有多少名学生 select count(成绩) as 人数 from xs
[1] [2]

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











MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Docker is important on Linux because Linux is its native platform that provides rich tools and community support. 1. Install Docker: Use sudoapt-getupdate and sudoapt-getinstalldocker-cedocker-ce-clicotainerd.io. 2. Create and manage containers: Use dockerrun commands, such as dockerrun-d--namemynginx-p80:80nginx. 3. Write Dockerfile: Optimize the image size and use multi-stage construction. 4. Optimization and debugging: Use dockerlogs and dockerex

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

CentOS is the first choice for server and enterprise environments for its superior security, stability and performance. 1) Security provides forced access control through SELinux to improve system security. 2) Stability is supported by the LTS version for up to 10 years to ensure the stability of the system. 3) Performance significantly improves system response speed and resource utilization by optimizing kernel and system configuration.

MySQL is suitable for rapid development and small and medium-sized applications, while Oracle is suitable for large enterprises and high availability needs. 1) MySQL is open source and easy to use, suitable for web applications and small and medium-sized enterprises. 2) Oracle is powerful and suitable for large enterprises and government agencies. 3) MySQL supports a variety of storage engines, and Oracle provides rich enterprise-level functions.

The core components of Linux include kernel, file system, shell, user and kernel space, device drivers, and performance optimization and best practices. 1) The kernel is the core of the system, managing hardware, memory and processes. 2) The file system organizes data and supports multiple types such as ext4, Btrfs and XFS. 3) Shell is the command center for users to interact with the system and supports scripting. 4) Separate user space from kernel space to ensure system stability. 5) The device driver connects the hardware to the operating system. 6) Performance optimization includes tuning system configuration and following best practices.
