【发布时间】:2021-04-06 11:18:59
【问题描述】:
用例:我正在处理下表的 sql 查询。我的架构中有 2 个表,即cropdetails 和 translationcropdetails。 Cropdetails 保存基本的公共信息,translationcropdetails 保存多种语言的翻译。
我正在编写一个查询,该查询将返回来自 translationcropdetails 的所有记录的列表,语言 id =english_lang_id>,并返回表中存在多少种语言的记录。
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 Boot,这是一个分页查询,我将作为本机查询执行。 目前,下面的查询将只获取英文记录列表。
@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);
需要达到上述结果的方法。
【问题讨论】: