2011-03-15 195 views
0

我有一个用户表,id,用户名和food_id列。这个想法是用户存储他们最喜欢的食物,我们拿出一个食品排行榜。我想为每种食物类型生成最高票数的报告。我正在使用MySQL和PHP。帮助MySQL查询

为了清楚起见,这里是表的例子:

id food_id username 
1 1   Bob 
2 100  Jane 
3 200  Andy 
4 1   Maggy 
5 100  Rich 
6 100  Mick 
7 1   Kevin 

我有一个查询,#2用户renegm'给了我。它给了我食物调查的结果。查询是:

select food_id, count(*) score 
    from myTable 
group by food_id 
order by score desc limit 100 

它完美地给了我结果为:

food_id score 
1   3 
100  4 

我意识到后,我得到了我得到food_ids而不是名称的答案。

我需要在食物表上进行连接。它看起来像

food_id food_name 
1   Salad 
100  Burgers 

如何将连接纳入上述查询?我看过我的书,但无法完成。

在此先感谢您的帮助。

干杯

丰富

回答

2
select f.food_id, 
     n.food_name, 
     count(f.food_id) score 
    from myTable 
    left join food_names n 
     on n.food_id = f.food_id 
group by f.food_id, 
     n.food_name 
order by score desc limit 100 
+0

感谢队友,我用你的代码和它的工作就像一个魅力!丰富 – Rich 2011-03-15 13:00:32

1

在这里你去:

 
select 
    myTable.food_id, 
    food.food_name, 
    count(myTable.id) score 
from 
    myTable 
    join food on (food.food_id = myTable.food_id) 
group by 
    myTable.food_id 
order by 
    myTable.score desc 
limit 100 
1
select 
    (SELECT food_name from food_table food where food.id = mt.food_id) foodName, 
    count(*) score  
from myTable mt 
group by food_id 
order by score desc limit 100 
2

你为什么不这样做,它使用触发器来维持收视率,所有您需要满足以下条件要做的是一个非常简单的查询来获得你想要的结果:

随着表的增长,此方法将更具性能。

希望它可以帮助

例子查询

select * from food order by rating desc; 
+---------+--------+-----------+-------------+--------+ 
| food_id | name | num_votes | total_score | rating | 
+---------+--------+-----------+-------------+--------+ 
|  1 | food 1 |   6 |   19 | 3.17 | 
|  3 | food 3 |   2 |   6 | 3.00 | 
|  2 | food 2 |   3 |   7 | 2.33 | 
+---------+--------+-----------+-------------+--------+ 
3 rows in set (0.00 sec) 

完整剧本

drop table if exists food; 
create table food 
(
food_id int unsigned not null auto_increment primary key, 
name varchar(255) not null, 
num_votes int unsigned not null default 0, 
total_score int unsigned not null default 0, 
rating decimal(8,2) not null default 0 
) 
engine = innodb; 

drop table if exists food_vote; 
create table food_vote 
(
food_id int unsigned not null, 
user_id int unsigned not null, 
score tinyint unsigned not null default 0, 
primary key (food_id, user_id) 
) 
engine=innodb; 

delimiter # 

create trigger food_vote_after_ins_trig after insert on food_vote 
for each row 
begin 
update food set 
    num_votes = num_votes + 1, 
    total_score = total_score + new.score, 
    rating = total_score/num_votes 
where 
    food_id = new.food_id; 
end# 

delimiter ; 

insert into food (name) values ('food 1'),('food 2'), ('food 3'); 

insert into food_vote (food_id, user_id, score) values 
(1,1,5),(1,2,4),(1,3,3),(1,4,2),(1,5,1),(1,6,4), 
(2,1,2),(2,2,1),(2,3,4), 
(3,1,4),(3,5,2); 
+0

哇,非常感谢您的详尽解答。如果网站发展到任何我将实施您的解决方案。队友的欢呼声 – Rich 2011-03-15 13:01:16

1

SELECT列名(S) FROM mytable的 INNER JOIN food_table ON myTable.food_ id = food_table.food_id

将连接两张表,前提是您的表名是food_table。通过

调整与所需的列名,排序和分组查询希望它有助于