mySQL - 获取具有相同外部属性的条目元组

时间:2016-05-20 13:01:25

标签: mysql sql

我有以下表格:

工人( id ,fname,lname生日)

works_on( worker_id task_id )。

现在,我希望让所有工作的元组都能完成相同的任务。 我的想法是让工作人员加入works_on,然后再次加入worker。基本上这是有效的,但我得到了重复:

John Smith, Walther White

Walther White, John Smith

希望你能帮助我。

约翰

1 个答案:

答案 0 :(得分:2)

您可以使用以下查询:

SELECT t1.fname, t1.lname, t4.fname, t4.lname, t2.task_id
FROM worker AS t1
JOIN works_on AS t2 ON t1.id = t2.worker_id
JOIN works_on AS t3 ON t2.task_id = t3.task_id AND t2.worker_id < t3.worker_id
JOIN worker AS t4 ON t3.worker_id = t4.id

这里的技巧是第二个ON操作的JOIN子句中的谓词:

t2.worker_id < t3.worker_id

这会使得在同一项目中工作的id 提供 id值大于id t1的值worker } <div id="main_place"> main </div> <button onclick="show(operation1)">Replace to operation 1</button> <button onclick="show(operation2)">Replace to operation 2</button> <button onclick="show(operation3)">Replace to operation 3</button> <div id=operation1 style=“display:none”> Some textboxes and text </div> <div id=operation2 style=“display:none”> Again some textboxes and text </div> <div id=operation3 style=“display:none”> And again some textboxes and text </div> <script> function show(param_div_id){ document.getElementById('main_place').innerHTML = Here should be div from param; } </script> 表。这样,排除了重复对。

Demo here