0

我有一张歌曲表,有些歌曲是专辑歌曲,有些是单曲……我有一张专辑表……

例子:

如果专辑ID为[null],则表示歌曲为单曲

歌曲表:

+--------+---------+-----------+----------------+---------+---------------+-------+--------+--------+------+-------+-------+----------+
| Row ID | Song ID | Album ID  |    Song Name   |   Band  | Date Released | Genre | Lyrics | Lenght | Size | Plays | Likes | Dislikes |
+--------+---------+-----------+----------------+---------+---------------+-------+--------+--------+------+-------+-------+----------+
|    1   | Song-01 |  Album-1  |    Song-001    | Band-1  |  2010-12-15   | Rock  |   ...  |  4:30  | 4 MB |  101  |  11   |    1     |
|    2   | Song-02 |  Album-1  |    Song-002    | Band-1  |  2010-12-15   | Rock  |   ...  |  5:30  | 5 MB |  102  |  12   |    2     |
|    3   | Song-03 |  Album-1  |    Song-003    | Band-1  |  2010-12-15   | Rock  |   ...  |  3:30  | 3 MB |  103  |  13   |    3     |
|    4   | Song-04 |   [null]  |    Song-004    | Band-1  |  2010-12-16   | Rock  |   ...  |  4:30  | 4 MB |  104  |  14   |    4     |
|    5   | Song-05 |  Album-2  |    Song-005    | Band-1  |  2010-12-17   | POP   |   ...  |  3:30  | 3 MB |  105  |  15   |    5     |
|    6   | Song-06 |  Album-2  |    Song-006    | Band-1  |  2010-12-17   | RAP   |   ...  |  5:30  | 5 MB |  106  |  16   |    6     |
|    7   | Song-07 |   [null]  |    Song-007    | Band-2  |  2010-12-17   | Rock  |   ...  |  3:30  | 3 MB |  107  |  17   |    7     |
|    8   | Song-08 |  Album-3  |    Song-008    | Band-2  |  2010-12-17   | Rock  |   ...  |  4:30  | 4 MB |  108  |  18   |    8     |
|    9   | Song-09 |  Album-3  |    Song-009    | Band-2  |  2010-12-17   | POP   |   ...  |  5:30  | 5 MB |  109  |  19   |    9     |
|   10   | Song-10 |  Album-3  |    Song-010    | Band-2  |  2010-12-17   | Punk  |   ...  |  6:30  | 6 MB |  110  |  20   |    0     |
|   11   | Song-11 |  Album-3  |    Song-011    | Band-2  |  2010-12-17   | RAP   |   ...  |  7:30  | 7 MB |  111  |  21   |    1     |
|   12   | Song-12 |   [null]  |    Song-012    | Band-2  |  2010-12-18   | Rock  |   ...  |  3:30  | 3 MB |  112  |  22   |    2     |
|   13   | Song-13 |   [null]  |    Song-013    | Band-2  |  2010-12-18   | Rock  |   ...  |  2:30  | 2 MB |  113  |  23   |    3     |
|   14   | Song-14 |   [null]  |    Song-014    | Band-3  |  2010-12-18   | Rock  |   ...  |  6:30  | 6 MB |  114  |  24   |    4     |
|   15   | Song-15 |   [null]  |    Song-015    | Band-3  |  2010-12-19   | Rock  |   ...  |  7:30  | 7 MB |  115  |  25   |    5     |
|   16   | Song-16 |   [null]  |    Song-016    | Band-3  |  2010-12-19   | Rock  |   ...  |  4:30  | 4 MB |  116  |  26   |    6     |
|   17   | Song-17 |   [null]  |    Song-017    | Band-4  |  2010-12-19   | POP   |   ...  |  3:30  | 3 MB |  117  |  27   |    7     |
|   18   | Song-18 |   [null]  |    Song-018    | Band-4  |  2010-12-19   | POP   |   ...  |  2:30  | 2 MB |  118  |  28   |    8     |
|   19   | Song-19 |   [null]  |    Song-019    | Band-5  |  2010-12-20   | Rock  |   ...  |  4:30  | 4 MB |  119  |  29   |    9     |
|   20   | Song-20 |   [null]  |    Song-020    | Band-5  |  2010-12-20   | Rock  |   ...  |  5:30  | 5 MB |  120  |  30   |    0     |
|   21   | Song-21 |   [null]  |    Song-021    | Band-5  |  2010-12-20   | Rock  |   ...  |  6:30  | 6 MB |  121  |  31   |    1     |
|   22   | Song-22 |  Album-4  |    Song-022    | Band-5  |  2010-12-21   | Rock  |   ...  |  3:30  | 3 MB |  122  |  32   |    2     |
|   23   | Song-23 |  Album-4  |    Song-023    | Band-5  |  2010-12-21   | Rock  |   ...  |  2:30  | 2 MB |  123  |  33   |    3     |
|   24   | Song-24 |  Album-4  |    Song-024    | Band-5  |  2010-12-21   | Rock  |   ...  |  4:30  | 4 MB |  124  |  34   |    4     |
|   25   | Song-25 |   [null]  |    Song-025    | Band-6  |  2010-12-22   | Rock  |   ...  |  5:30  | 5 MB |  125  |  35   |    5     |
|   26   | Song-26 |   [null]  |    Song-026    | Band-6  |  2010-12-22   | Rock  |   ...  |  6:30  | 6 MB |  126  |  36   |    6     |
|   27   | Song-27 |  Album-5  |    Song-027    | Band-7  |  2010-12-22   | POP   |   ...  |  4:30  | 4 MB |  127  |  37   |    7     |
|   28   | Song-28 |  Album-5  |    Song-028    | Band-7  |  2010-12-22   | PUNK  |   ...  |  3:30  | 3 MB |  128  |  38   |    8     |
|   29   | Song-29 |   [null]  |    Song-029    | Band-7  |  2010-12-23   | Rock  |   ...  |  2:30  | 2 MB |  129  |  39   |    9     |
|   30   | Song-30 |  Album-6  |    Song-030    | Band-8  |  2010-12-25   | Rock  |   ...  |  5:30  | 5 MB |  130  |  40   |    0     |
+--------+---------+-----------+----------------+---------+---------------+-------+--------+--------+------+-------+-------+----------+

