Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize select simple query in SQL

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to achieve select simple query in SQL, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Prepare the data:

/ * create a new student form stu*/

Create table stu (

Id int not null PRIMARY key auto_increment comment' primary key'

Name varchar (12) comment' name'

Age varchar (12)

)

/ * insert data * /

Insert into stu (id,name,age) values

(1001)

(1002)

(1003, recording, etc.)

(1004 people jackhammer 19)

I. SELECT statement

Using select to query table data, you must give at least two pieces of information-- what you want to choose and where to choose.

# 1. Query a single column:

Select id from stu

Unsorted data if you do not explicitly sort the query results, you may find that the order of the displayed output is different from that of the original table, and the order of the returned data has no special significance, either in the order in which the data was added to the table, or not. It is normal as long as the same number of rows are returned.

End SQL statement multiple SQL statements are separated by semicolons (;).

SQL statements and case SQL statements are not case sensitive, and SELECT is the same as select. Similarly, it doesn't matter if you write it as Select. Many SQL developers prefer to use uppercase for all SQL keywords and lowercase for all column and table names, making the code easier to read and debug.

Using spaces and blank lines when processing SQL statements, all spaces are ignored. SQL statements can be given on one line or split into many lines, and most SQL developers find it easier to read and debug SQL statements into multiple lines.

# 2. Query multiple columns: query the values of two columns of id,name in the student table

Select id,name from stu

Multiple column names are given after the select keyword, separated by commas, and no commas are added to the last column name.

# 3. Query all columns: query the values of all columns in the student table

Select * from stu

It is common to use * wildcards, and it is best not to use * wildcards unless you really need each column in the table. It is not necessary to explicitly list the required columns, but retrieving unwanted columns usually degrades the performance of the retrieval and application.

Retrieving unknown columns does not specify a column name explicitly (because the asterisk retrieves each column), so columns with unknown names can be retrieved.

# 4. Query different rows (distinct to remove duplicates): query the student table for the ages of all students

Select distinct age from stu

Using the DISTINCT keyword, it must be placed directly before the column name.

You cannot partially apply the DISTINCT DISTINCT keyword to all columns, not just the columns that precede it, unless the two specified columns are different, or all rows will be retrieved.

# 5.1 restriction result (limit pagination): query the student ID of the first four rows of the student table

Select id from stu limit 4

# 5.2 restriction result (limit mfocus n: n rows from line m, m in the first line is 0): query the ID of the last three students in the student table

Select id from stu limit 1,3

LIMIT with a value always starts at the first row, and the number given is the total number of rows. A LIMIT with two values can be specified to start at the location where the line number is the first value.

Line 0 starts the first behavior retrieved from row 0 instead of line 1. Therefore, LIMIT 1, 1 will retrieve the second row instead of the first row.

When the number of rows is not enough, the number of rows specified in LIMIT is the maximum number of rows to be retrieved. If there are not enough rows (for example, LIMIT 10,5, but only 13 rows are given), MySQL will return only as many rows as it can return.

The above is all the contents of the article "how to implement select simple query in SQL". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report