简体   繁体   English

如何一次匹配两个表中的记录

[英]How to Match Records from two tables once

This is the Match Table I'm trying to achieve I have a problem i am trying to resolve. 这是我要实现的匹配表我有一个要解决的问题。 Kindly help. 请帮助。 Thanks in Advance. 提前致谢。

Scenario: There are three tables in a database. 场景:数据库中有三个表。 Let's say tblA, tblB and tblC. 假设tblA,tblB和tblC。 It's for a service swap kind of scenario. 这是一种服务交换方案。 So, tblA contains records of people who request for a service and tblB contains records of those who offer their services. 因此,tblA包含请求服务的人员的记录,而tblB包含提供服务的人员的记录。 So, tblB is supposed to be matched with someone requesting the same service at the same available time and put the match records into tblC. 因此,应该将tblB与在相同可用时间请求相同服务的某人匹配,并将匹配记录放入tblC。

What I have Done/Tried: I have been able to create a query for a match to occur between both tables which is good progress. 我做过的事情/尝试过的事情:我已经能够为两个表之间发生的匹配创建查询,这是一个很好的进步。 But this leads me to a major problem. 但这导致我遇到一个重大问题。

The Problem: Problem is based on what i've done, the query matches one person requesting to more than one person offering. 问题:问题基于我所做的事情,查询将一个请求的人与一个以上的提议相匹配。 I want it to be once it matches a record in tblA to someone in tblB and put it in tblC, it should delete immediately so that it doesn't match those records to other people. 我希望它一旦与tblA中的某条记录匹配到tblB中的某人并将其放入tblC中,就应立即删除,以使其与其他人不匹配。

Example code: 示例代码:

    $match = "SELECT * FROM tblmatch";
    $Resmatch = mysql_query($match, $localhost) or die(mysql_error());
    $row_match = mysql_fetch_assoc($Resmatch);
    $mat_offuemail = $row_match['useremail'];
    $mat_offustype = $row_match['stype'];
    $mat_offtrange = $row_match['trange'];

    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "request-form")) {
      $insertSQL = sprintf("INSERT INTO tblrequest (orderid, useremail, catname, rdate, stype, trange, rdesc, rloc) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
                           GetSQLValueString($_POST['uorder'], "text"),
                           GetSQLValueString($_POST['uemail'], "text"),
                           GetSQLValueString($_POST['rcat'], "text"),
                           GetSQLValueString($_POST['date'], "text"),
                           GetSQLValueString($_POST['serv'], "text"),
                           GetSQLValueString($_POST['trange'], "text"),
                           GetSQLValueString($_POST['rdesc'], "text"),
                           GetSQLValueString($_POST['rloc'], "text"));

      If ($_POST['uemail'] == $mat_offuemail AND $_POST['serv'] == $mat_offustype AND $_POST['trange'] == $mat_offtrange){
        echo "Match Done Previously";
        }
      else{
        $inmatch = "INSERT INTO tblmatch (useremail, userorder, stype, uemail, uorder, trange)
SELECT tbloffer.useremail, tbloffer.orderid, tbloffer.stype, tblrequest.useremail, tblrequest.orderid, tbloffer.trange
FROM tbloffer
INNER JOIN tblrequest
ON tbloffer.stype = tblrequest.stype
AND tbloffer.trange = tblrequest.trange
WHERE tbloffer.useremail != tblrequest.useremail
AND tbloffer.catname != tblrequest.catname
ORDER BY tbloffer.useremail
LIMIT 1";
        }

      mysql_select_db($database_localhost, $localhost);
      $Result1 = mysql_query($insertSQL, $localhost) or die(mysql_error());
      $Result2 = mysql_query($inmatch, $localhost) or die(mysql_error());

      $insertGoTo = "match.php";
      if (isset($_SERVER['QUERY_STRING'])) {
        $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
        $insertGoTo .= $_SERVER['QUERY_STRING'];
      }
      header(sprintf("Location: %s", $insertGoTo));
    }

In order to match with only one offer we can use a sub-query and the GROUP BY clause. 为了仅与一个要约匹配,我们可以使用子查询和GROUP BY子句。 I am assuming that orderid is unique and can be used as primary key to select only one matching offer if they are more than one. 我假设orderid是唯一的,并且可以用作多个选择的报价的主键。 With the MIN function on the orderid I am always picking the one with smallest ID number, but any other aggregate function will work just as well (eg MAX ). 使用orderid上的MIN函数,我总是选择ID编号最小的函数,但是任何其他聚合函数也可以正常工作(例如MAX )。

Here is the complete SELECT stmnt: 这是完整的SELECT stmnt:

SELECT tbloffer.useremail, tbloffer.orderid, tbloffer.stype, tblrequest.useremail, tblrequest.orderid, tbloffer.trange
FROM tbloffer
JOIN tblrequest
ON tbloffer.stype = tblrequest.stype
AND tbloffer.trange = tblrequest.trange
WHERE tbloffer.useremail != tblrequest.useremail
AND tbloffer.catname != tblrequest.catname
AND tbloffer.orderid IN (
    SELECT min(orderid)
    FROM tbloffer
    GROUP BY stype, trange
)

Again I would suggest to evaluate if you could use a view instead of inserting matching rows by PHP script. 我再次建议评估是否可以使用视图而不是通过PHP脚本插入匹配的行。 A view can be created easily based on the above SQL statement and work as substitute for your script. 根据上述SQL语句可以轻松创建视图,并可以替代脚本。

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

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