专辑表:

+--------+-----------+----------------+---------+---------------+-------------------+--------+-------+----------+
| Row ID | Album ID  |    Album Name  |   Band  | Date Released |       Genre       | Lenght | Likes | Dislikes |
+--------+-----------+----------------+---------+---------------+-------------------+--------+-------+----------+
|    1   |  Album-1  |    Album One   | Band-1  |  2010-12-15   |       Rock        | 13:30  |  101  |    31    |
|    2   |  Album-2  |    Album Two   | Band-1  |  2010-12-17   |      POP/RAP      |  9:00  |  102  |    32    |
|    3   |  Album-3  |   Album Three  | Band-2  |  2010-12-17   | Rock/Punk/POP/RAP | 24:00  |  103  |    33    |
|    4   |  Album-4  |    Album Four  | Band-5  |  2010-12-21   |       Rock        | 10:30  |  104  |    34    |
|    5   |  Album-5  |    Album Five  | Band-7  |  2010-12-22   |      Punk/POP     |  8:00  |  105  |    35    |
|    6   |  Album-6  |    Album Six   | Band-8  |  2010-12-25   |       Rock        |  5:30  |  106  |    36    |
+--------+-----------+----------------+---------+---------------+-------------------+--------+-------+----------+

如果我只想获取最新的五张专辑和/或单曲,这意味着结果将是(从最新到最老的顺序):

通过“名称”列,我的意思是“单个名称或专辑名称”

