我有2个表,表 ONE 由category_id
组成,其中每个表都有自己的唯一name
属性,每个product_id
在这个中都是唯一的表没有重复。
表 TWO 由唯一product_id
组成,就像表 ONE 和rank
一样,基于最大的amount
。
表: ONE
+--------+---------+------------+--------+
| brd_id | cat_id | product_id | name |
+--------+---------+------------+--------+
| 1 | 1 | 333 | w |
| 2 | 1 | 444 | w |
| 3 | 1 | 555 | w |
| 4 | 2 | 666 | y |
| 5 | 2 | 777 | y |
| 6 | 3 | 888 | t |
+--------+---------+------------+--------+
表: TWO
+--------+---------+------------+--------+--------+
| new_id | type | product_id | rank | amount |
+--------+---------+------------+--------+--------+
| 1 | all | 333 | 1 | 80 |
| 2 | all | 444 | 2 | 70 |
| 3 | all | 555 | 3 | 60 |
| 4 | all | 666 | 4 | 50 |
| 5 | all | 777 | 5 | 40 |
| 6 | all | 888 | 6 | 30 |
+--------+---------+------------+--------+--------+
我想要的是根据表 TWO 表中 ONE 的数量,仅从表 ONE 中显示name
ONE product_id
具有相同的cat_id
。
我能够基本直接从数据库中使用此查询,因此字段不同:
SELECT DISTINCT `cat`.`brand_name`
FROM `ps_product_brand` AS `cat`
INNER JOIN `ps_product_custom_statistics_weekly` AS `cat_p`
ON `cat_p`.`id_product` = `cat`.`id_product`
WHERE `cat_p`.`type` LIKE '%all%'
AND `cat_p`.`rank` <= 16
ORDER BY `cat_p`.`rank` ASC
问题
我得到重复的名字,所以我用了DISTINCT
,最后忽略了重复的值。
我想计算amount
表{strong> ONE 中的product_id
是否在表 TWO 并且表中的category_id相同的地方 +---------+--------+
| cat_id | name | amount
+---------+--------+
| 1 | w | 210
| 2 | y | 90
| 3 | t | 30
+--------+---------+
的 ONE
预期输出
var pieData = pie(data);
希望它有意义。致谢
答案 0 :(得分:1)
我没有小提琴可以使用,但我认为这是对的。
可能有拼写错误。
一般的想法是我们做独特的FIRST嵌套吧。然后,一旦我们获得了distinct,那么我们从辅助表中获得不同元素的SUM。我们这样做的原因是因为否则SUM(如果在内部完成)将连接多个记录并获得太高的数量。
events {
worker_connections 1024;
}
http {
server {
listen 80;
listen [::]:80;
server_name www.example.com example.com;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name www.example.com;
ssl_certificate "/etc/letsencrypt/live/example.com-0001/fullchain.pem";
ssl_certificate_key "/etc/letsencrypt/live/example.com-0001/privkey.pem";
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name example.com;
ssl_certificate "/etc/letsencrypt/live/example.com-0001/fullchain.pem";
ssl_certificate_key "/etc/letsencrypt/live/example.com-0001/privkey.pem";
location / {
proxy_pass http://127.0.0.1:5000;
}
}
}
答案 1 :(得分:1)
尝试以下方法:
SELECT `cat`.`brand_name`, `cat_p`.`amount`, SUM(`cat_p`.`amount`) AS `sum_amount`
FROM `ps_product_brand` AS `cat`
INNER JOIN `ps_product_custom_statistics_weekly` AS `cat_p` ON `cat_p`.`id_product` = `cat`.`id_product`
WHERE `cat_p`.`type` LIKE '%all%' AND `cat_p`.`rank` <= 16
GROUP BY `cat`.`brand_name`
ORDER BY `sum_amount` DESC, `cat`.`brand_name`
请注意重复项之间的金额与sum_amount之间的数字差异。您可能需要增加排名并可能将LIMIT添加到最后。