请帮助我们提出一个算法,我不打算创建一个包含两个或更多参数的复杂样本。
问题在于,在表中有一个可以包含item1 or item2
的字段,但是有两个强制参数,算法应该在这两个之间进行vyberat,但所以他应该看起来像一个可能的匹配适用于同一数据集的其他标准。
请帮助,我已经陷入困境。 根据我出来的标准发现并挑战 - 没有。
目前我有三张桌子:
看起来像这样
+-----------+ +---------------+ +---------------------------------------------------+
|disciplines| |specializations| | discipline_specializations |
+----+------+ +---------------+ +----+---------------+-------------------+----------+
| id | name | | id | name | | id | discipline_id | specialization_id | priority |
+----+------+ +----+----------+ +----+---------------+-------------------+----------+
请帮助,我根本不知道如何实施它 谢谢!
答案 0 :(得分:0)
这里似乎有两个问题。
第一个是存储数据。 item3 or item4
已经标准化,因此您应该将这些内容分解为具有相同优先级的单独学科(如果我已了解您的示例)。
+---------------------------------------------------+
| discipline_specializations |
+----+---------------+-------------------+----------+
| id | discipline_id | specialization_id | priority |
+----+---------------+-------------------+----------+
| 1 | item1 | object1 | 1 |
+----+---------------+-------------------+----------+
| 2 | item2 | object1 | 2 |
+----+---------------+-------------------+----------+
| 3 | item3 | object1 | 3 |
+----+---------------+-------------------+----------+
| 4 | item4 | object1 | 3 |
+----+---------------+-------------------+----------+
| 5 | item5 | object2 | 1 |
+----+---------------+-------------------+----------+
| 6 | item4 | object2 | 2 |
+----+---------------+-------------------+----------+
| 7 | item2 | object2 | 3 |
+----+---------------+-------------------+----------+
| 8 | item1 | object2 | 3 |
+----+---------------+-------------------+----------+
| 9 | item1 | object3 | 1 |
+----+---------------+-------------------+----------+
| 10 | item3 | object3 | 2 |
+----+---------------+-------------------+----------+
| 11 | item6 | object3 | 3 |
+----+---------------+-------------------+----------+
| 12 | item5 | object3 | 3 |
+----+---------------+-------------------+----------+
| 13 | item2 | object4 | 1 |
+----+---------------+-------------------+----------+
| 14 | item1 | object4 | 2 |
+----+---------------+-------------------+----------+
| 15 | item4 | object4 | 1 |
+----+---------------+-------------------+----------+
| 16 | item5 | object4 | 1 |
+----+---------------+-------------------+----------+
第二个问题是查询。您正在寻找的是具有所有必要学科的专业。 MySQL没有INTERSECT运算符,但我们可以用IN语句模仿它。所以这将是查找匹配一个学科的专业化的查询......
select * from specializations s
where s.specialization_id in ( select ds.specialization_id
from discipline_specializations ds
where ds.discipline_id = 'item1' ) ;
这将是查找匹配四个学科的专业化的查询...
select * from specializations s
where s.specialization_id in ( select ds.specialization_id
from discipline_specializations ds
where ds.discipline_id = 'item1' )
and s.specialization_id in ( select ds.specialization_id
from discipline_specializations ds
where ds.discipline_id = 'item2' )
and s.specialization_id in ( select ds.specialization_id
from discipline_specializations ds
where ds.discipline_id = 'item3' )
and s.specialization_id in ( select ds.specialization_id
from discipline_specializations ds
where ds.discipline_id = 'item4' )
;
答案 1 :(得分:0)
解决我的问题:
SELECT d.name as discipline_name, s.name as specialization_name
FROM pqh1s_enrolle_disciplines d
LEFT JOIN pqh1s_enrolle_discipline_specializations ds ON ds.discipline_id = d.id
LEFT JOIN pqh1s_enrolle_specializations s ON s.id = ds.specialization_id
WHERE d.id IN (1, 2)
GROUP BY s.id
HAVING COUNT(DISTINCT ds.priority) = 2