2014-02-22 215 views
1

我有兩個表,一個:兩個不同的表連接mysql的

專輯:

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

顯示的一些數據和想要的結果。 – wumpz

回答

1

有兩個截然不同的部分,你的輸出似乎包括:

  • 專輯及其發行日期;

  • 無專輯曲目及其發行日期。

從不同的表上的每一個部分,這在我看來是一個工會一個典型的例子,而不是加入,兩套:

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

但我有音樂沒有專輯和albumId = null – Neron7

+0

@ Neron7 - 所以切換它,所以它是'SELECT ...從音樂左加入專輯...' –

+0

我玩JOIN但我不給正確的結果。 – Neron7