简体   繁体   中英

Compare different rows from database table

I have a table in my database with the name : connected and it look like the image below ...

在此处输入图片说明

What I want is to compare the rows of table in order to check that if the user with username for example niklakis has a friend with name tlagkas and if user with username for example tlagkas has a friend with name niklakis then echo a message

I tried something but it does not work.

This is the code...

//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/>";
        }  
    }

Can you help me ?

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]'
;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM