duangan6731 2016-12-23 15:08
浏览 31
已采纳

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

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. 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. 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.

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.

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));
    }
  • 写回答

1条回答 默认 最新

  • douji8549 2016-12-23 15:32
    关注

    In order to match with only one offer we can use a sub-query and the GROUP BY clause. 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. 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 (e.g. MAX).

    Here is the complete 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. A view can be created easily based on the above SQL statement and work as substitute for your script.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存