vertopal.com_02.Retrieve-Data
vertopal.com_02.Retrieve-Data
Retrieve-Data
2 Retrieving Data
In this lesson, you'll learn how to use the SELECT statement to retrieve one or more
columns of data from a table.
• Analysis
The previous statement uses the SELECT statement to retrieve a single column
called prodname from the Products table. The desired column name is specified right
after the SELECT keyword, and the FROM keyword specifies the name of the table
from which to retrieve the data. The output from this statement is shown in the
following:
SELECT prod_name
FROM Products;
SELECT
prod_name
FROM
Products;
Most SQL developers find that breaking up statements over multiple lines makes
them easier to read and debug.
The SELECT statement returned 14 rows (even though there are only four vendors in that
list) because there are 14 products listed in the products table. So how could you retrieve a
list of distinct values?
The solution is to use the DISTINCT keyword which, as its name implies, instructs the
database to only return distinct values.
select distinct vend_id from Products;
SELECT DISTINCT vendid tells the DBMS to only return distinct (unique) vendid rows, and so
only four rows are returned, as seen in the following output. If used, the DISTINCT keyword
must be placed directly in front of the column names.
Caution: Can't Be Partially DISTINCT
The DISTINCT keyword applies to all columns, not just the one it precedes. If you
were to specify SELECT DISTINCT vend\id, prod\price, all rows would be retrieved
unless both of the specified columns were distinct.
Limiting Results
SELECT statements return all matched rows, possibly every row in the specified table.
What if you want to return just the first row or a set number of rows? This is doable, but
unfortunately, this is one of those situations where all SQL implementations are not
created equal.
If you are using MySQL, MariaDB, PostgreSQL, or SQLite, you can use the LIMIT clause, as
follows:
select prod_name from Products limit 5;
prodname
Fish bean bag toy
Bird bean bag toy
Rabbit bean bag toy
8 inch teddy bear
12 inch teddy bear
The previous statement uses the SELECT statement to retrieve a single column. LIMIT 5
instructs the supported DBMSs to return no more than five rows. The output from this
statement is shown in the following code.
To get the next five rows, specify both where to start and the number of rows to retrieve,
like this:
select prod_name from Products limit 5 offset 5;
prodname
18 inch teddy bear
Raggedy Ann
King doll
Queen doll
LIMIT 5 OFFSET 5 instructs supported DBMSs to return five rows starting from row 5. The
first number is where to start, and the second is the number of rows to retrieve. The output
from this statement is shown in the following code:
So, LIMIT specifies the number of rows to return. LIMIT with an OFFSET specifies where to
start from. In our example there are only nine products in the Products table, so LIMIT 5
OFFSET 5 returned just four rows (as there was no fifth).
Caution: Row 0
The first row retrieved is row 0, not row 1. As such, LIMIT 1 OFFSET 1 will
retrieve the second row, not the first one.
Tip: MySQL and MariaDB Shortcut
MySQL and MariaDB support a shorthand version of LIMIT 4 OFFSET 3, enabling
you to combine them as LIMIT 3,4. Using this syntax, the value before the , is the
LIMIT and the value after the , is the OFFSET.
Note: Not ALL SQL Is Created Equal
I included this section on limiting results for one reason only, to demonstrate that
while SQL is usually quite consistent across implementations, you can't rely on it
always being so. While very basic statements tend to be very portable, more
complex ones tend to be less so. Keep that in mind as you search for SQL solutions
to specific problems.
Using Comments
As you have seen, SQL statements are instructions that are processed by your DBMS. But
what if you wanted to include text that you'd not want processed and executed? Why
would you ever want to do this? Here are a few reasons:
• The SQL statements we've been using here are all very short and very simple. But, as
your SQL statement grow (in length and complexity), you'll want to include
descriptive comments (for your own future reference or for whoever has to work on
the project next). These comments need to be embedded in the SQL scripts, but they
are obviously not intended for actual DBMS processing. (For an example of this, see
the create.sql and populate.sql files used in Appendix B).
• The same is true for headers at the top of SQL file, perhaps containing the
programmer contact information and a description and notes. (This use case is also
seen in the Appendix B .sql files.).
• Another important use for comments is to temporarily stop SQL code from being
executed. If you were working with a long SQL statement, and wanted to test just
part of it, you could comment out some of the code so that MariaDB saw it as
comments and ignored it.
Most DBMSs supports several forms of comment syntax. We'll Start with inline comments:
SELECT prod_name -- this is a comment FROM Products;
Comments may be embedded inline using – (two hyphens). Anything after the – is
considered comment text, making this a good option for describing columns in a CREATE
TABLE statement, for example.
Here is another form of inline comment (although less commonly supported):
# This is a comment
SELECT prod_name
FROM Products;
A # at the start of a line makes the entire line a comment. You can see this format comment
used in the accompanying create.sql and populate.sql scripts.
You can also create multi line comments, and comments that stop and start anywhere
within the script:
/* SELECT prod_name,
vend_id FROM Products; */
SELECT prod_name FROM Products;
/* starts a comments, and */ ends it. Anything between /* and */ is comment text. This
type of comment is often used to comment out code, as seen in this example. Here, two
SELECT statements are defined, but the first won't execute because it has been commented
out.
Summaries
In this lesson, you learned how to use the SQL SELECT statement to retrieve a single table
column, multiple table columns, and all table columns. You also learned how to return
distinct values and how to comment your code. And unfortunately, you were also
introduced to the fact that more complex SQL tends to be less portable SQL. Next you'll
learn how to sort the retrieved data.
Django 查找的是精确的条目
总结: select, retrieve individual, multiple, all