Contact Our Development Team
Free Code Tutorials & Open Source Code
MySQL Queries - Where
Tutorials > MySQL > Queries - Where
Selecting what you want
Now that you have seen how you can store and retrieve data in MySQL databases, you might be wondering how you can get back specific data. Luckily, you don't have to use PHP to filter through your data (especially since you might end up with database tables which have millions of rows), but you can use the WHERE clause in your SQL statement to control which data is returned
First, we'll add some more data to our customers table so that we can demonstrate this. We are going to leave out the PHP sections of the code from now on, unless we introduce something new, so that you can see what is happening with the SQL
INSERT INTO `customers`(`taxid`, `name`, `address`) VALUES (`789012`, `Daniel Davidson`, `45 Made Up Road`)
INSERT INTO `customers`(`taxid`, `name`, `address`) VALUES (`654321`, `Tom Thompson`, `1 Not Real Close`)
INSERT INTO `customers`(`taxid`, `name`, `address`) VALUES (`135792`, `Eddie Ericson`, `975C Imaginary Drive`)
Now we can use SELECT with WHERE to get specific sets of data from our table. For example, if we have the taxid "654321" and we want to know who it belongs to, we could use the MySQL query:
SELECT `name` FROM `customers` WHERE `taxid` = '654321'
Tom Thompson
You can also use '>' and '<' to compare numerical columns:
SELECT `address` FROM `customers` WHERE `customerid` < 3
123 Fake Street 45 Made Up Road
Page Responses
Currently there have been no responses to this page...
If you have anything to contribute to this tutorial, found a bug, or know a better way of achieving the same goal, please leave your response below.
     
Copyright ©2009, Wired IDS Ltd. | Licensed under Creative Commons Attribution Share-Alike | Load time: 0.3062 seconds