简体   繁体   English

两个不同的表加入mysql

[英]two different table join mysql

I have 2 table which one: 我有2张桌子,其中一张:

Albums: 专辑:

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 ;

Music: 音乐:

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 ;

I want join that's table and ordered by releaseDate. 我想加入该表并按releaseDate排序。 It's possible? 这是可能的? Sorry for my english. 对不起我的英语不好。

RESULT: Now I get some result: 结果:现在我得到一些结果:

+-----------------------------------------------+-------------------------+-------------+
| 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  |
+-----------------------------------------------+-------------------------+-------------+

I want: 我想要:

+-----------------------------------------------+-------------------------+-------------+
| 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  |
+-----------------------------------------------+-------------------------+-------------+

You should do some studying / playing with JOIN . 您应该和JOIN学习/玩耍。 There's a few different types (INNER JOIN, LEFT JOIN). 有几种不同的类型(INNER JOIN,LEFT JOIN)。

Here's a simple example to get you started: 这是一个入门的简单示例:

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

Or, if you need music and only the album when it matches: 或者,如果您需要音乐,并且仅在匹配时才需要专辑:

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

There are two very distinct parts that your output appears to consist of: 您的输出似乎包含两个非常不同的部分:

  • albums and their release dates; 专辑及其发行日期;

  • album-less tracks and their release dates. 无专辑曲目及其发行日期。

Each part coming from a different table, this strikes me as a classic example of a union , not a join, of two sets: 每个部分都来自不同的表,这使我成为两个集合的联合而不是联接的经典示例:

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
;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM