列出每个项目的编号计数的类别

时间:2016-01-05 15:09:11

标签: php mysql

您好我想在类别页面上列出6个类别,每个类别包含每个项目的数量。

我的桌子是..

table:categorii

+--------+--------+--------+----------+--------+
| catid  |  name  |  title |  url     |  icon  |
+--------+--------+--------+----------+--------+
|   1    |  Name  |  Name  |  urlname | 1.jpg  |
|   2    |  Name  |  Name  |  urlname | 2.jpg  |
|   3    |  Name  |  Name  |  urlname | 3.jpg  |
|   4    |  Name  |  Name  |  urlname | 4.jpg  |
|   5    |  Name  |  Name  |  urlname | 5.jpg  |
|   6    |  Name  |  Name  |  urlname | 6.jpg  |
|   7    |  Name  |  Name  |  urlname | 7.jpg  |
|   8    |  Name  |  Name  |  urlname | 8.jpg  |
|   9    |  Name  |  Name  |  urlname | 9.jpg  |
|   10   |  Name  |  Name  |  urlname | 10.jpg |
|   11   |  Name  |  Name  |  urlname | 11.jpg |
|   12   |  Name  |  Name  |  urlname | 12.jpg |
|   13   |  Name  |  Name  |  urlname | 13.jpg |
|   14   |  Name  |  Name  |  urlname | 14.jpg |
|   15   |  Name  |  Name  |  urlname | 15.jpg |
|   16   |  Name  |  Name  |  urlname | 16.jpg |
+--------+--------+--------+----------+--------+

table:jocuri

+---------+--------+----------+-----------+-----------+-----------+-----------+-----------+-----------+
| fileid  |  name  | category | category2 | category3 | category4 | category5 | category6 |   status  +
+---------+--------+----------+-----------+-----------+-----------+-----------+-----------+-----------+
|    1    |  name  |     1    |    9      |     0     |     0     |     0     |     0     |      1    |
|    2    |  name  |     1    |    18     |     24    |     16    |     11    |     9     |      0    |
|    3    |  name  |     2    |    11     |     9     |     13    |     0     |     0     |      0    |
|    4    |  name  |     1    |    20     |     14    |     0     |     0     |     0     |      1    |
|    5    |  name  |     6    |    8      |     16    |     22    |     10    |     18    |      1    |
|    6    |  name  |     2    |    13     |     10    |     0     |     0     |     0     |      1    |
|    7    |  name  |     1    |    14     |     9     |     8     |     0     |     0     |      0    |
|    8    |  name  |     5    |    12     |     11    |     9     |     15    |     8     |      1    |
|    9    |  name  |     1    |    9      |     8     |     11    |     13    |     10    |      1    |
|    10   |  name  |     1    |    10     |     8     |     9     |     0     |     0     |      1    |
|    11   |  name  |     1    |    8      |     9     |     12    |     10    |     16    |      1    |
|    12   |  name  |     6    |    10     |     0     |     0     |     0     |     0     |      0    |
+---------|--------+----------+-----------+-----------+-----------+-----------+-----------+-----------+

在类别页面上列出的正确命令(php和mysql)是什么,所有类别的状态为1,随机并显示每个类别的fileid数量?

非常感谢!

1 个答案:

答案 0 :(得分:1)

首先将数据库更改为3个表。

分类

+--------+--------+--------+----------+--------+
| catid  |  name  |  title |  url     |  icon  |
+--------+--------+--------+----------+--------+
|   1    |  Name  |  Name  |  urlname | 1.jpg  |
|   2    |  Name  |  Name  |  urlname | 2.jpg  |
|   3    |  Name  |  Name  |  urlname | 3.jpg  |
|   4    |  Name  |  Name  |  urlname | 4.jpg  |
|   5    |  Name  |  Name  |  urlname | 5.jpg  |
|   6    |  Name  |  Name  |  urlname | 6.jpg  |
|   7    |  Name  |  Name  |  urlname | 7.jpg  |
|   8    |  Name  |  Name  |  urlname | 8.jpg  |
|   9    |  Name  |  Name  |  urlname | 9.jpg  |
|   10   |  Name  |  Name  |  urlname | 10.jpg |
|   11   |  Name  |  Name  |  urlname | 11.jpg |
|   12   |  Name  |  Name  |  urlname | 12.jpg |
|   13   |  Name  |  Name  |  urlname | 13.jpg |
|   14   |  Name  |  Name  |  urlname | 14.jpg |
|   15   |  Name  |  Name  |  urlname | 15.jpg |
|   16   |  Name  |  Name  |  urlname | 16.jpg |
+--------+--------+--------+----------+--------+

FileIDs

+---------+--------+
| fileid  |  name  |
+---------+--------+
|    1    |  name  |
|    2    |  name  |
|    3    |  name  |
|    4    |  name  |
|    5    |  name  |
|    6    |  name  |
|    7    |  name  |
|    8    |  name  |
|    9    |  name  |
|    10   |  name  |
|    11   |  name  |
|    12   |  name  |
+---------|--------+

FileIDs_has_Categories

    +---------+------------+
    | fileid  |  category  |
    +---------+------------+
    |    1    |    1       |
    |    1    |    9       |
    |    2    |    1       |
    |    2    |    18      |
    |    2    |    24      |
    |    2    |    16      |
    |    2    |    11      |
    |    2    |    9       |
    |    3    |    11      |
    |    3    |    9       |
    |    3    |    13      |
    |    4    |    1       |
    +---------|------------+

然后,您可以通过

获取类别的计数(与该类别相关联的文件ID的数量)
SELECT COUNT(fileid) FROM FileIDs_has_Categories where category = 1

哪个会返回您对类别1的文件数量