MySQL database_data query_basic query and conditional query

MySQL database

Data query

Basic query of data

Build database, data table

-- 创建数据库
create database python_test charset=utf8;

-- 使用数据库
use python_test;

-- students表
create table students(
    id int unsigned primary key auto_increment not null,
    name varchar(20) default '',
    age tinyint unsigned default 0,
    height decimal(5,2),
    gender enum('男','女','保密') default '保密',
    cls_id int unsigned default 0,
    is_delete bit default 0
);

-- classes表
create table classes (
    id int unsigned auto_increment primary key not null,
    name varchar(30) not null
);


Prepare data

-- 向students表中插入数据
insert into students values
(0,'小明',18,180.00,2,1,0),
(0,'小月月',18,180.00,2,2,1),
(0,'彭于晏',29,185.00,1,1,0),
(0,'刘德华',59,175.00,1,2,1),
(0,'黄蓉',38,160.00,2,1,0),
(0,'凤姐',28,150.00,4,2,1),
(0,'王祖贤',18,172.00,2,1,1),
(0,'周杰伦',36,NULL,1,1,0),
(0,'程坤',27,181.00,1,2,0),
(0,'刘亦菲',25,166.00,2,2,0),
(0,'金星',33,162.00,3,3,1),
(0,'静香',12,180.00,2,4,0),
(0,'郭靖',12,170.00,1,4,0),
(0,'周杰',34,176.00,2,5,0);

-- 向classes表中插入数据
insert into classes values (0, "python_01期"), (0, "python_02期");

  • Query all fields
select * from 表名;
例:
select * from students;

  • Query specified field
select 列1,列2,... from 表名;
例:
select name from students;

  • Use as to alias a field

select id as 序号, name as 名字, gender as 性别 from students;

  • You can alias the table through as
-- 如果是单表查询 可以省略表明
select id, name, gender from students;

-- 表名.字段名
select students.id,students.name,students.gender from students;

-- 可以通过 as 给表起别名 
select s.id,s.name,s.gender from students as s;

Deduplication (eliminate duplicate rows)

  • Use distinct before the column behind select to eliminate duplicate rows
select distinct 列1,... from 表名;
例:
select distinct gender from students;

Conditional query of data

  • Use the where clause to filter the data in the table, and rows with a result of true will appear in the result set

  • The syntax is as follows:

select * from 表名 where 条件;
例:
select * from students where id=1;
  • Support multiple operators behind where to process conditions
    • Comparison operator
    • Logical Operators
    • Fuzzy query
    • Range query
    • Null judgment

Comparison operator

* 等于: =
* 大于: >
* 大于等于: >=
* 小于: <
* 小于等于: <=
* 不等于: != 或 <> [<>:只在python2.x 可以作为‘不等于’]

Example 1: Query students whose number is greater than 3

select * from students where id > 3;

Example 2: Query students whose number is not greater than 4

select * from students where id <= 4;

Example 3: Query students whose name is not "Huang Rong"

select * from students where name != '黄蓉';

Example 4: Query students who have not been deleted

select * from students where is_delete=0;

Logical Operators

* and
* or
* not

Example 5: Query female students whose number is greater than 3

select * from students where id > 3 and gender=0;

Example 6: Query students whose numbers are less than 4 or have not been deleted

select * from students where id < 4 or is_delete=0;

Fuzzy query

  • like
  • % Means any number of any characters
  • _ Represents an arbitrary character
  • rlike regular matching

Example 7: Query a student whose surname is Huang

select * from students where name like '黄%';

Example 8: Query students whose last name is Huang and whose "first name" is one word

select * from students where name like '黄_';

Example 9: Query a student whose surname is Huang or Jing

select * from students where name like '黄%' or name like '%靖';

Example 10: regular matching query the name at the beginning of the week

select name from students where name like '^周.*';
select name from students where name like '^周.*伦$';

Range query

  • in means in a non-contiguous range

Example 11: Query students whose number is 1 or 3 or 8

select * from students where id in(1,3,8);
  • between… and… means in a continuous range

Example 12: Query students whose numbers are 3 to 8

select * from students where id between 3 and 8;

Example 13: Query boys whose numbers are 3 to 8

select * from students where (id between 3 and 8) and gender=1;

Null judgment

  • Note: null and `` are different
  • Is null

Example 14: Query students who did not fill in their height

select * from students where height is null;
  • Is not null is not null
    Example 15: Query students who filled in their height
select * from students where height is not null;

Example 16: Query the boy who filled in the height

select * from students where height is not null and gender=1;

priority

  • Descending order of priority 小括号is: not, 比较运算符, , 逻辑运算符.
  • andThan the orfirst operation, and if there is hope at the same time the first count or, requires a combination of ()use.

Guess you like

Origin blog.csdn.net/weixin_42250835/article/details/90370483