简体   繁体   English

用MySQL计算在两个表之间重复一个String的次数

[英]Count the times that a String are repeated between two tables with MySQL

Well I'm trying to get all the times that a String are repeated in two tables using a comun column and getting all the results with a While, like that: 好吧,我正在尝试使用comun列获取在两个表中重复一个String的所有时间,并使用While来获取所有结果,像这样:

    $result = mysql_query("SELECT * FROM hits") or die(mysql_error());

while($row = mysql_fetch_assoc( $result )) 
{
    $result=mysql_query("SELECT COUNT(app) FROM info WHERE app='$row[page]'");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) 

{

$toip = $row[0] ;  
}

echo '<td bgcolor="#75D169">'; 
    echo "$toip";
    echo '</td>';

    }

But it doesn't show it very well as you can see it here: http://ikillcraft.a0001.net/counter/view.php?pass=test 但是它显示的不是很好,您可以在这里看到它: http : //ikillcraft.a0001.net/counter/view.php?pass=test

The output would be: 输出为:

Instalaciones de                    4       0 
Instalaciones de IkillLauncher      3       3
Instalaciones de gogogoa            3       0 
Instalaciones de MasterShell        0       0

But I don't know how to do it. 但是我不知道该怎么做。 :( :(

Using a single piece of SQL, something like this would do it:- 使用单条SQL,可以这样:

$result = mysql_query("SELECT a.page, COUNT(b.app) AS AppCount
                        FROM hits a
                        LEFT OUTER JOIN info b
                        ON a.page = b.app
                        GROUP BY a.page";

while($row = mysql_fetch_assoc( $result )) 
{
    echo '<td bgcolor="#75D169">'; 
    echo $row['AppCount'];
    echo '</td>';

}

Note I do not know your column names, and you should have all the non aggregate column names in the GROUP BY clause (ie, anything except the ones in the COUNT in this case) 注意我不知道您的列名,并且您应该在GROUP BY子句中具有所有非聚合的列名(即,在这种情况下,除了COUNT中的那些以外的任何东西)

I don't know your table structure, so I am doing guesswork here, but this should work: 我不知道您的表结构,所以我在这里做猜测,但这应该可以工作:

SELECT hits.page, appgrouped.apphit
FROM (hits)
LEFT JOIN (SELECT COUNT(app) as apphit, app
           FROM info 
           GROUP BY app) AS appgrouped ON appgrouped.app = hits.page

This would be quite slow - depending on how big: 这会非常慢-取决于大小:

SELECT COUNT(app) as apphit, app
FROM info 
GROUP BY app

This is, if it is big, then the above join will be very slow, and in that case, PHP code would be a better solution (yes it would be faster). 也就是说,如果很大,则上述连接将非常慢,在这种情况下,PHP代码将是更好的解决方案(是的,它会更快)。

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

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