如何比较同一个 MySQL 表(在 php 中)中不同行的不同列?
我正在尝试将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这将需要自加入。假设(根据您的代码)A 列是请求者,B 列是被请求者。请参阅以下示例以获取比较中的所有记录。根据您的要求调整查询并向查询添加更多过滤器。
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.