如何比较同一个 MySQL 表(在 php 中)中不同行的不同列?

发布于 2024-10-27 21:07:02 字数 1985 浏览 3 评论 0原文

我正在尝试将 A 行的 A 列与 B 行的 B 列进行比较。我如何在 php 中执行此操作?

这是我的代码:

<?php

include("include/config.php");
include("include/functions/import.php");

$addToFriends = array();
$atf = 0;

//connect to mysql and get data for main user
mysql_connect($DBHOST,$DBUSER,$DBPASSWORD);
mysql_select_db($DBNAME);

//grabs all friend requests sent by user
$sql = "SELECT * FROM friends_requests WHERE REQUESTER = '.$_SESSION[USERID].' ";
$q = mysql_query($sql) or die(mysql_error());

$ar1 = array();
$RIDs = array();
$i=0;
while($row = mysql_fetch_array($q))
{
$ar1[] = $row[2];
$RIDs[] = $row[0];
echo "ar1".$ar1[$i];
$i++;
}

//grabs all friend requests sent to user
$sql = "SELECT * FROM friends_requests WHERE REQUESTEE = '.$_SESSION[USERID].' ";
$q = mysql_query($sql) or die(mysql_error());


$ar2 = array();
$i=0;
while($row = mysql_fetch_array($q))
{
$ar2[] = $row[1];
echo "ar2".$ar2[$i];

$i++;
}


    for($int = 0; $int < sizeof($ar1); $int++)
    {

        for($t = 0; $t < sizeof($ar2); $t++)
        {

            if($ar1[$int] == $ar2[$t])
            {
            $sql = "SELECT * FROM friends_requests WHERE RID = '.$RIDs[$int].' ";
$q = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($q))
{
            echo "lookie".$row[1];
            echo "lookie".$row[2];
            echo "/".$t;
            echo "/".$int;
                mysql_query("INSERT INTO friends (USERID,FRIENDID,time_added) VALUES ($row[1],$row[2],NOW())");
                mysql_query("INSERT INTO friends (USERID,FRIENDID,time_added) VALUES ($row[2],$row[1],NOW())");
                mysql_query("DELETE FROM friends_requests WHERE REQUESTER = $row[1] AND REQUESTEE = $row[2]");
                mysql_query("DELETE FROM friends_requests WHERE REQUESTER = $row[2] AND REQUESTEE = $row[1]");
                echo "make friends".$row[1]."b/w".$row[2];  }
            }

        }

    }


?>

目标是从 ColumnA,RowA = ColumnB,RowB 的尽可能多的行中找到两行

I'm trying to compare column A of row A to column B of row B. How could I go about doing this in php?

This is the code i have:

<?php

include("include/config.php");
include("include/functions/import.php");

$addToFriends = array();
$atf = 0;

//connect to mysql and get data for main user
mysql_connect($DBHOST,$DBUSER,$DBPASSWORD);
mysql_select_db($DBNAME);

//grabs all friend requests sent by user
$sql = "SELECT * FROM friends_requests WHERE REQUESTER = '.$_SESSION[USERID].' ";
$q = mysql_query($sql) or die(mysql_error());

$ar1 = array();
$RIDs = array();
$i=0;
while($row = mysql_fetch_array($q))
{
$ar1[] = $row[2];
$RIDs[] = $row[0];
echo "ar1".$ar1[$i];
$i++;
}

//grabs all friend requests sent to user
$sql = "SELECT * FROM friends_requests WHERE REQUESTEE = '.$_SESSION[USERID].' ";
$q = mysql_query($sql) or die(mysql_error());


$ar2 = array();
$i=0;
while($row = mysql_fetch_array($q))
{
$ar2[] = $row[1];
echo "ar2".$ar2[$i];

$i++;
}


    for($int = 0; $int < sizeof($ar1); $int++)
    {

        for($t = 0; $t < sizeof($ar2); $t++)
        {

            if($ar1[$int] == $ar2[$t])
            {
            $sql = "SELECT * FROM friends_requests WHERE RID = '.$RIDs[$int].' ";
$q = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($q))
{
            echo "lookie".$row[1];
            echo "lookie".$row[2];
            echo "/".$t;
            echo "/".$int;
                mysql_query("INSERT INTO friends (USERID,FRIENDID,time_added) VALUES ($row[1],$row[2],NOW())");
                mysql_query("INSERT INTO friends (USERID,FRIENDID,time_added) VALUES ($row[2],$row[1],NOW())");
                mysql_query("DELETE FROM friends_requests WHERE REQUESTER = $row[1] AND REQUESTEE = $row[2]");
                mysql_query("DELETE FROM friends_requests WHERE REQUESTER = $row[2] AND REQUESTEE = $row[1]");
                echo "make friends".$row[1]."b/w".$row[2];  }
            }

        }

    }


?>

the goal is to find the two rows out of as many as there are where ColumnA,RowA = ColumnB,RowB

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霓裳挽歌倾城醉 2024-11-03 21:07:02

这将需要自加入。假设(根据您的代码)A 列是请求者,B 列是被请求者。请参阅以下示例以获取比较中的所有记录。根据您的要求调整查询并向查询添加更多过滤器。

select a.*,b.* from friends_requests a, friends_requests b 
where a.requester = b.requestee

This will require a self join. Assuming (from your code) that column A is requester and coumn B is requestee. See the following example to get all records from the comparison. Tweak and add more filters to the query as per your requirements.

select a.*,b.* from friends_requests a, friends_requests b 
where a.requester = b.requestee
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文