简体   繁体   English

如何对联结表中的数据进行计数和排序

[英]How to count and sort data in junction table

I have tables with many to many relationship: 我的表之间存在多对多关系:

Parameters 参量

+-------------------+-----------------------+
| Field             | Type                  |
+-------------------+-----------------------+
| id                | int(11)               |
| name              | varchar(20)           |

Product 产品

+-------------------+-----------------------+
| Field             | Type                  |
+-------------------+-----------------------+
| id                | int(11)               |
| name              | varchar(20)           |

Parameters_Product 参数_产品

+-------------------+-----------------------+
| Field             | Type                  |
+-------------------+-----------------------+
| id_parameters     | int(11)FK             |
| id_product        | int(11)FK             |

So, product may have some parameters, parameters may relates to products. 因此,产品可能具有一些参数,参数可能与产品有关。 When I choose several parameters I need to output the sorted names of products which contains checked parameters, but sorting must be based on the number of match parameters, but more than one. 当我选择多个参数时,我需要输出包含已检查参数的产品的排序名称,但是排序必须基于匹配参数的数量,但不能超过一个。

Example: 例:

Parameters_Product 参数_产品

+---------+----------+
|id_param |id_product|
+--------------------+
| 1       | 1        | 
| 2       | 1        | ----> Product#1
| 3       | 1        | 
| 4       | 1        | 
----------------------
| 1       | 2        |
| 2       | 2        |
| 6       | 2        | ----> Product#2
| 4       | 2        |
| 9       | 2        |
----------------------
| 5       | 3        |
| 7       | 3        | ----> Product#3
| 1       | 3        |

Client chooses Id_params: 1,2,6,9. 客户选择Id_params:1、2、6、9。

Result in order: 结果如下:

Product#2 -> 4 matches
Product#1 -> 2 matches
Product#3 -> 1 matches (doesn't outputted)

I did it in php with terrible code, but I think it can be resolved easier. 我在php中使用了糟糕的代码,但是我认为它可以更轻松地解决。

How to do this in SQL? 如何在SQL中执行此操作?

you have to group by id_product field, filter single results with id_param values and then order by the id_param count. 您必须按id_product字段分组,使用id_param值过滤单个结果,然后按id_param计数排序。 like this: 像这样:

SELECT name
FROM Parameters_Product
 INNER JOIN Parameters ON id_product = id
WHERE id_param IN (1,2,6,9)
GROUP BY id_product
HAVING COUNT(id_param) > 1
ORDER BY COUNT(id_param) desc

If you need just id_product: 如果只需要id_product:

select id_product,
count(id_param) as matches 
from Parameters_Product
where
id_param in (1,2,6,9)
group by id_product
having count(id_param) >1
order by matches desc;

If you need Product Name: 如果您需要产品名称:

select p.name,count(pp.id_param) as matches 
from 
Product p
join Parameters_Product pp
on p.id=pp.id_product
where
pp.id_param in (1,2,6,9)
group by p.name
having count(pp.id_param) >1
order by matches desc;

OR 要么

select p.name,pp.matches from Product p
join
(select id_product,
count(id_param) as matches 
from Parameters_Product
where
id_param in (1,2,6,9)
group by id_product
having count(id_param) >1) pp
on p.id = pp.id_product
order by pp.matches desc;

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

相关问题 将数据插入联结表 - Inserting data into a junction table 如何自动更新联结表并从联结表访问数据? - How i can update junction table automatically and access data from junction table? 如何在更新操作中获取多选复选框数据并将其保存在联结表中 - How to get multiselect checkbox data in update action and save it in junction table 如何将值插入联结表? - How to insert values into a junction table? 如何向联结表添加行为? - How to add behaviour to a junction table? 从Yii2中的联结表中检索数据 - Retrieve data from junction table in Yii2 如何 SELECT 查询包括联结表 - How to SELECT query including a junction table 如何从联结表中检索值作为字符串? - How to retrieve values from junction table as a String? 如何在 Laravel 连接表中获取列 - How to get column in Laravel junction table 如何排序<div id="text_translate"><p>我在作业中需要做的一件事是在表格中显示有关注册用户的信息。 必须通过单击按钮(性别、名字、姓氏……)以不同的方式对其进行排序。</p><p> 我在 PHP 中写这个。 我唯一想到的是使用不同的页面,但我会得到大约 20 个不同的页面。</p><p> 实现这一点的最佳方法是什么? 每种订单类型都有不同的页面?<br> 我从未使用过 jQuery,但它似乎对这类事情有好处?</p></div>PHP 的数据<table> </table> - How to sort <table> data with PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM