mySQL Super-tip! Use LIMIT and OFFSET to paginate your SELECT statements!

| No Comments »

Awesome, it seems that mySQL now supports the OFFSET clause.

This is very useful for paginating your SQL statements.

The following will return the first 10 entries of your select statement:

“SELECT field FROM table LIMIT 10″

This, on the otherhand, will OFFSET your result by 10, selecting your entries #10-20:

“SELECT field FROM table LIMIT 10 OFFSET 10″

Here’s an example of how to capture entries #10-30:

“SELECT field FROM table LIMIT 20 OFFSET 10″

Things just keeps on getting easier and easier!


mySQL: Simplify your multiple WHERE queries with WHERE in

| No Comments »

_Problem #1_: How do I simplify my mySql where queries? _Problem #2_: How can I search through an array in my where statement?

Answer: An awesome trick when it comes to querying multiple where clauses is to use the WHERE <…> IN command.

$id = ’1,4,3,6,8,2′; “SELECT * FROM <…> WHERE id IN ($id)”

Oh and did I mention you can prepend the NOT statement to show results which is NOT included in the list?

$id = ’5,7′; “SELECT * FROM <…> WHERE id NOT IN ($id)”

Oh-Some!