简体   繁体   English

在MYSQL计数查询中获取额外的数据

[英]Getting extra data in MYSQL count query

Hi all i've had help from bluefleet (BF) with a mysql query which i've now put in a php file, the query works fine but i find i need to get extra data as well as the count, so what i need is when a match is found i need to look up the username in table tview3. 大家好,我从bluefleet(BF)那里获得了有关mysql查询的帮助,我现在将它放在一个php文件中,该查询工作正常,但是我发现我需要获取额外的数据以及计数,所以我需要什么当找到匹配我需要查找的用户名表tview3是。

Whichever table the ip match is found there will be a column called UID, in the table tview3 there will also be a column called USERNAME, what i'd like to do is get the username(s) for the matches and then use them in a text file. 无论找到哪个IP匹配表,都会有一个名为UID的列,在表tview3中也会有一个名为USERNAME的列,我想做的就是获取匹配的用户名,然后在其中使用它们文本文件。

I have to state i'm a complete newbie at this, all help is appreciated 我必须说明我在这一个完整的新手,所有的帮助表示赞赏

$myquery= "select sum(total)
from
(
    SELECT count(*) as total
    FROM " .TABLE_PREFIX."tview v
    where v.ipaddress = '$ips'
    union all
    SELECT count(*) as total
    FROM " .TABLE_PREFIX."tview2 v1
    where v1.ipaddress = '$ips'
    union all
    SELECT count(*) as total
    FROM " .TABLE_PREFIX."tview3 v3
    where v3.ipaddress = '$ips'
) src";

 $result = mysql_query($myquery);
    $rowCount = mysql_num_rows($result);
    If($rowCount !=0){
       echo "NOT EMPTY";
    }else{
      echo "EMPTY";
    }
   mysql_free_result($result);

$UAM = strtoupper($_SERVER['HTTP_USER_AGENT']);
$ips = strtoupper($_SERVER['REMOTE_ADDR']);
$fp = fopen("logtext.txt", "a");
$DateOfRequest = date('m-d-Y H:i:s');

fwrite($fp, "$DateOfRequest . \nMatched Member: . $username . \nWith User Agent:  . $UAM . \n\nIP: . $ips . \n\n");

So i'd like to populate a variable $username with the username(s) where the matches were found. 所以我想用找到匹配项的用户名填充变量$ username

Regards, Silo 问候,筒仓

Change: 更改:

SELECT count(*) as total

to

 SELECT count(*) as total, username

Then you could do something like: 然后,您可以执行以下操作:

$row = mysql_fetch_array($query);
$username = $row['username'];

Hope that helps. 希望能有所帮助。 Also check out this question for a bit more detail: How do I find the most common result in a column in my MySQL table 还请查看此问题以获取更多详细信息: 如何在MySQL表的列中找到最常见的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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