获取具有相同IP的用户名引用的所有裁判。会员可以拥有多个帐户

时间:2014-07-18 11:13:11

标签: php mysql

我希望分支机构能够看到他们有多少推荐人。

所以我的表有3列:idIPusernamereferee。会员可以拥有多个帐户。

这是我到目前为止所拥有的。

//Get all of that IP's usernames
$sql = "SELECT username FROM ".MYSQLTABLE." WHERE ip = '" . $_SERVER['REMOTE_ADDR']."'";
$res = mysql_query($sql)or die(mysql_error());

//Select all usernames, who's referee = one of IP's usernames (using above query) 
$sql1 = "SELECT usernames FROM ".MYSQLTABLE." WHERE referee = '".$res."'";
$res1 = mysql_query($sql1)or die(mysql_error());

while($row=mysql_fetch_array($res1))
{

1 个答案:

答案 0 :(得分:0)

你可以这样做是一个单一的查询,简化了事情:

SELECT DISTINCT(u.username)
FROM yourTable u
INNER JOIN yourTable r ON (r.username = u.referee AND r.ip = 'TheIP');