Using MySQL SELECT

Related Tutorials

Introduction

This tutorial aims to outline the usage of SQL SELECT queries, used for the retrieval of information from a requested database table. Previous knowledge of SQL would prove helpful, but fear not young dragon slayer, its not too hard to get to grips with in practice, so lets take a quick sneeky-peeky at what it is we're dealing with!

Know your enemy

If MySQL were a dragon, and SELECT was your damsel in distress, you'd probably whip out your sword and slay your fire breathing foe before making off with said damsel, right? No problemo? Well, perhaps... But wouldn't it be more luctrative if we could all play nice? Pay our old friend the dragon a visit every now and again, perhaps to salivate over the forbidden fruits your captured damsel possess? Well, as it happens, its actually fairly simple once you know what your up against.

Mythical creatures aside, the usage of SQL SELECT is a fairly simple affair. So lets come back to the real world and take a look at an example query string.

Code Snippet:

SELECT * FROM `table_name`

Taming the beast

Its that simple! As you can see from the above code snippet, the query string comprises of four values, each of which are used to identify what it is we want to retrive and where it is we should be looking to find it. The astrerix character '*' may look unusual, but this simply means 'all', so the query string is really saying; "SELECT ALL FROM table_name". The FROM segment should be fairly self explanitory, with the following value 'table_name' identiying which database table we want to search for the information we desire.

To write more concise queries, we can substitue the asterix wilcard from our query string and instead, provide the column names of the information we need. Writing queries this way helps to eleminate redundant data from any results returned by your requests. If your not intending on using all the results returned by using the asterix wildcard, include the names of your table columns as such;

Code Snippet:

SELECT id, first_name, last_name, age FROM `table_name`

Just what I wanted!

Hopefully, the ammendments to the code won't have shaken you up too much, it's going well so far, yes? Well, we've gone ahead and replaced our asterix '*' wildcard with the actual fields we're interested in retriving from our database table, namely; 'id', 'first_name', 'last_name' and 'age'. The last three fields are the names of the columns in our table that store the information we're trying to find. You may be wondering what this 'id' is all about? You may, or you may not. If you haven't already read the previous tutorial, i suggest you catch up before reading on. For now though, lets just say that the 'id' column will make life easier for both us and SQL.

Currently, our sql query should be returning all the rows of information currently stored inside our database table. Depending on the scale of your database content, this could be a few hundred, or a few thousand rows. Do we really need to find them all? What if we want to find the total amount of clown shoes sold by your online shoe emporium, or how many fake beards your employees wear to work on tuesdays? Lords and ladies, i introduce to you...

Code Snippet:

SELECT id, first_name, last_name, age FROM `table_name` WHERE age = '18'

What, Where?

Over here, cooee! Got your attention? Ok, lets continue, the end is drawing near. As you can see from our example query, not a lot has changed with the addition of the WHERE clause. Aside from outlining which columns we're looking for, we can also go that extra mile and point out which rows we want to find too. By careful use of the WHERE clause, we can set up boundaries to capture specific rows from our database. Our above example will simply search for all rows which satisfy the critera set by age = '18', ignoring those which do not fall within our search terms. More complex search terms can be added to refine the results returned more thoroughly, but that requires an entire tutorial within itself. So for now, i leave you with some extended examples to ponder upon... Thanks for reading!

Code Snippet:

SELECT id, first_name FROM `table_name` WHERE last_name != 'Brown'
SELECT id, age FROM `table_name` WHERE first_name = 'Zack' OR last_name 'Brown'
SELECT id, last_name, age FROM `table_name` WHERE age >= '18' AND age <= '21'

Related Tutorials

Related Comments