2011-03-15 151 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。通過

調整與所需的列名,排序和分組查詢希望它有助於