第 1 页:

+-----------------+---------+-----------------+-------------------+----------+-----------+--------+--------+------+-------+----------+-------+
|       Name      |   Band  |  Date Released  |       Genre       | IsAlbum? | IsSingle? | Lyrics | Lenght | Size | Likes | Dislikes | Plays |
+-----------------+---------+-----------------+-------------------+----------+-----------+--------+--------+------+-------+----------+-------+
|  Album Six      | Band-8  |    2010-12-25   |       Rock        |   YES    |     NO    |    -   |  5:30  |  --  |  106  |    36    |   -   |
|  Song 29        | Band-7  |    2010-12-23   |       Rock        |    NO    |    YES    |   ...  |  2:30  | 2 MB |   39  |     9    |  129  |
|  Album Five     | Band-7  |    2010-12-22   |      Punk/POP     |   YES    |     NO    |    -   |  8:00  |  --  |  105  |    35    |   -   |
|  Song 26        | Band-6  |    2010-12-22   |       Rock        |    NO    |    YES    |   ...  |  6:30  | 6 MB |   36  |     6    |  126  |
|  song 25        | Band-6  |    2010-12-22   |       Rock        |    NO    |    YES    |   ...  |  5:30  | 5 MB |   35  |     5    |  125  |
+-----------------+---------+-----------------+-------------------+----------+-----------+--------+--------+------+-------+----------+-------+

如果我想只取最近五张(上表中的那些)专辑和/或单曲之后的最近五张,结果将按从最新到最旧的顺序排列):

第2页:

+-----------------+---------+-----------------+-------------------+----------+-----------+--------+--------+------+-------+----------+-------+
|       Name      |   Band  |  Date Released  |       Genre       | IsAlbum? | IsSingle? | Lyrics | Lenght | Size | Likes | Dislikes | Plays |
+-----------------+---------+-----------------+-------------------+----------+-----------+--------+--------+------+-------+----------+-------+
|  Album Four     | Band-5  |    2010-12-21   |       Rock        |   YES    |     NO    |    -   | 10:30  |  --  |  104  |    34    |   -   |
|  Song 21        | Band-5  |    2010-12-20   |       Rock        |    NO    |    YES    |   ...  |  6:30  | 6 MB |   31  |     1    |  121  |
|  Song 20        | Band-5  |    2010-12-20   |       Rock        |    NO    |    YES    |   ...  |  5:30  | 5 MB |   30  |     0    |  120  |
|  Song 19        | Band-5  |    2010-12-20   |       Rock        |    NO    |    YES    |   ...  |  4:30  | 4 MB |   29  |     9    |  119  |
|  song 18        | Band-4  |    2010-12-19   |        POP        |    NO    |    YES    |   ...  |  2:30  | 2 MB |   28  |     8    |  118  |
+-----------------+---------+-----------------+-------------------+----------+-----------+--------+--------+------+-------+----------+-------+

他们之前的五个将是:

第 3 页:

+-----------------+---------+-----------------+-------------------+----------+-----------+--------+--------+------+-------+----------+-------+
|       Name      |   Band  |  Date Released  |       Genre       | IsAlbum? | IsSingle? | Lyrics | Lenght | Size | Likes | Dislikes | Plays |
+-----------------+---------+-----------------+-------------------+----------+-----------+--------+--------+------+-------+----------+-------+
|  Song 17        | Band-4  |    2010-12-19   |        POP        |    NO    |    YES    |   ...  |  3:30  | 3 MB |   27  |     7    |  117  |
|  Song 16        | Band-3  |    2010-12-19   |       Rock        |    NO    |    YES    |   ...  |  4:30  | 4 MB |   26  |     6    |  116  |
|  Song 15        | Band-3  |    2010-12-19   |       Rock        |    NO    |    YES    |   ...  |  5:30  | 5 MB |   25  |     5    |  115  |
|  Song 14        | Band-3  |    2010-12-18   |       Rock        |    NO    |    YES    |   ...  |  6:30  | 6 MB |   24  |     4    |  114  |
|  song 13        | Band-2  |    2010-12-18   |       Rock        |    NO    |    YES    |   ...  |  2:30  | 2 MB |   23  |     3    |  113  |
+-----------------+---------+-----------------+-------------------+----------+-----------+--------+--------+------+-------+----------+-------+

