Hudzilla.org - the homepage of Paul Hudson
Contents > Databases > SQL Wish List | Report Bug | About Me ]

9.3.7     Selecting data

This is NOT the latest copy of this book; click here for the latest version.

The next step is being able to extract data from your database - this uses the "SELECT" SQL command. Here is a basic example:

SELECT ID, LastName FROM usertable;

SELECT takes the form, "SELECT field1 , field2 , ... FROM table ;" as you can see above. You can select as many (or as few) fields as you like. To select all fields from a table, use "SELECT * FROM table ". As your skills progress with SQL, you can also do more complicated data queries like selecting multiple records from across different data sets, perform functions on data before its returned, and such.

At this point you know enough to select some data from the usertable table you created and added data to. Type these three queries in, pressing Enter between each one to see the results:

SELECT ID FROM usertable;
SELECT LastName, FirstName, Age FROM usertable;
SELECT * FROM usertable;

Here is what MySQL should show you for the output of the last query:

mysql> SELECT * FROM usertable;

+------+-----------+----------+------+
| ID   | FirstName | LastName | Age  |
+------+-----------+----------+------+
| 1    | Jack      | Black    | 29   |
+------+-----------+----------+------+
1 row in set (0.00 sec)

mysql>

Each of the other queries should show the requested data, and will be different to each other. There are three key things to note here:

  1. MySQL draws each field with its own header so you can see whets in there. This becomes very important later on, particularly when you start running functions on data.

  2. MySQL shows all fields in the order you SELECTed them.

  3. If you run SELECT *, MySQL selects all fields from the table, then shows them in the order they were created in the CREATE TABLE call.

SELECT is probably the most complex SQL statement of them all - we will be covering much more of its power later on. Right now, there is just one last thing you need to learn before you can continue: how to limit your result. SELECT has a number of "clauses" you can add to it that affect the data selected. One of them is "WHERE", and it allows you to force MySQL to return only rows that match certain criteria.

Our test entry in the database has an Age of 29, so what do you think this next query will do?

SELECT * FROM usertable WHERE Age > 30;

Naturally it will look through all of usertable, checking the Age values against 30, and only return rows where the Age field is greater than 30. If you try that on your database, you will find MySQL returns no rows as expected. Now try this:

SELECT * FROM usertable WHERE Age < 30;

This time Jack Black should be back in the rows returned, because his record matches the WHERE clause we specified. You can provide much more specific WHERE clauses, such as "WHERE ID = 1", or "WHERE FirstName= 'Jack' ".

The other popular clause is "LIMIT", which allows you to force MySQL to only return a certain number of results. LIMIT can be used in two ways: "LIMIT n " and "LIMIT m , n ". The first way allows you force MySQL to only return the first n rows that match your WHERE clause (if you have one), whereas the second option allows you to force MySQL to return the first n rows after the first m that match.

If we had hundred rows of matching data, here are three possible SELECT statements:

SELECT * FROM SomeTable LIMIT 1;
SELECT * FROM SomeTable LIMIT 10;
SELECT * FROM SomeTable LIMIT 20, 10;

The first one will return just the first row, the second one will return the first ten rows, and the last one will skip the first twenty rows, and return the next ten. Each type of LIMIT has its own use - the last option, for example, allows you to do search engine-style results, "See next ten matches".

Author's Note: Some databases, most notably Microsoft SQL Server, use the syntax "SELECT TOP 5 * FROM table;" as opposed to "SELECT * FROM table LIMIT 5;". You will need to read your database documentation to determine the correct usage for your system.





<< 9.3.6 Inserting data   9.3.8 Extra SELECT keywords >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
jente@jentech.be - 29 Aug 2008

Yes you can use and in mysql queries. But you are stating "==" while it is "=" that's needed... Only one will do =).

>select * from coffeetable where race='orc' and class='warrior'

would do the trick.

A PHP User - 29 Aug 2008

I love this site... but I'll agree that we need more examples.

I just want a simple example, perhaps: putting a variable into MySQL and then bringing it back into PHP.

Dopple - 29 Aug 2008

I'll second that. There should be more example on using PHP with SQL also.

A PHP User - 29 Aug 2008

this site is starting to get frustrating. can you use AND in sql queries?

> SELECT * FROM coffeetable WHERE race == 'orc' AND class == 'warrior'


There is so much information on this site. yet SO LITTLE AT THE SAME TIME! This is the entire usage of select? this should be a 6 page document at least, filled with really clever examples of how to use select.

A PHP User - 29 Aug 2008

whets > what's

A PHP User - 29 Aug 2008

spelling mistake! :p



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is eight plus ten?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow