mysql_ common operating statement

personal blog


Database operations

Command line access to databases: mysql -u username -p password

Show Database: show databases

New Database: create database databaseName

Delete the database: drop databse databaseName

Select the database: use databaseName

View all data tables: show tables

View table structure: desc tablesName


Data Manipulation

增:insert into table (`field1`, `field2`...) values ('value1','value2'...)

删:delete from table where field1=value1

改:update tablename SET field1=value2 where field1=value1


check:

Query the user table all the data:

select * from user;

where conditions of inquiry:

select * from user where id = 1002

as query field Rename:

select name as 名字 from user where id = 1002

Sort by :

select * from user order by age, id asc

age and positive sequence id field arrangement (high priority field age)

select * from user order by id, age desc

age and arranged in order to id field (field id high priority)


Skip to limit the query data:

select * from user where id>0 limit n

Front of the display data of n

select * from user where id>0 limit m,n

Skip ahead and m show the next n data


Fuzzy query:

like +% is used (without%, 'like' equal to '=')

field contains the name 'ven'

select * from user where name LIKE "%ven%"

Lee name field begins with:

select * from user where name LIKE "Lee%"

the end of the name field Jun:

select * from user where name LIKE "%Jun"


Range query :

in the statement :( query data idzai () in)

select * from user where id in (1001,1002,1003)

between ... and statements: (a query id range 1001-1010 data)

select * from buser where id between 1001 and 1010


polymerization:

Statistical number of records: count (fidle)

select count(id) where id>1001

Summing a field: sum (fidle)

select sum(age) where id>1001

Field seeking average : avg (fidle)

select avg(age) where id>1001

Seeking a maximum value : max (fidle)

select max(age) where id>1001

Seeking the minimum value : min (fidle)

select min(age) where id>1001


UNION operator:

Results for SELECT statements are connected to two or more combinations of a set of results. More SELECT statements will delete duplicate data

select id,title from articles union select id, title from books order by id


Grouping query results: group by

On the name field grouping and statistics of the total number of fields of age

select name,count(age) from employee_tbl group by name

On the name field and a statistical average age grouping fields

select name,avg(age) from employee_tbl group by name

Statistics on the name field of the packet and the maximum age field

select name,max(age) from employee_tbl group by name

Statistics on the name field and a minimum age grouping fields

select name,min(age) from employee_tbl group by name

Field Grouping of name and age fields of statistics and

select name,sum(age) from employee_tbl group by name

having query

Field name and the field conditions and the statistical grouping of singin name <> 'Li'

select name,sum(singin) from employee_tbl group by name HAVING name <> '丽'

Note:

wherehaving

    where and having keywords used to set conditional expressions to filter query results, the difference is behind having polymerization can be followed

    Function, and where not, are usually used in conjunction with having the keywords group by, the data represents packet filter


Connect using:

The connector (INNER JOIN): Get record matching the fields in the relationship between two tables

Left connecting (LEFT JOIN): Get the left table all records, even if no record corresponding to the right table matching.

Right connection (RIGHT JOIN): LEFT JOIN contrast with, for acquiring the right table all records, even if no record corresponding to the left table matching.

Data table is as follows:

The connecting use:

A query display table id, title and author and table id field b, a query for the table ID field = b table ID field

select a.id,a.title,a.author, b.id from books a inner join articles b on a.id = b.id

Left connect using:

Query left table ID field (Books) in id, title field and autho right table (Articles) content and the id field, the query condition is left = right table of the table ID field

select a.id,a.title,a.author, b.id,b.content from books a left join articles b on a.id = b.id

Note: the left connecting the left reads all the data of the data table, even if no corresponding data table on the right, as follows

The right connection to use:

Query left table ID field () in id, title fields, query left = right table of the table ID field

select a.id, a.content,b.id,b.title,b.author from articles a right join books b on a.id = b.id

Note: all data to the right of the right connection data table will be read, even if no data corresponding to the left side of the table; following

 

Published 59 original articles · won praise 19 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_43507959/article/details/96722755