Use case: I am working on a sql query for the below tables. I have 2 tables in my schema viz cropdetails and translationcropdetails. Cropdetails holds the basic common info and translationcropdetails holds the translations in multiple languages.
I am working on writing a query that would return a list of all records from translationcropdetails with language id =<english_lang_id> and also return in how many languages the record is present in the 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)
I need direction in which I can move in writing this. I am not sure if I can go ahead with case when operator. Any inputs would be helpful in achieving the expected result.
+--------------+--------------+------+-----+---------+----------------+
| 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 | |
+-----------------+--------------+------+-----+---------+-------+
I am using spring boot and this is a paging query that I will be executing as a native query. Currently the below query will fetch only the list of records in english.
@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);
Need the way to achieve the result as mentioned above.