两个不同的表连接mysql

时间:2014-02-22 23:34:25

标签: mysql sql join

我有2张表,其中一张:

相册:

CREATE TABLE IF NOT EXISTS `albums` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(64) NOT NULL,
  `singer` varchar(64) NOT NULL,
  `year` int(11) NOT NULL,
  `releaseDate` date DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `categoryId` (`categoryId`),
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;

音乐:

CREATE TABLE IF NOT EXISTS `musics` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(128) NOT NULL,
  `singer` varchar(128) NOT NULL,
  `genre` varchar(128) NOT NULL,
  `albumId` int(11) DEFAULT NULL,
  `year` int(4) NOT NULL,
  `releaseDate` date DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `categoryId` (`categoryId`),
  KEY `albumId` (`albumId`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=16 ;

我想加入那个表并按releaseDate排序。这是可能的? 抱歉我的英文。

结果: 现在我得到了一些结果:

+-----------------------------------------------+-------------------------+-------------+
| albums_name                                   | musics_name             | releaseDate |
+-----------------------------------------------+-------------------------+-------------+
| The Artificial Theory For The Dramatic Beauty | K                       | NULL        |
| The Artificial Theory For The Dramatic Beauty | Fiction In Hope         | NULL        |
| The Artificial Theory For The Dramatic Beauty | Chemicarium             | NULL        |
| The Artificial Theory For The Dramatic Beauty | Voice                   | NULL        |
| The Artificial Theory For The Dramatic Beauty | Blue                    | NULL        |
| The Artificial Theory For The Dramatic Beauty | Mirror                  | NULL        |
| The Artificial Theory For The Dramatic Beauty | If You Want To Wake Up? | NULL        |
| The Artificial Theory For The Dramatic Beauty | Interlude               | NULL        |
| NULL                                          | Everything At Once      | 2010-11-11  |
| NULL                                          | Blue Freightliner       | 2011-11-11  |
+-----------------------------------------------+-------------------------+-------------+

我想:

+-----------------------------------------------+-------------------------+-------------+
| albums_name                                   | musics_name             | releaseDate |
+-----------------------------------------------+-------------------------+-------------+
| The Artificial Theory For The Dramatic Beauty | NULL                    | 2009-11-11  |
| NULL                                          | Everything At Once      | 2010-11-11  |
| NULL                                          | Blue Freightliner       | 2011-11-11  |
+-----------------------------------------------+-------------------------+-------------+

2 个答案:

答案 0 :(得分:3)

你应该学习/玩JOIN。有几种不同的类型(INNER JOIN,LEFT JOIN)。

这是一个让你入门的简单例子:

SELECT albums.name AS albums.name, musics.name AS musics_name, musics.releaseDate 
    FROM albums 
    LEFT JOIN musics ON albums.id = musics.albumId 
    ORDER BY musics.releaseDate

或者,如果你需要音乐,只有匹配的专辑:

SELECT albums.name AS albums.name, musics.name AS musics_name, musics.releaseDate 
    FROM musics 
    LEFT JOIN albums ON musics.albumId = albums.id
    ORDER BY musics.releaseDate

答案 1 :(得分:1)

您的输出有两个截然不同的部分:

  • 相册及其发布日期;

  • 无专辑曲目及其发布日期。

每个部分来自不同的表格,这对我来说是一个典型的 union 示例,而不是两个集合的连接:

SELECT
  name AS albums_name,
  NULL AS musics_name,
  releaseDate
FROM albums

UNION ALL

SELECT
  NULL AS albums_name,
  name AS musics_name,
  releaseDate
FROM musics
WHERE
  album_id IS NULL

ORDER BY
  releaseDate ASC
;