简体   繁体   English

来自单个表的链接记录,两个表在PHP mysql中

[英]Chainup records from single table with two columns in PHP mysql

I have two columns ie: 我有两列,即:

   caseSno                 newCaseSno
   7592                     7593
   7591                     7592
   2935                     7591
   7092                     7572

In the above table caseSno and new CaseSno are two columns where in caseSno is the original case number while after case has been transferred to someone else newCaseSno keep the new number, now I want to find all the associated cases herein this table. 在上表中, caseSno和new CaseSno是两列,其中caseSno是原始案件编号,而案件已转移到其他人之后newCaseSno保留新编号,现在我要在此表中找到所有相关案件。 ie in the first row caseSno 7593 should be splayed in my report 4 times as it is associated with 7593,7592,7591 and 2935. In simple words I need something like Friends of friend mechanism 即在第一行的情况下, caseSno 7593应该在我的报告中展开四次,因为它与7593,7592,7591和2935相关联。简而言之,我需要像“ Friends offriend机制”之类的东西

Please help me, how to find out all the record associated to each column in other. 请帮助我,如何找出与其他每一列相关的所有记录。

Simple and fast executable query will be appreciated. 简单和快速的可执行查询将不胜感激。

One idea would be to define three tables: 一种想法是定义三个表:

CaseTbl         Person             CasePersonHistory
------------    --------------     ------------------
case_id (PK)    person_id (PK)     case_id   (PK)
case_name       etc etc...         person_id (PK)
case_descr                         seq_num   (PK)
etc etc...                         etc etc...

EDIT: Can't use case for table name... choose something else instead 编辑:不能为表名使用case ...选择其他内容

The CaseTbl always has one id... it's unique description. CaseTbl总是有一个ID ...这是唯一的描述。 Same for Person . 同样的Person CasePersonHistory supplies a many-to-many relationship between cases and people with a sequence number, where the highest seq_num would be the person who currently owns the case, and all lower seq_num s being people who previously owned the case. CasePersonHistory在案例和具有序列号的人之间提供多对多关系,其中最高的seq_num将是当前拥有该案例的人,而所有较低的seq_num是先前拥有该案例的人。

So for example, if 例如,如果

fred's person_id  = 54
mikes's person_id = 55
jane's person_id  = 56

If the case_id is 1 , And Fred originally had the case, then gave it to Mike, who then gave it to Jane then CasePersonHistory would look like... 如果case_id1 ,并且Fred最初拥有该案例,则将其交给Mike,后者CasePersonHistory其交给Jane,那么CasePersonHistory看起来就像...

CasePersonHistory
------------------
case_id      person_id     seq_num
-----------------------------------
1            54            1
1            55            2
1            56            3

Now you can find out who the current owner or owners of the case are by selecting from the table based on the case_id and finding the max of the sequence number: 现在,您可以通过基于case_id从表中进行选择,并找到序列号的最大值,从而找出案件的当前所有者是谁?

SELECT MAX(seq_num) FROM CasePersonHistory WHERE case_id = 1;

or to get the person who currently owns the case... 或让目前拥有此案的人...

SELECT person_id FROM CasePersonHistory WHERE case_id = 1 order by seq_no DESC LIMIT 1

Hope that helps :) 希望有帮助:)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM