简体   繁体   English

如何从SQL中的字段计算相同的评分

[英]how to count same rating from field in sql

I have a problem counting ratings in SQL. 我在计算SQL中的评分时遇到问题。 This is what my data looks like: 这是我的数据:

data 数据

 CREATE TABLE `restaurant` (
  `id_restaurant` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id_restaurant`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

insert  into `restaurant`(`id_restaurant`,`name`) values (1,'Mc Donald');
insert  into `restaurant`(`id_restaurant`,`name`) values (2,'KFC');

    CREATE TABLE `user` (
  `id_user` int(11) NOT NULL AUTO_INCREMENT,
  `userName` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id_user`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

insert  into `user`(`id_user`,`userName`) values (1,'Audey');


    CREATE TABLE `factors` (
  `factor_id` int(11) NOT NULL AUTO_INCREMENT,
  `factor_clean` int(11) NOT NULL DEFAULT '0',
  `factor_delicious` int(11) NOT NULL DEFAULT '0',
  `id_restaurant` int(11) DEFAULT NULL,
  `id_user` int(11) DEFAULT NULL,
  PRIMARY KEY (`factor_id`),
  KEY `id_restaurant` (`id_restaurant`),
  KEY `id_user` (`id_user`),
  CONSTRAINT `factors_ibfk_1` FOREIGN KEY (`id_restaurant`) REFERENCES `restaurant` (`id_restaurant`),
  CONSTRAINT `factors_ibfk_2` FOREIGN KEY (`id_user`) REFERENCES `user` (`id_user`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

    insert  into `factors`(`factor_id`,`factor_clean`,`factor_delicious`,`id_restaurant`,`id_user`) values (1,1,5,1,1);
insert  into `factors`(`factor_id`,`factor_clean`,`factor_delicious`,`id_restaurant`,`id_user`) values (2,0,5,1,1);
insert  into `factors`(`factor_id`,`factor_clean`,`factor_delicious`,`id_restaurant`,`id_user`) values (3,1,5,1,1);
insert  into `factors`(`factor_id`,`factor_clean`,`factor_delicious`,`id_restaurant`,`id_user`) values (4,3,3,1,1);

And the result should be like this, Show all ratings (1,2,3,4,5) and their count from the fields rating_clean , rating_delicious , and rating_clean 结果应该是这样的,显示所有评级(1,2,3,4,5)及其来自rating_cleanrating_deliciousrating_clean字段的计数

在此处输入图片说明

Thanks for your help. 谢谢你的帮助。

but the result i get 但是我得到的结果

SELECT COUNT(`factor_clean`+`factor_delicious`),'1' AS rating_1 FROM `factors` WHERE 1 GROUP BY `id_restaurant`

result not should like this 结果不应该这样

the result should not like that, my question is, how to select just factor_clean and factor_delicious where factor_clean =1 and factor_delicious = 1 结果不应该这样,我的问题是,如何只选择factor_clean和factor_delicious,其中factor_clean = 1和factor_delicious = 1

Use union all to unpivot the data and then aggregate: 使用union all取消透视数据,然后进行汇总:

select id_restaurant, rating, count(*)
from ((select r.id_restaurant, r.rating_clean as rating, r.date
       from ratings r
      ) union all
      (select r.id_restaurant, r.rating_delicious, r.date
       from ratings r
      ) union all
      (select r.id_restaurant, r.rating_clean2, r.date
       from ratings r
      ) 
     ) r
group by id_restaurant, rating
order by id_restaurant, rating;

For example this is solution for table with colums rating_delicious and rating_clean (only one!): 例如,这是针对具有colums rating_delicious和rating_clean(仅一个!)的表的解决方案:

First of all you should create additional table, I called it factors: 首先您应该创建其他表,我将其称为因素:

CREATE TABLE `factors` (
 `factor_id` int(11) NOT NULL AUTO_INCREMENT,
 `factor_clean` int(11) NOT NULL DEFAULT '0',
 `factor_delicious` int(11) NOT NULL DEFAULT '0',
 PRIMARY KEY (`factor_id`)
)

Next add two records: 接下来添加两条记录:

INSERT INTO `factors` (`factor_id`, `factor_clean`, `factor_delicious`) VALUES (NULL, '1', '0'), (NULL, '0', '1');

Now you can join this tables and get results: 现在,您可以加入此表并获取结果:

SELECT x.id_restaurant
     , (x.rating_clean * f.factor_clean) + (x.rating_delicious * f.factor_delicious) AS rating
     , count(*) 
  FROM your_table x
  JOIN factors f
 WHERE 1 
 GROUP 
    BY x.id_restaurant
     , rating

In order to use next colum ( rating_third ), you should and column factor_third to factors , insert new row with 1 in this column and finally add something like your_table.rating_third*factors.factor_third to sum in SELECT 为了使用下一科拉姆( rating_third ),你应该和列factor_thirdfactors ,插入新行1在此列,最后加入类似your_table.rating_third*factors.factor_third在总结SELECT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM