首页 文章

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

提问于
浏览
0

This is the Match Table I'm trying to achieve我有一个问题,我想解决 . 请帮助 . 提前致谢 .

Scenario: 数据库中有三个表 . 让's say tblA, tblB and tblC. It'用于服务交换类场景 . 因此,tblA包含请求服务的人的记录,而tblB包含提供其服务的人的记录 . 因此,tblB应该与在相同可用时间请求相同服务的人匹配并将匹配记录放入tblC .

What I have Done/Tried: 我已经能够为两个表之间的匹配创建一个查询,这是一个很好的进展 . 但这导致了一个重大问题 .

The Problem: 问题是基于我将这些记录与其他人匹配的内容 .

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 回答

  • 0

    为了只与一个商品匹配,我们可以使用子查询和 GROUP BY 子句 . 我假设 orderid 是唯一的,并且可以用作主键,如果它们不止一个,则只选择一个匹配的商品 . 使用orderid上的 MIN 函数,我总是选择ID号最小的函数,但任何其他聚合函数也可以正常工作(例如 MAX ) .

    这是完整的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
    )
    

    我建议再次评估是否可以使用视图而不是通过PHP脚本插入匹配的行 . 可以基于上述SQL语句轻松创建视图,并替代脚本 .

相关问题