和之前的五个:

第 4 页:

+-----------------+---------+-----------------+-------------------+----------+-----------+--------+--------+------+-------+----------+-------+
|       Name      |   Band  |  Date Released  |       Genre       | IsAlbum? | IsSingle? | Lyrics | Lenght | Size | Likes | Dislikes | Plays |
+-----------------+---------+-----------------+-------------------+----------+-----------+--------+--------+------+-------+----------+-------+
|  Song 12        | Band-2  |    2010-12-18   |       Rock        |    NO    |    YES    |   ...  |  3:30  | 3 MB |   22  |     2    |  112  |
|  Album Three    | Band-2  |    2010-12-17   | Rock/Punk/POP/RAP |   YES    |     NO    |    -   | 24:00  |  --  |  103  |    33    |   -   |
|  Song 7         | Band-2  |    2010-12-17   |       Rock        |    NO    |    YES    |   ...  |  3:30  | 3 MB |   17  |     7    |  107  |
|  Album Two      | Band-1  |    2010-12-17   |      POP/RAP      |   YES    |     NO    |    -   |  9:00  |  --  |  102  |    32    |   -   |
|  song 4         | Band-1  |    2010-12-16   |       Rock        |    NO    |    YES    |   ...  |  4:30  | 4 MB |   14  |     4    |  104  |
+-----------------+---------+-----------------+-------------------+----------+-----------+--------+--------+------+-------+----------+-------+

在最后一个表(或页面)中:

第 5 页:

+-----------------+---------+-----------------+-------------------+----------+-----------+--------+--------+------+-------+----------+-------+
|       Name      |   Band  |  Date Released  |       Genre       | IsAlbum? | IsSingle? | Lyrics | Lenght | Size | Likes | Dislikes | Plays |
+-----------------+---------+-----------------+-------------------+----------+-----------+--------+--------+------+-------+----------+-------+
|  Album One      | Band-1  |    2010-12-15   |       Rock        |   YES    |     NO    |    -   | 13:00  |  --  |  101  |    31    |   -   |
+-----------------+---------+-----------------+-------------------+----------+-----------+--------+--------+------+-------+----------+-------+

但是,问题是当我这样做时,“LIMIT X , Y”中的 X 和 Y 不会是 X=0,Y=5 或 X=5,Y=5 或 X=10,Y=5 并且依此类推...另外,我需要结果中每个表的不同列(查看示例)...

所以,我的问题是:我怎样才能做我上面在 PHP 和 SQL (MySQL) 中所做的事情?


对不起我的英语 ,提前谢谢

4

1 回答 1

1

我在想它可以通过查询来完成,而 php 代码只会显示信息并执行分页查询

查询可能类似于:

SELECT IF(Album_ID IS NULL,s.Song_Name,a.Album_Name) as name,IF(Album_ID IS NULL,s.Date_Released,a.Date_Released) as datereleased
FROM songs s LEFT JOIN albums a ON (s.Album_ID = a.Row_ID)
GROUP BY 1,2
ORDER BY 2 DESC,1
LIMIT 0,5; /* for the first page */

这个想法是使用该IF()函数,每当您在两个表之间发生冲突(例如显示歌曲名称或专辑名称)并且其余列将具有 NULL 作为值时,您可以使用 php 来更改它(列如“专辑”)

其余的只是 php 执行查询、显示结果、链接到下一页等。

祝你好运

于 2011-04-10T09:03:55.320 回答