嵌套查询返回空集-MySQL

时间:2019-07-07 03:36:43

标签: mysql

嵌套查询-NOT IN(从用户u中选择ub.book_id,在User_Book ub中选择u.id = 5,而u.id = ub.user_id)

我尝试在没有嵌套查询的情况下运行查询,这确实可行,但是当我添加嵌套查询时,得到的是空集。

总代码:



    public Collection<Bookmark> getBooks(boolean isBookmarked, long userId) {
        Collection<Bookmark> result = new ArrayList<>();

                try {
                    Class.forName("com.mysql.cj.jdbc.Driver");
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }

                //Dao connection
                try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useSSL=false", "root",
                        "password"); /* a) Database User Profile: root is who the user is b) Database user password */
                        Statement stmt = conn.createStatement();) /* execute mysql queries */ {

                    String query = "";
                    if (!isBookmarked) {
                        query = "Select b.id, title, image_url, publication_year, GROUP_CONCAT(a.name SEPARATOR ',') AS authors, book_genre_id, " +
                                            "amazon_rating from Book b, Author a, Book_Author ba where b.id = ba.book_id and ba.author_id = a.id and " + 
                                            "b.id NOT IN (select ub.book_id from User u, User_Book ub where u.id = " + userId +
                                            " and u.id = ub.user_id) group by b.id";                
                    } /*else {
                        query = "Select b.id, title, image_url, publication_year, GROUP_CONCAT(a.name SEPARATOR ',') AS authors, book_genre_id, " +
                                "amazon_rating from Book b, Author a, Book_Author ba where b.id = ba.book_id and ba.author_id = a.id and " + 
                                "b.id IN (select ub.book_id from User u, User_Book ub where u.id = " + userId +
                                " and u.id = ub.user_id) group by b.id";
                    }*/

                    ResultSet rs = stmt.executeQuery(query);                

                    while (rs.next()) {

                        long id = rs.getLong("id");
                        String title = rs.getString("title");
                        String imageUrl = rs.getString("image_url");
                        int publicationYear = rs.getInt("publication_year");
//                      String publisher = rs.getString("name");
                        String[] authors = rs.getString("authors").split(",");
                        int genre_id = rs.getInt("book_genre_id");
                        BookGenreEnum genre = BookGenreEnum.values()[genre_id];
                        double amazonRating = rs.getDouble("amazon_rating");


                        System.out.println("id: " + id + ", title: " + title + ", publication year: " + publicationYear + ", authors: " + String.join(", ", authors) + ", genre: " + genre + ", amazonRating: " + amazonRating);

                        Bookmark bookmark = BookmarkManager.getInstance().createBook(id, title, imageUrl, publicationYear, null, authors, genre, amazonRating/*, values[7]*/);
                        result.add(bookmark); 
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }

                return result;
            }


嵌套查询:

Select b.id, title, image_url, publication_year, GROUP_CONCAT(a.name SEPARATOR ',') AS authors, book_genre_id, amazon_rating 
from Book b, Author a, Book_Author ba 
where b.id = ba.book_id and ba.author_id = a.id and b.id 
NOT IN (select ub.book_id from User u, User_Book ub where u.id = 5 and u.id = ub.user_id) group by b.id;

嵌套查询-MySQL结果:

Empty set (0.00 sec)

无需嵌套查询:

Select b.id, title, image_url, publication_year, p.name, GROUP_CONCAT(a.name SEPARATOR ',') AS authors, book_genre_id, amazon_rating, created_date 
from Book b, Publisher p, Author a, Book_Author ba 
where b.publisher_id = p.id and b.id = ba.book_id and ba.author_id = a.id 
group by b.id;

无需嵌套查询-MySQL结果


mysql> select * from book \G;
*************************** 1. row ***************************
                    id: 1
                 title: The Complete Software Developer's Career Guide
             image_url: https://images-na.ssl-images-amazon.com/images/I/51yr12gkjRL._SY346_.jpg
      publication_year: 2017
          publisher_id: 1
         book_genre_id: 10
         amazon_rating: 4.7
   kid_friendly_status: 0
kid_friendly_marked_by: 4
             shared_by: 5
          created_date: 2019-06-22 09:17:29
*************************** 2. row ***************************
                    id: 2
                 title: The Rational Male
             image_url: https://images-na.ssl-images-amazon.com/images/I/51dS%2Bd%2BwZEL._SX322_BO1,204,203,200_.jpg
      publication_year: 2013
          publisher_id: 2
         book_genre_id: 8
         amazon_rating: 4.5
   kid_friendly_status: 1
kid_friendly_marked_by: 3
             shared_by: 5
          created_date: 2019-06-22 09:17:29
*************************** 3. row ***************************
                    id: 3
                 title: The 48 Laws of Power
             image_url: https://images-na.ssl-images-amazon.com/images/I/41EhEN9nsmL._SY346_.jpg
      publication_year: 2000
          publisher_id: 3
         book_genre_id: 5
         amazon_rating: 4.5
   kid_friendly_status: 1
kid_friendly_marked_by: 5
             shared_by: 5
          created_date: 2019-06-22 09:17:29
*************************** 4. row ***************************
                    id: 4
                 title: Rich Dad Poor Dad
             image_url: https://images-na.ssl-images-amazon.com/images/I/51N2wpwLcVL._SX342_.jpg
      publication_year: 2017
          publisher_id: 4
         book_genre_id: 8
         amazon_rating: 4.6
   kid_friendly_status: 0
kid_friendly_marked_by: 3
             shared_by: 4
          created_date: 2019-06-22 09:17:30
*************************** 5. row ***************************
                    id: 5
                 title: Eat That Frog!
             image_url: https://images-na.ssl-images-amazon.com/images/I/51N38zCWljL._SX342_.jpg
      publication_year: 2017
          publisher_id: 5
         book_genre_id: 8
         amazon_rating: 4.6
   kid_friendly_status: 0
kid_friendly_marked_by: 4
             shared_by: 5
          created_date: 2019-06-22 09:17:30

这表示要显示特定用户已为其添加书签的所有图书。

0 个答案:

没有答案