Print

Select syntax introduction:

Select command is used to acquire and return data from certain table. In this intro I will describe only the most elementary select statements and modifiers.

Select syntax is very self explanatory, but you must respect positioning of all modificators (where, limit, order by, etc). When I explain the details of using a modifier I always mention the positioning of the parameter within the query.

The most simple select statement looks like: 

select * from tablename

This will select all the data from table named tablename

To select only certain columns run:

select col1, col3 from tablename

You may wish to select only those columns that match certain criteria

select * from tablename where col1='string1'

In this example only rows with containing col1 that has the exact value of string1.

Sometimes you do not want to select every row. This is the case for limit modifier.

select * from tablename where col1='string1' limit 0,2

Limit is always placed at the end of the query. Limit can be used with other types of queries (delete, update, replace) and even there it has to be at the very end.

The first numbers telling the start position, the second stands for count of rows to be returned.

In this case you will get  the first two rows of the table with col1 containing value string1

select * from tablename where col1='string1' limit 2,1

This one will output the third row (start at position two and return one row)

If you want to order the output by values in certain columns:

select * from tablename where col1='string1' order by col0

By default mysql orders the data ascending (starting at the smallest value ), but by appending the desc modifier after the column name you can order it descending (starting at the greatest value).

select * from tablename where col1='string1' order by col0 desc

Hits: 1989