Mam dwa podzapytania które teoretycznie (zgodnie z moim zamysłem) powinny dawać takie same wyniki, a dają różne:
select ( select order_id
from order_items
where order_items.product_id = products.product_id) as order_id
from products
where 'Heller' = ( select brand_name
from brands
where brands.brand_id = products.brand_id)
select order_id
from order_items
where product_id IN (select product_id
from products
where brand_id IN(select brand_id
from brands
where brand_name = 'Heller'))
W pierwszym przypadku znalezionych tabel jest tylko 3, a w drugim blisko 100.
Podzapytania odnoszą się do przykładowej bazy której schemat znajduje się tutaj:
https://www.sqlservertutorial.net/sql-server-sample-database/
Czy z tymi zapytaniami jest coś nie tak, czy może to jakiś problem z biblioteką Javy?
Podzapytania uruchamiam przez taką funkcję:
public void order_id_subquery_test(){
String sql = "select ( select order_id \n" +
"\t\tfrom order_items\n" +
"\t\twhere order_items.product_id = products.product_id) as order_id\n" +
"from products\n" +
"where 'Heller' = ( select brand_name\n" +
"\t\t\t\t from brands\n" +
"\t\t\t\t where brands.brand_id = products.brand_id)";
try (Connection conn = this.connect();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)){
// loop through the result set
while (rs.next()) {
System.out.println(rs.getInt("order_id"));
}
System.out.println ( "end" );
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}