比较数据库表中的不同行

时间:2015-05-06 23:02:39

标签: php mysql

我的数据库中有一个表格,其名称为:已连接,如下图所示......

enter image description here

我想要的是比较表的行,以便检查具有用户名的用户(例如niklakis)是否有名为tlagkas的朋友,如果用户名为tlagkas的用户是否有名为niklakis的朋友则回显邮件

我尝试了一些东西,但它不起作用。

这是代码......

//ALERT SYSTEM    
    $query2 = mysql_query("SELECT username as patient, typeValue FROM sensors WHERE status='0' AND sensorValue < min OR sensorValue > max");  
    $p1 = mysql_num_rows(queryMysql("SELECT * FROM connected
            WHERE username='$row[0]' AND friend='$username'"));
    $p2 = mysql_num_rows(queryMysql("SELECT * FROM connected
            WHERE username='$username' AND friend='$row[0]'"));  

    while($row = mysql_fetch_array($query2))
    {
        if(($p1 + $p2) >1)
        {
            $alert_message= " <b><font color=red><p align='center'>User " . $row['patient'] . " Has A Health Problem with his/her ".$row['typeValue']."</font></b>";

            echo $alert_message."<br/>";
            //echo $row[0]."<br/>";
            //echo $row['patient']."<br/>";
            //echo $username."<br/>";
            //echo $p1."<br/>";
            //echo $p2."<br/>";
            //echo $p1 + $p2."<br/>";
        }  
    }

你能帮助我吗?

1 个答案:

答案 0 :(得分:0)

SELECT a.*
FROM connected AS a 
INNER JOIN connected AS b 
ON a.username = b.friend AND a.friend = b.username
WHERE a.username='$username' AND a.friend='$row[0]'
;