优先级选择SQL

时间:2017-11-06 11:27:38

标签: mysql sql

table:blog_post

+------+--------+-------------+-------------+----------+
| id   | type   | added       | completed   | status   |
+------+--------+-------------+-------------+----------+
| 2    | page   | 1505221377  | 1           | 1        |
+------+--------+-------------+-------------+----------+
| 4    | page   | 1505231377  | 1           | 1        |
+------+--------+-------------+-------------+----------+

表:blog_post_description

+--------------+---------------+-------------------------+-----------------------+-----------------+------------+---------+
| article_id   | language_id   | title                   | permalink             | description     | keywords   | image   |
+--------------+---------------+-------------------------+-----------------------+-----------------+------------+---------+
| 2            | 1             | Privacy policy          | privacy-policy        | Privacy Policy  | privacy    | NULL    |
+--------------+---------------+-------------------------+-----------------------+-----------------+------------+---------+
| 2            | 2             | Politica sulla          | politica-sulla        | description     | privacy    | NULL    |
+--------------+---------------+-------------------------+-----------------------+-----------------+------------+---------+
| 2            | 3             | Política de privacidad  | politica-privacidad   | description     | privacy    | NULL    |
+--------------+---------------+-------------------------+-----------------------+-----------------+------------+---------+
| 4            | 1             | eng                     | eng                   | description     | eng        | NULL    |
+--------------+---------------+-------------------------+-----------------------+-----------------+------------+---------+
| 4            | 2             | it                      | it                    | description     | it         | NULL    |
+--------------+---------------+-------------------------+-----------------------+-----------------+------------+---------+

我想要的结果:(language_id = 3具有优先级,如果不可用,则为language_id 3,按language_id ASC排序)

+------+--------------------------+-------------+----------+
| id   | title                    | added       | status   |
+------+--------------------------+-------------+----------+
| 2    | Política de privacidad   | 1505221377  | 1        |
+------+--------------------------+-------------+----------+
| 4    | eng                      | 1505231377  | 1        |
+------+--------------------------+-------------+----------+

我试过

SELECT bp.`id`, bp.`type`, bpd.`title`, bpd.`permalink`, bpd.`description`, bpd.`keywords`, bpd.`image`, bp.`added`, bp.`status`
FROM `blog_post` AS `bp`
LEFT JOIN `blog_post_description` AS `bpd` ON (bp.`id` = bpd.`article_id`)
WHERE (bpd.`language_id` = 3 OR bpd.`language_id` IS NOT NULL) GROUP BY bpd.`article_id`

但输出正在选择language_id 1(英文)

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用相关子查询执行此操作:

SELECT . . .
FROM blog_post bp LEFT JOIN 
     blog_post_description bpd
     ON bp.id = bpd.article_id AND
        bpd.language_id = (SELECT bpd2.language_id
                           FROM blog_post_description bpd2
                           WHERE bpd2.article_id = bpd.article_id
                           ORDER BY (bpd2.language_id = 3) DESC,
                                    bpd2.language_id
                           LIMIT 1
                          );