Most common parts of a SELECT SQL query?

Posted by jnrbsn on Programmers See other posts from Programmers or by jnrbsn
Published on 2012-04-14T21:59:31Z Indexed on 2012/04/14 23:45 UTC
Read the original article Hit count: 214

Filed under:

I'm writing a function that generates a SELECT SQL query. (I'm not looking for a tool that already does this.) My function currently takes the following arguments which correspond to different parts of the SELECT query (the base table name is already known):

  • where
  • order
  • fields
  • joins
  • group
  • limit

All of these arguments will be optional so that the function generates something like this by default:

SELECT * FROM `table_name`

I want to order the arguments so that the most often used parts of a SELECT query are first. That way the average call to the function will use as few of the arguments as possible rather than passing a null value or something like that to skip an argument. For example, if someone wanted to use the 1st and 3rd arguments but not the rest, they might have to pass a null value as the 2nd argument in order to skip it.

So, for general purpose use, how should I order the arguments?

Edit:

To be more precise, out of the query parts I listed above, what is the order from most used to least used?

Also, I'm not looking for solutions that allow me to not have to specify the order.

Edit #2:

The "fields" argument will default to "*" (i.e all fields/columns).

© Programmers or respective owner

Related posts about sql