2015-10-09 41 views
2

查詢時遇到一些麻煩。我試圖創建從表中選擇deposit_type_language_id = 3或如果行不存在1(作爲後備)與deposit_type_id分組。MySQL從備用組中選擇

+-----------------+--------------------------+-------------------+ 
    | deposit_type_id | deposit_type_language_id | deposit_type_name | 
    +-----------------+--------------------------+-------------------+ 
    |    1 |      1 | jewellery   | 
    |    1 |      2 | bijuterii   | 
    |    1 |      3 | šperky   | 
    |    2 |      1 | equipment   | 
    |    2 |      2 | tehnica   | 
    +-----------------+--------------------------+-------------------+ 

我試圖用下面的查詢:

SELECT t1.* from deposit_type AS t1 
     INNER JOIN 
      (SELECT deposit_type_id, min(deposit_type_language_id) from 
        deposit_type group by deposit_type_id) AS t2 
     ON t1.deposit_type_id=t2.deposit_type_id group by deposit_type_id; 

輸出是:

+-----------------+--------------------------+-------------------+ 
| deposit_type_id | deposit_type_language_id | deposit_type_name | 
+-----------------+--------------------------+-------------------+ 
|    1 |      1 | jewellery   | 
|    2 |      1 | equipment   | 
+-----------------+--------------------------+-------------------+ 

但我試圖實現類似如下:

+-----------------+--------------------------+-------------------+ 
| deposit_type_id | deposit_type_language_id | deposit_type_name | 
+-----------------+--------------------------+-------------------+ 
|    1 |      3 | šperky   | 
|    2 |      1 | equipment   | 
+-----------------+--------------------------+-------------------+ 

在哪裏我的錯?

表結構:

deposit_type | CREATE TABLE `deposit_type` (
    `deposit_type_id` smallint(5) unsigned NOT NULL, 
    `deposit_type_language_id` smallint(5) unsigned NOT NULL, 
    `deposit_type_name` varchar(96) NOT NULL, 
    PRIMARY KEY (`deposit_type_id`,`deposit_type_language_id`), 
    KEY `deposit_type_language_id` (`deposit_type_language_id`), 
    CONSTRAINT `deposit_type_ibfk_1` FOREIGN KEY (`deposit_type_language_id`) REFERENCES `language` (`language_id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 | 

回答

1

仇敵會討厭......

SELECT x.deposit_type_id 
    , COALESCE(y.deposit_type_language_id,x.deposit_type_language_id) deposit_type_language_id 
    , COALESCE(y.deposit_type_name,x.deposit_type_name) deposit_type_name 
    FROM deposit_type x 
    LEFT 
    JOIN deposit_type y 
    ON y.deposit_type_id = x.deposit_type_id 
    AND y.deposit_type_language_id = 3 
WHERE x.deposit_type_language_id = 1; 
+0

@Strawberrry,嗨,哥們你的答案似乎是正確的。我正在學習你的答案,謝謝! – vit

+0

如果你想用兩個字解釋你的想法,請。 – vit

+0

@vit如果存在有效的連接,則左[outer]連接將返回值,否則返回NULL。然後COALESCE簡單地忽略NULL值。 – Strawberry

0

@CFreitas,我恨這個男人誰投下來你。你回答不正確,但給了我一個想法。我們應該增加一個比較,見下文。

SELECT t1.* from deposit_type AS t1 
     INNER JOIN 
       (SELECT deposit_type_id, max(deposit_type_language_id) as dtl FROM deposit_type 
        WHERE deposit_type_language_id=3 or deposit_type_language_id=1 group by deposit_type_id) AS t2 
       ON t1.deposit_type_id=t2.deposit_type_id and t1.deposit_type_language_id=t2.dtl group by deposit_type_id; 

輸出是:

+-----------------+--------------------------+-------------------+ 
| deposit_type_id | deposit_type_language_id | deposit_type_name | 
+-----------------+--------------------------+-------------------+ 
|    1 |      3 | šperky   | 
|    2 |      1 | equipment   | 
+-----------------+--------------------------+-------------------+ 
+0

你可以最多投票我呢:) – CFreitas