Building an index table from a MySQL database with PHP
Using MySQL and PHP; I'm trying to build an index that contains the averages from table_1 grouped by:
type, name, hour, day, month, year
So I need to know which combination's of values are in table_1 so I know what to put in my AVG() queries.
What I want is to figure out all the different combination's that can be made when comparing the following rows in the table:
type
name
hour
day
month
year
Here's an example of table_1:
ID|type|name|location|amount|year|month|day_num|day|hour|minute|second
1|car|ben|1|1.00|2010|10|01开发者_运维技巧|Friday|03|05|45
1|car|bob|1|3.00|2010|10|01|Friday|04|05|45
2|cow|bob|2|2.00|2009|07|12|Sunday|09|10|12
2|cow|ben|2|4.00|2009|07|12|Sunday|10|10|12
So what I would end up with is:
type|name|year|month|day|hour
car|ben|2010|10|01|Friday|03
car|bob|2010|10|01|Friday|04
cow|bob|2009|07|12|Sunday|09
cow|ben|2009|07|12|Sunday|10
How would I format a query to do that?
Since you just want the combinations that exist, you can simply run this query:
SELECT DISTINCT type, name, hour, day, month, year FROM table
This goes through all of the rows, and for each combination that exists in the table, that combination will be output once in the result set.
精彩评论