I “Group By” all the deals in my database by store. How can I show all the other deals who where not grouped after the query?
This is the query I’m using:
$query = mysql_query("SELECT * FROM deals WHERE DATE_FORMAT(expiration_date,'%Y-%m-%d %H:%i:%s') >= NOW() $cat ORDER BY deal_id ASC") or die(mysql_error());
On a slightly different note, a query like this to retrieve records based on date is bad.
What is the format of your expiration date? Given that you are passing it to DATE_FORMAT I would assume it is a DATE or DATETIME type. Wrapping your
expiration_datein DATE_FORMAT means the optimiser cannot use an index on that field, forcing a full table scan. This is bad news!You will get the same result with the DATE_FORMAT removed –
and now the optimiser will be able to use an index that contains expiration_date as the first field in the index.
The fact that you have a field called
cat1leads me to think that you may have other category fields. Again, this is bad news. Repeating groups across columns are not your friend. They make all sorts of things harder than they need to be and they are inefficient. Have a read of this article on First Normal Form.