MYSQL:如何从2个连接表中下载最后一个

时间:2015-03-23 18:31:38

标签: php mysql database join left-join

这是我的第一个问题,所以请保持友善:)

我有这个:

mysql> select cat_id, cat_name from phpbb_dm_eds_cat;
+--------+----------+
| cat_id | cat_name |
+--------+----------+
|      9 | catx     |
|     10 | cat2     |
|     11 | test     |
+--------+----------+

mysql> select subcat_id, subcat_name from phpbb_dm_eds_subcat;
+-----------+--------------+
| subcat_id | subcat_name  |
+-----------+--------------+
|        39 | aaa          |
|        40 | xxx111       |
|        41 | TESTXX       |
|        42 | xxa          |
+-----------+--------------+

这是下载表:

mysql> select download_id, download_title, download_cat_id from phpbb_dm_eds;
+-------------+----------------+-----------------+
| download_id | download_title | download_cat_id |
+-------------+----------------+-----------------+
|           3 | s              |               9 |
|           5 | raver          |              41 |
|           6 | hans           |              10 |
|           7 | readme         |              42 |
+-------------+----------------+-----------------+

现在查询:

mysql>
SELECT bc.cat_name, bc.cat_id,
    COUNT(bd.download_id) AS number_downloads,
    MAX(bd.last_changed_time) AS last_download
FROM phpbb_dm_eds_cat bc
LEFT JOIN phpbb_dm_eds bd ON bd.download_cat_id = bc.cat_id
GROUP BY bc.cat_id   ;

+----------+--------+------------------+---------------+
| cat_name | cat_id | number_downloads | last_download |
+----------+--------+------------------+---------------+
| catx     |      9 |                1 |    1427072549 |
| cat2     |     10 |                1 |    1427125950 |
| test     |     11 |                0 |          NULL |
+----------+--------+------------------+---------------+

'测试' category有一个子类别,它在subcat表中有一个下载,类别是catx'和' cat2'也有下载,上次下载显示正确,但我想在subcat中测试last_download' test'也要显示

如何编写查询?

3 个答案:

答案 0 :(得分:0)

你不清楚自己想要什么,但可能是:

SELECT bc.cat_name, bc.cat_id,
    COUNT(bd.download_id) AS number_downloads,
    MAX(bd.last_changed_time) AS last_download
FROM (
        select cat_id, cat_name from phpbb_dm_eds_cat
    union
        select subcat_id as cat_id, subcat_name as cat_name
        from phpbb_dm_eds_subcat
    ) as bc
LEFT JOIN phpbb_dm_eds bd ON bd.download_cat_id = bc.cat_id
GROUP BY bc.cat_id   ;

(您的SELECT使用MySQL扩展,因为cat_id既不在GROUP BY中也不在聚合中。)

答案 1 :(得分:0)

从你的笔记“'测试'类别有一个子类别”,我建议cat表“phpbb_dm_eds_cat”和sub_cat表之间应该有一个关系 “phpbb_dm_eds_subcat”。

像这样:
表名
phpbb_dm_eds_cat

(CAT_ID,cat_name)

表名
phpbb_dm_eds_subcat

(cat_id引用表phpbb_dm_eds_cat(cat_id),subcat_id,subcat_name)

答案 2 :(得分:0)

可悲的是,你的查询不起作用,它给了我所有子类,如下:

+----------+--------+------------------+---------------+
| cat_name | cat_id | number_downloads | last_download |
+----------+--------+------------------+---------------+
| catx     |      9 |                1 |    1427072549 |
| cat2     |     10 |                1 |    1427125950 |
| test     |     11 |                0 |          NULL |
| aaa      |     39 |                0 |          NULL |
| xxx111   |     40 |                0 |          NULL |
| TESTXX   |     41 |                1 |    1427123713 |
| xxa      |     42 |                1 |    1427136789 |
+----------+--------+------------------+---------------+
7 rows in set (0,00 sec)

我不希望显示子类,我只想从subcat上次下载,这应该是正确的输出:

+----------+--------+------------------+---------------+
| cat_name | cat_id | number_downloads | last_download |
+----------+--------+------------------+---------------+
| catx     |      9 |                1 |    1427072549 |
| cat2     |     10 |                1 |    1427125950 |
| test     |     11 |                1 |    1427213321 |
+----------+--------+------------------+---------------+
3 rows in set (0,00 sec)