我有一个包含user_id
,skill
的表格。
user_id skill
1 foo
1 bar
2 foo
3 bar
我们可以根据他的 bar 技能为user 1
提供工作。
如果收到的新要约也需要栏技能,我想将其委托给具有该技能的下一个用户。
如果找不到合格用户,则从表顶部查询以查找具有该技能的用户。
另一个名为jobs的表记录了所有作业history.its列是id,user_id,skill和timestamps
答案 0 :(得分:1)
这是不完整的,但应该提供一些思考的食物......
DROP TABLE IF EXISTS user_skill;
CREATE TABLE user_skill
(user_id INT NOT NULL
,skill_id INT NOT NULL
,PRIMARY KEY(user_id,skill_id)
);
INSERT INTO user_skill VALUES
(1,101),
(1,102),
(2,101),
(3,103);
DROP TABLE IF EXISTS jobs;
CREATE TABLE jobs
(job_id INT NOT NULL PRIMARY KEY
,skill_id INT NOT NULL
,assigned_to INT NULL
);
INSERT INTO jobs VALUES
(1001,101,1),
(1002,103,NULL),
(1003,102,NULL),
(1004,101,NULL);
UPDATE jobs x
JOIN
( SELECT u.*
, us.skill_id
, unassigned.job_id
FROM user u
JOIN user_skill us
ON us.user_id = u.user_id
LEFT
JOIN jobs assigned
ON assigned.assigned_to = u.user_id
JOIN jobs unassigned
ON unassigned.skill_id = us.skill_id
WHERE unassigned.assigned_to IS NULL
ORDER
BY assigned.job_id IS NULL DESC
, assigned.job_id DESC LIMIT 1
) y
ON y.skill_id = x.skill_id
AND y.job_id = x.job_id
SET x.assigned_to = y.user_id;