MySQL从具有后备

时间:2015-10-09 14:26:00

标签: mysql

查询时遇到一些问题。我正在尝试从表中创建select,其中deposit_type_language_id = 3或者如果行不存在1(作为后备),并通过deposit_type_id进行分组。

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

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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         |
+-----------------+--------------------------+-------------------+