These queries are for demographics. Each query is for an individual demographic that considers all the POSTed selections that aren't itself.
For example, if there were only 3 qualities, and the user had already chosen that "gender" could be "m", and "age" could be "25" to "49", I would do the following:
PHP Code:
//Primary query
$customeridquery = mysql_query( "SELECT `customerid` FROM `invoices` WHERE `invoicedate` BETWEEN '...' AND '...'" );
//this is the set of 'customerids' to which all future queries are restricted
//3 demographic queries
$agequery = mysql_query( "SELECT `age`, count( `age` ) FROM `customers` WHERE `customerid` IN ( '$customeridstring' ) AND `gender` = 'M' GROUP BY `age`" );
$genderquery = mysql_query( "SELECT `gender`, count( `gender` ) FROM `customers` WHERE `customerid` IN ( '$customeridstring' ) AND `age` BETWEEN '25' AND '40' GROUP BY `gender`" );
$locationquery = mysql_query( "SELECT `location`, count( `location` ) FROM `customers` WHERE `customerid` IN ( '$customeridstring' ) AND `gender` = 'M' AND `age` BETWEEN '25' AND '40' GROUP BY `location`" );
//each query is restricted by everything but itself
//"age" is restricted by the initial $customeridquery and by the selected gender
//this way if the user wants to change their "age" values, they can see how many
//records are available for other existing "age" values within this same set of
//POSTed selections
I have considered what I think you're implying, which is select all the demographics from one big query
PHP Code:
SELECT `age`, count( `age` ), `gender`, count( `gender` ), ...
FROM `invoices` LEFT JOIN `customers` USING ( `customerid` )
WHERE `invoicedate` ... AND `age` ... AND `gender` ...
However I could not figure out how to properly group this since the demographics are independant of each other. Also, the result set would be restricted to already selected values. I want the user to see what the selection would look like before they added 'F' to their 'gender' selection without having to add it to see what changed. With this query they would only get a count of 'M'.