mysql基础入门-单表查询

141 阅读2分钟

基础查询单列,多列,全部列。

select 

    name1,name2 or *

from

    table1

查询常量式,表达式,函数。

select

    100 or 100*4 or 函数() 

起别名(方式1用as 方式2用空格)

SELECT
    `salary` AS output
FROM
    `employees`

image.png

去重(使用关键字distinct)

SELECT
    DISTINCT `department_id`
FROM
    `employees`

image.png

查询实现不同的功能,需要用到不同的关键字,接下来整理了一下mysql查询的关键字大全。

selectfrom 查找

distinct 合并相同的项

limit /limit … offset 限制输出

order by 排序

order bydesc 排序,降序

where/ wherebetweenand … 条件判断输出

whereand 多条件,同时符合

whereor 多条件,符合其中一个

wherein 在某一个范围中

like 通配筛选

regexp 利用正则表达式筛选

concat 字段拼接

concat … as 字段拼接后重命名

desc 描述表中的字段信息

函数:

count avg max min sum 使用函数对列进行操作

group by 分组

having 分组后筛选

wheregroup byhaving 先筛选再分组再筛选

union 组合查询,多个select

union all 组合查询,不合并相同信息

Match Against 搜索,match字段,against内容

Insert into 插入

updateset 更新内容

delete from 删除

create 创建

alter tableadd/drop 增加/删除表字段

drop 删除表/

加号不起链接的作用,得使用关键字concat

SELECT
    CONCAT(`last_name` , `first_name`) AS NAME
FROM
    `employees`

image.png

条件查询

SELECT
	查询列表
FROM
	表名
WHERE
	筛选条件

分类:
(1)按条件表达式筛选 > <  or <>2)按逻辑表达式查询筛选 逻辑运算符 and or not
 (3)模糊查询 like ,between...and, in ,is null
 
 #查询...列中包含...字符 %为通配符 0个或多个  _为任意单个字符	转义字符\后面跟特殊字符。
 
SELECT
	*
FROM
	`employees`
WHERE
	`last_name` LIKE '%a%'
        
SELECT
	*
FROM
	`employees`
WHERE
	`salary` BETWEEN 10000 AND 12000


in 判断某字段的值是否属于in列表中的某一项

值列表要兼容。

使用inor 要简单
job_id in(值1,值2,值3)

空值的判断不能用等号或者不等号判断,需要使用 下面关键字判断。
is null (空值) is not null(非空值)