1

我试图弄清楚如何让 PHP 检查和打印 2 个不同的函数。

这两个问题都指的是名为“remix”的表。目前第一个也是更重要的问题是,我想知道如何显示“作者”下有多少不同的值,以编译注册的作者总数。我不仅需要知道如何最有效地使用 COUNT 来返回“作者”下的唯一名称,还需要知道如何将其与当前编号的总行数内联显示。

第二个问题是问我如何根据他们的名字在列表中出现的次数来设置前 3 名艺术家。这也将显示在与上述代码相同的页面上。

这是我当前的代码:

require 'remix/archive/connect.php';
mysql_select_db($remix);
$recentsong = mysql_query("SELECT ID,song,author,filename FROM remix ORDER by ID desc limit 1;");
$row = mysql_fetch_array($recentsong);

echo'
<TABLE BORDER=1><TR><TD WIDTH=500>

Currently '.$row['ID'].' Remixes by **(want total artists here)** artists.<BR>

Most recent song: <A HREF=remix/archive/'.$row['filename'].'>'.$row['song'].'</A> by <FONT COLOR=white>'.$row['author'].'</FONT>

如您所见,我目前已将其设置为显示最近的歌曲(不是最有效的方式),但想要其他内容,例如至少是顶级贡献者,但不知道我是否会能够将它全部放在一个 php 块中,打破它,或者能够使用正确的代码在一次采石调用中完成所有操作。

谢谢你的帮助!

4

1 回答 1

1

我不确定我是否真的理解您问题中的所有内容,但我们会一起解决这个问题:p

我创建了一个 SQLFiddle 来处理一些测试数据:http ://sqlfiddle.com/#!2/9b613/1/0 。
注意现场的索引author,它将确保良好的性能:)

为了to know how to show how many DIFFERENT values are under "author"您可以使用:

SELECT COUNT(DISTINCT author) as TOTAL_AUTHORS
FROM remix;

为了知道the total number of rows, which are currently numbered你可以使用:

SELECT COUNT(*) as TOTAL_SONGS
FROM remix;

您可以将两者结合在一个查询中:

SELECT
    COUNT(DISTINCT author) as TOTAL_AUTHORS,
    COUNT(*) as TOTAL_SONGS
FROM remix;

To the top 3 subject now. This query will give you the 3 authors with the greatest number of songs, first one on top:

SELECT
    author,
    COUNT(*) as AUTHOR_SONGS
FROM remix
GROUP BY author
ORDER BY AUTHOR_SONGS DESC
LIMIT 3;

Let me know if this answer is incomplete and have fun with SQL !

Edit1: Well, just rewrite your PHP code in:

(...)
$recentsong = mysql_query("SELECT COUNT(DISTINCT author) as TOTAL_AUTHORS, COUNT(*) as TOTAL_SONGS FROM remix;");
$row = mysql_fetch_array($recentsong);
(...)
Currently '.$row['TOTAL_SONGS'].' Remixes by '.$row['TOTAL_AUTHORS'].' artists.<BR>
(...)

For the top3 part, use another mysql_query and create your table on the fly :)

于 2012-06-18T08:14:12.563 回答