使用PHP从MySQL数据库构建索引表

时间:2010-10-01 20:51:59

标签: php mysql indexing

使用MySQL和PHP;我正在尝试构建一个索引,其中包含table_1的平均值:

类型,名称,小时,日,月,年

所以我需要知道table_1中哪些值的组合,所以我知道在AVG()查询中要放什么。

我想要的是找出在比较表格中的以下行时可以做出的所有不同组合:

type
name
hour
day
month
year

以下是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

所以我最终得到的是:

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

如何格式化查询来执行此操作?

1 个答案:

答案 0 :(得分:1)

由于您只想要存在的组合,您只需运行此查询:

SELECT DISTINCT type, name, hour, day, month, year FROM table

这遍历所有行,对于表中存在的每个组合,该组合将在结果集中输出一次。

相关问题