获取逗号分隔的关键字,然后对其进行排序

时间:2015-12-03 02:22:45

标签: php mysql sql database phpmyadmin

我正在寻找使用MySQL和/或PHP的解决方案。

所以我有一个带有视频表的MySQL数据库......

                   if ($username && $password)
                   {
                   $query = mysqli_query($link, "SELECT * FROM login");
                   while($row = mysqli_fetch_assoc($query))
                    {
                    $dbusername = $row['UserId'];
                    $dbpassword = $row['Password'];
                    $dbprivileges = $row['Privileges'];

                    if($username == $dbusername && $password == $dbpassword)
                    {
                    //admin home page
                    if($dbprivileges == "Admin")
                    {
                        @$_SESSION['admin'] = $username;
                        header('Location:hris_admin_index.php');
                    }
                    //company home page
                    elseif($dbprivileges == "Company")
                    {
                        @$_SESSION['employer'] = $username;
                        header('Location:hris_company_index.php');
                    }
                    //employee home page
                    elseif($dbprivileges == "Employee")
                    {
                        @$_SESSION['employee'] = $username;
                        header('Location:hris_employee_index.php');
                    }
                     }

我的问题是,我需要找出一种方法来获取不同的关键字,计算它们的出现次数以及观看次数......所以我想尝试这样的事情:

Record    Keywords
1         Action, Sci-Fi, Future
2         Comedy, Jokes, Sci-Fi
3         Youtube, Sci-Fi, Jokes

现在,我在这个表中有超过20,000行,每个行都有多个关键字,我需要能够在每个页面上放置一个前N个,此列表需要是动态的并且是真实的 - 时间,所以表现是一个非常重要的方面。

2 个答案:

答案 0 :(得分:0)

假设您没有重组数据库:

您需要获取所有行,并遍历它们。

// array of keyword counters
$keyword_counts = array();

foreach ($rows as $row)
{
    // explode by ', '
    $keywords = explode($row["Keywords"], ", ");
    foreach ($keywords as $key)
    {
        if (array_key_exists($key, $keyword_counts))
        {  
            // counter for this keyword exists, increment
            $keyword_counts[$key]++;
        }
        else
        {
            // this is the first instance of this keyword, set it to 1
            $keywords_counts[$key] = 1;
        }
    }
}

这将为您留下类似于:

的未排序数组
array(
"Jokes" => 34,
"Action" => 22,
"Comedy" => 15)

并对其进行排序,您可以使用

arsort($keyword_counts, SORT_NUMERIC);
保存密钥的

,因为它们是关键字的名称,并且命令以数字方式递减。

答案 1 :(得分:0)

您可以使用此类查询

SELECT COUNT(record) count, tag FROM (
    SELECT b.record, SUBSTRING_INDEX(SUBSTRING_INDEX(b.keywords, ',', a.n), ',', -1) tag
    FROM (
        SELECT (i+1) n 
        FROM (
          SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 
          4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9
        ) a
    ) a
    INNER JOIN (
        SELECT record, keywords
        FROM video
    ) b ON CHAR_LENGTH(b.keywords)-CHAR_LENGTH(REPLACE(b.keywords, ',', ''))>=a.n-1
) a GROUP BY tag ORDER BY count DESC, tag

请参阅sqlfiddle here