Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

Tulis kueri asli sql dengan gabungan kiri dan pagination di hibernate (springboot)

Saya menyelesaikannya...

Kueri harus terlihat seperti ini:

@Query(
        value = "SELECT * FROM \n" +
                "(institution INNER JOIN user ON institution.user_id = user.id) \n" +
                "LEFT JOIN \n" +
                "(SELECT * FROM \n" +
                "building_institutions \n" +
                "WHERE building_id = :userId)\n" +
                " AS reserved_institutions \n" +
                "ON reserved_institutions.institutions_user_id = kits_nwt.institution.user_id \n" +
                " where reserved_institutions.institutions_user_id IS null \n"
                + "ORDER BY ?#{#pageable}"
        ,
        countQuery = "SELECT count(*) FROM \n" +
                "(institution INNER JOIN user ON institution.user_id = user.id) \n" +
                "LEFT JOIN \n" +
                "(SELECT * FROM \n" +
                "building_institutions \n" +
                "WHERE building_id =:userId)\n" +
                " AS reserved_institutions \n" +
                "ON reserved_institutions.institutions_user_id = kits_nwt.institution.user_id \n" +
                "where reserved_institutions.institutions_user_id IS null \n" +
                "ORDER BY ?#{#pageable}",
        nativeQuery = true)
Page<Institution> findPotentialInstitutionsByBuildingId(@Param("userId") Long userId, Pageable pageable);

Saya mendapatkan kesalahan pada baris 4 dekat WHERE, ketika kueri saya terlihat seperti ini:

@Query(
        value = "SELECT username, password, description, location, title, user_id FROM (institution INNER JOIN user ON institution.user_id = user.id) LEFT JOIN\n" +
                "(SELECT * FROM building_institutions WHERE building_institutions.building_id = 1) AS reserved_institutions\n" +
                "ON reserved_institutions.institutions_user_id = kits_nwt.institution.user_id\n" +
                "WHERE reserved_institutions.institutions_user_id IS null ORDER BY ?#{#pageable}",
        nativeQuery = true)
Page<Institution> findPotentialInstitutionsByBuildingId(Long userId, Pageable pageable);

dan itu karena saya tidak menambahkan countQuery parameter ke @Query anotasi.

Terima kasih banyak kepada Anda semua yang mencoba membantu.

Saya berharap bahwa saya menyelamatkan orang lain berjam-jam kesengsaraan.

Bersulang! :)




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Cara Memperbarui Banyak Kolom di MySQL

  2. butuh bantuan untuk mengoptimalkan meta_query wordpress

  3. Pemicu di mysql menyebabkan kesalahan

  4. Pencarian teks lengkap MySql di PHP menggunakan string yang mengandung kata kunci

  5. Bagaimana cara menangani Kesalahan Kunci Asing MySQL di Php?