当前位置:网站首页>Basic operation of database

Basic operation of database

2020-11-07 21:04:18 haoaixia

1. Search single column

select name from students;

2. Search multiple columns

select name,age from students;

3. Alias the column name

select name as  full name  from students;

  Multiple column names are separated by commas

select name as  full name , age as  Age  from students;

4. Retrieve all columns

select * from students

5. In some query results , There are multiple data duplications , Sometimes you need to get rid of duplicate data , Use keywords distinct Deduplication

SELECT DISTINCT age FROM students;

6.limit Limit the number of result bars , Sometimes just look at the front 10 Data , Or look at a few days of data , Use limit Keywords can limit the results

select * from table_name limit 10;

  limit After the query

# Limit the result to 10 strip 
select id,name from students limit 10;

  

#limit 10,15 Says from the first 11 position ( That is the first. 10 There's a man behind you ) Start searching ,
#15 Presentation display 15 Data 
select id,name from students limit 10,15;

   notes : How to query the last 11 strip - Last but not least 20 Data

SELECT id,name FROM students
ORDER BY id DESC
limit 10,10

   Sorting data

By default, the query data is displayed in the order of inserting data . In real life , It is often necessary to sort some fields , stay sql of use order by You can specify fields to sort . For example, the following example :order by The default is asc, Sort from small to large

SELECT name,workyear from students ORDER BY workyear;
# according to workyear Sort , Sort from small to large by default 

  

SELECT name,workyear from students ORDER BY age;
# According to the database ,age Field to sort 

   If order by It's followed by two fields , First sort by the first field , In the case of the first field data , And then according to 2 Fields to sort

SELECT name,age workyear from students ORDER BY age,workyear;

   What are the top three oldest ages in the query table , Remove duplicate data

SELECT  DISTINCT age FROM students 
ORDER BY age DESC 
LIMIT 3

  Specify sorting direction

  DESC Keyword can sort the sorted data in reverse order , The specific way to use it is to add desc keyword

SELECT id,name FROM students ORDER BY id DESC

  

practice

 

 

1. Inquire about sql_score The name of the student with the highest score ,sql achievement ;

SELECT name,sql_score from score
ORDER BY sql_score DESC
LIMIT 1

2. Before query 10 name Pyhton Results and Sql The students with the highest scores , According to the first Python Sort the grades , Post press Sql Sort the grades ;  

SELECT * from score
ORDER BY python_score,sql_score DESC
limit 10

3. Inquire about python No 20 name - The first 30 Of the famous students sql achievement

SELECT name,sql_score from score
ORDER BY python_score DESC
LIMIT 19,11

  

 

 

版权声明
本文为[haoaixia]所创,转载请带上原文链接,感谢