Fat Galah NewsFat Galah NewsFat Galah BlogFat Galah Client Login
Fat Galah Internet Design Studios
103 Burrendah Road, Jindalee, QLD

Local call: 1800 99 33 99 (Australia Only)
International Ph: +61 7 3715 5999
Ph: 07 37155999

Sorting variables that don't exists

March 16th, 2009

How to run a MySQL using variables that are not listed in your database tables. 

I have seen MySQL code that uses PHP to sort something that MySQL can do much quicker and then  just get PHP to display the results. MySQL has a great function called "HAVING". How this works with things like count, sum,  working out ages or other variables. Here is an example.

$str = sprintf("SELECT ci.*, SUM(ci.customer_sub_total) as customerTotal FROM customer_info GROUP BY ci.customer_id HAVING customerTotal > %d", 0);

$sql = mysql_query($str);

This will return only rows where the customerTotal is above 0. We can use the variable customerTotal and get HAVING to sort customers that have a customerTotal more then 0.

WARNING: Don't use HAVING with variables that really exists in your database tables.

 

Leave A Comment:

Name:(Required)
XHTML ValidationCSS Validation