MySql中的if条件时如何应用案例

时间:2018-12-11 11:40:12

标签: mysql sql

我在下面提到了两个表:

表1

ID       Key
TTT-1    atre-0-1
TTT-2    atrt-4-2
TTT-3    attr-1-3
TTT-4    aert-2-5

表2

Key          Red1       Red2
atre-0-1     10         25
atre-0-1     15         07
atre-0-1     16         19
atrt-4-2     11         21
atrt-4-2     07         22
attr-1-3     -4         3
attr-1-3     33         11
attr-1-3     50         45
aert-2-5     -5         2
aert-2-5     -4         1

通过利用上述拖曳表,我希望在下述情况下按Red1提取Red2ID组的最小值。

  • 对于任何IDRed1值在-5到5之间的任何Red2,请考虑其他最大为5的值,如果只有一个条目可用于ID的值在-5到5之间,而对于最低标准,仅考虑-5到5。

必需的输出:

ID        Number
TTT-1     07
TTT-2     07
TTT-3     11
TTT-4     -5

我写了下面提到的查询,但是不能正常工作。

select t1.ID,
case when min(t2.Red1)<min(t2.Red2) then min(t2.Red1) else min(t2.Red2) End as `Number`
from Table1 t1
left join Table2 t2 on t1.key=t2.key
group by t1.ID;

2 个答案:

答案 0 :(得分:1)

以下查询应解决此问题:

SELECT t1.ID,
CASE WHEN min(t2.Red1)<min(t2.Red2) THEN t2.min_red1 ELSE t2.min_red2 END AS 
`Number`
FROM Table1 t1
LEFT JOIN 
(
SELECT 
    key,
    Red1
    Red2
    min(Red1) AS min_red1,
    min(Red2) AS min_red2
FROM Table2 ) AS t2
ON t1.key=t2.key
GROUP BY t1.ID;

答案 1 :(得分:0)

您可以使用下面的SQL,这是您期望的示例。

查询:

SELECT 
    a.ID,
    CASE
        WHEN
            LEAST(MIN(b.red1), MIN(b.red2)) BETWEEN - 5 AND 5
        THEN
            IFNULL(LEAST(MIN(CASE
                                WHEN red1 NOT BETWEEN - 5 AND 5 THEN red1
                            END),
                            MIN(CASE
                                WHEN red2 NOT BETWEEN - 5 AND 5 THEN red2
                            END)),
                    LEAST(MIN(b.red1), MIN(b.red2)))
        ELSE LEAST(MIN(b.red1), MIN(b.red2))
    END AS `Number`
FROM
    table1 a
        JOIN
    table2 b ON a.key = b.key
GROUP BY a.ID;

示例:

mysql> create table table1(ID varchar(20),`key` varchar(20));
Query OK, 0 rows affected (0.23 sec)

mysql> insert into table1 values 
    -> ('TTT-1','atre-0-1'),
    -> ('TTT-2','atrt-4-2'),
    -> ('TTT-3','attr-1-3'),
    -> ('TTT-4','aert-2-5');
Query OK, 4 rows affected (0.27 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> 
mysql> create table table2(`key` varchar(20),red1 int(5),red2 int(5));
Query OK, 0 rows affected (0.30 sec)

mysql> insert into table2 values
    -> ('atre-0-1',10,25),
    -> ('atre-0-1',15,07),
    -> ('atre-0-1',16,19),
    -> ('atrt-4-2',11,21),
    -> ('atrt-4-2',07,22),
    -> ('attr-1-3',-4,3),
    -> ('attr-1-3',33,11),
    -> ('attr-1-3',50,45),
    -> ('aert-2-5',-5,2),
    -> ('aert-2-5',-4,1);
Query OK, 10 rows affected (0.29 sec)
Records: 10  Duplicates: 0  Warnings: 0

mysql> 
mysql> select * from table1;
+-------+----------+
| ID    | key      |
+-------+----------+
| TTT-1 | atre-0-1 |
| TTT-2 | atrt-4-2 |
| TTT-3 | attr-1-3 |
| TTT-4 | aert-2-5 |
+-------+----------+
4 rows in set (0.20 sec)

mysql> select * from table2;
+----------+------+------+
| key      | red1 | red2 |
+----------+------+------+
| atre-0-1 |   10 |   25 |
| atre-0-1 |   15 |    7 |
| atre-0-1 |   16 |   19 |
| atrt-4-2 |   11 |   21 |
| atrt-4-2 |    7 |   22 |
| attr-1-3 |   -4 |    3 |
| attr-1-3 |   33 |   11 |
| attr-1-3 |   50 |   45 |
| aert-2-5 |   -5 |    2 |
| aert-2-5 |   -4 |    1 |
+----------+------+------+
10 rows in set (0.21 sec)

mysql> 
mysql> SELECT 
    ->     a.ID,
    ->     CASE
    ->         WHEN
    ->             LEAST(MIN(b.red1), MIN(b.red2)) BETWEEN - 5 AND 5
    ->         THEN
    ->             IFNULL(LEAST(MIN(CASE
    ->                                 WHEN red1 NOT BETWEEN - 5 AND 5 THEN red1
    ->                             END),
    ->                             MIN(CASE
    ->                                 WHEN red2 NOT BETWEEN - 5 AND 5 THEN red2
    ->                             END)),
    ->                     LEAST(MIN(b.red1), MIN(b.red2)))
    ->         ELSE LEAST(MIN(b.red1), MIN(b.red2))
    ->     END AS `Number`
    -> FROM
    ->     table1 a
    ->         JOIN
    ->     table2 b ON a.key = b.key
    -> GROUP BY a.ID;
+-------+--------+
| ID    | Number |
+-------+--------+
| TTT-1 |      7 |
| TTT-2 |      7 |
| TTT-3 |     11 |
| TTT-4 |     -5 |
+-------+--------+
4 rows in set (0.28 sec)