Summary of database Simple query

Source: Internet
Author: User

Basic classification of query operations for databases:
1. Projection Operation 2. Select Action 3. Sort operations


Projection operation:

SELECT column list from table name;
SELECT * from table name; (Query entire table)
SELECT column name as new column name from table name; (Query column displays new column name)
SELECT DISTINCT column name from table name; (excluding duplicate data)
SELECT CONCAT (column name 1, '-', column Name 2, '-', column name 3) as ' title ' from table name; (String connection)
SELECT column name from table name limit start ordinal, return row number; (return qualified row query)


Select action:

SELECT column name from table name WHERE condition;
SELECT column name from table name WHERE condition and condition;
SELECT column name from table name WHERE condition or condition;
SELECT COUNT (*) as ' title ' from table name WHERE condition; (statistic satisfies the number of conditions)
SELECT column name from table name WHERE condition between upper and lower bounds;
SELECT * FROM table name WHERE age in (11,13,14); (Find the specified range)
SELECT * FROM table name WHERE age not In (11,13,14) (Find out in the specified range);

SELECT column name from table name WHERE condition like ' _% '; (Fuzzy query)
Escape character '%50p%% ' escape ' P ' or '%50<escape char>%% ' or '%50[%% '
Not the latter two escaped MySQL

SELECT column name from table name WHERE column Name 1 is not NULL; (Query test column name 1 not empty)
SELECT column name from table name WHERE column name 1 is null; (Query test column name 1 is empty)


Sort operations:

SELECT column name from table name ORDER by column name ASC (ascending);
SELECT column name from table name ORDER by column name DESC (descending);


Execution Order of SQL:
1.FROM 2.WHERE 3.SELECT 4.ORDER by

--standard structure of simple query statements
--Select Column1,column2,column3 from t_name where Column1 > ORDER by column1 ASC; --desc

-Drop database demo;
--Create DATABASE demo;
-Use demo;
/*
CREATE TABLE Student (
ID int primary KEY auto_increment,
' Name ' varchar (255) is not NULL,
Age int,
Gender char (2),
Address varchar (255) Default ' Chengdu City '
) Charset=utf8;
*/
/*
INSERT into student (name, age, gender) values
(' Zhang San ', 17, ' Male '),
(' John Doe ', 19, ' Male '),
(' Harry ', 23, ' female '),
(' Type builded with ', 22, ' Male '),
(' David ', 30, ' male '),
(' Heshasha ', 22, ' female '),
(' Wang Dan ', 20, ' female '),
(' Liu Tangyuan ', 24, ' Male '),
(' In the Ocean ', 15, ' Male '),
(' Nie Fengtao ', 39, ' Male ');
*/
--Select Id,name,age,gender from student where the age >18 order by Age,name,id;

--Select ID as ' number ', name as ' student name ', age as ' old ', gender as ' gender ' from student;

--Select Age+10 as ' ten years later ', name as ' name ' from student;

--Update student Set gender = ' female ' where id = 4;

--select distinct age,gender from student; --Filter duplicate data, use combination match when multiple columns

--Select CONCAT (Name, '-', gender, '-', age) as ' student information ' from student;

--Select ID, name, age, gender from student limit 5;

--Select ID, name, age, gender from student limit 4, 2;

--select * FROM student where name! = ' Zhang San ' or age > 18;

--select * from student where age between and 22;

--select * from student where Age in (15,18,22);

--select * from student where age is not in (15,22);

--INSERT into student (name, age, gender, address) VALUES (' Search ', 80, ' Male ', ' Chengdu [');

--Select id,name,address from the student where address like '%a%% ' escape ' a ';

SELECT * FROM student where gender is null;

SELECT * from student where name is not null;

Summary of database Simple query

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.