SQL 根据条件查询

SQL query based on condition

用例:我正在为以下 table 进行 sql 查询。我的架构中有 2 个 tables,即 cropdetails 和 translationcropdetails。 Cropdetails 包含基本的公共信息,translationcropdetails 包含多种语言的翻译。

我正在编写一个查询,该查询将 return 来自 translationcropdetails 的所有记录的列表,语言 ID =<english_lang_id> 以及 return 记录在 table.

中以多少种语言显示
   Example output:
   cropid  |  cropname    |    langid1(say eng)                           |    langid2(say spanish)          | langid3
      1       Wheat        true(if translation present for that language)   false(if translation not present)

我在写这篇文章时需要方向。我不确定我是否可以继续使用 case when 运算符。任何输入都有助于实现预期结果。

 +--------------+--------------+------+-----+---------+----------------+
   | Field        | Type         | Null | Key | Default | Extra          |
   +--------------+--------------+------+-----+---------+----------------+
   | cropid       | bigint(20)   | NO   | PRI | NULL    | auto_increment |
   | cropimage    | varchar(255) | NO   |     | NULL    |                |
   | noofdays     | int(11)      | NO   |     | NULL    |                |
   +--------------+--------------+------+-----+---------+----------------+
      
   +-----------------+--------------+------+-----+---------+-------+
    | Field           | Type         | Null | Key | Default | Extra |
   +-----------------+--------------+------+-----+---------+-------+
   | trcropid        | bigint(20)   | NO   | PRI | NULL    |       |
   | cropadvice      | text         | NO   |     | NULL    |       |
   | cropattacks     | text         | NO   |     | NULL    |       |
   | cropdescription | text         | NO   |     | NULL    |       |
   | cropname        | varchar(255) | NO   |     | NULL    |       |
   | cropid          | bigint(20)   | YES  | MUL | NULL    |       |
   | languageid      | bigint(20)   | YES  | MUL | NULL    |       |
   +-----------------+--------------+------+-----+---------+-------+ 

我正在使用 spring 引导,这是一个分页查询,我将作为本机查询执行。 目前,以下查询将仅获取英文记录列表。

   @Query(value = "select cr.* from (select cr.trcropid, cr.cropname, cr.cropid, cr.languageid from "
        + "translationcropdetails cr where cr.languageid=:languageId) cr \n#pageable\n",
        countQuery = "select count(*) from (select cr.trcropid, cr.cropname, cr.cropid, cr.languageid from "
                + "translationcropdetails cr where cr.languageid=:languageId) temp", nativeQuery = true)
public Page<Object[]> getAllCrops(@Param("languageId") Long languageId, Pageable pageable);

需要实现上述结果的方法。

您可以尝试使用此查询,它会构建另一个查询来检索您需要的结果集。这似乎是一种奇怪的方法,但是 MySQL 是动态构建最终查询的唯一方法,不知道您定义了多少种语言。

    SELECT CONCAT(
        'SELECT `translationcropdetails`.cropid', GROUP_CONCAT(', `t_', REPLACE(languageid, '`', '``'), '`.trcropid IS NOT NULL AS `lang_', REPLACE(languageid, '`', '``'), '`' SEPARATOR ''),
 ' FROM `translationcropdetails` ', GROUP_CONCAT('
     LEFT JOIN `translationcropdetails`   AS `t_', REPLACE(languageid, '`', '``'), '`
            ON `translationcropdetails`.cropid = `t_', REPLACE(languageid, '`', '``'), '`.cropid
           AND `t_', REPLACE(languageid, '`', '``'), '`.languageid = ', QUOTE(languageid)
     SEPARATOR ''),
 ' GROUP BY `translationcropdetails`.cropid'
) FROM (SELECT DISTINCT languageid FROM `translationcropdetails`) t;

这是在定义了 3 种语言的情况下从上一个查询构建的查询:

SELECT
    `translationcropdetails`.cropid,
    `t_1`.trcropid IS NOT NULL AS `lang_1`,
    `t_2`.trcropid IS NOT NULL AS `lang_2`,
    `t_3`.trcropid IS NOT NULL AS `lang_3`
FROM `translationcropdetails`
    LEFT JOIN `translationcropdetails` AS `t_1`
        ON `translationcropdetails`.cropid = `t_1`.cropid
        AND `t_1`.languageid = '1'
    LEFT JOIN `translationcropdetails` AS `t_2`
        ON `translationcropdetails`.cropid = `t_2`.cropid
        AND `t_2`.languageid = '2'
    LEFT JOIN `translationcropdetails` AS `t_3`
        ON `translationcropdetails`.cropid = `t_3`.cropid
        AND `t_3`.languageid = '3'
GROUP BY `translationcropdetails`.cropid;

您可以在 SQLFiddle 中看到生成的查询结果,其中我使用了以下虚拟数据:

-- languageid: 1 = English | 2 = Spanish | 3 = French | ...
INSERT INTO translationcropdetails VALUES
  (1, 'adv 1', 'att 1', 'english descr', 'name 1', 1, 1),
  (2, 'adv 1', 'att 1', 'español descr', 'name 1', 1, 2),
  (3, 'adv 2', 'att 2', 'eng descr', 'name 2', 2, 1),
  (4, 'adv 2', 'att 2', 'descr en français', 'name 2', 2, 3),
  (5, 'adv 3', 'att 3', 'eng descr', 'name 3', 3, 1),
  (6, 'adv 4', 'att 4', 'eng descr', 'name 4', 4, 1),
  (7, 'adv 5', 'att 5', 'español descr', 'name 5', 5, 2),
  (8, 'adv 5', 'att 5', 'descr en français', 'name 5', 5, 3)
 ;

输出将是

cropid  lang_1  lang_2  lang_3
------- ------- ------- -------
1       1       1       0
2       1       0       1
3       1       0       0
4       1       0       0
5       0       1       1