MySQL specified field query

Specify field query

SELECT syntax

SELECT [ALL | DISTINCT]
{
   
   * | table.* | [table.field1[as alias1][,table.field2[as alias2]][,...]]}
FROM table_name [as table_alias]
  [left | right | inner join table_name2]  -- 联合查询
  [WHERE ...]  -- 指定结果需满足的条件
  [GROUP BY ...]  -- 指定结果按照哪几个字段来分组
  [HAVING]  -- 过滤分组的记录必须满足的次要条件
  [ORDER BY ...]  -- 指定查询记录按一个或多个条件排序
  [LIMIT {
   
   [offset,]row_count | row_countOFFSET offset}];
   -- 指定查询的记录从哪条至哪条

Note: [] brackets represent optional, {} brackets represent required

grammar: select 字段, 字段, .... from 表名

-- 查询全部学生
-- SELECT 字段 FROM 表名
SELECT * FROM `student`

-- 查询指定字段
SELECT `name`, `pwd` FROM `student`

-- 给查询结果起名字 -- as 可以给字段和表起别名
SELECT `name` AS 学生姓名, `pwd` AS 学生密码 FROM `student` AS 学生表

-- 函数 Concat(a,b) 拼接a和b
SELECT CONCAT('密码:', `pwd`) AS 新密码 FROM `student`

AS aliases- 旧字段名 AS 新字段名,旧表名 AS 新表名



Deduplication distinct

-- 查询所有数据
SELECT `name` FROM student
-- 去重
SELECT DISTINCT `name` FROM student


Database column (expression)

  • select 表达式 from 表名
-- 查看系统版本 (函数)
SELECT VERSION()

-- 计算 (表达式)
SELECT 100 - 1 AS 结果

-- 查询自增的步长 (变量)
SELECT @@auto_increment_increment

-- 查询出所有数据 + 1 查看
SELECT `name` + 1 AS 所有结果加1 FROM student

Guess you like

Origin blog.csdn.net/weixin_44953227/article/details/108737510