0

我有以下 sql,但目前无法转换。我停在子选择的极限处。谁能帮我?

select( select p2.numnfe from xml_itens as p2
where p2.cod_prod_cli is null
and p2.ean_prod = p1.ean_prod
and p2.cnpj_for = p1.cnpj_for
and p2.cod_prod_for = p1.cod_prod_for
and p2.cfop != 5929 and 6929
LIMIT 1
) as num_nfe,
(SELECT descricao_prod from xml_itens as p2
where p2.cod_prod_cli is null
and p2.ean_prod = p1.ean_prod
and p2.cnpj_for = p1.cnpj_for
and p2.cod_prod_for = p1.cod_prod_for
and p2.cfop != 5929 and 6929
LIMIT 1
) as descricao_prod, 
cnpj_for, ean_prod, cod_prod_for, cod_prod_cli
from xml_itens as p1 
where cod_prod_cli is null
and p1.cfop != 5929 and 6929 
group by cnpj_for, ean_prod, cod_prod_for;

我设法做到了,但及时赶上了从数据库中只返回一条记录。

 String sql = "select ("
            + " select p2.numnfe from XmlItens p2 where p2.codProdCli is null and p2.eanProd = p1.eanProd and p2.cnpjFor = p1.cnpjFor and p2.cfop != 5929 and p2.cfop != 6929) as numnfe, "
            + " p1.cnpjFor, p1.eanProd, p1.codProdFor from XmlItens p1 "
            + " where p1.codProdCli is null "
            + " group by p1.cnpjFor, p1.eanProd, p1.codProdFor";

谢谢

4

2 回答 2

1

我找不到任何可以替代 LMIT sql 的东西。因此,我没有转换查询,而是使用了函数createsqlquery()hibernate 并且它起作用了。

于 2013-10-03T12:41:46.157 回答
0

不管 hibernate 团队在这个变化背后的原因是什么,我都无法告诉你这是多么令人沮丧。

话虽如此,使用 EXIST 关键字获得创意。在此示例中,我们正在寻找 1 的限制。选择 TOP 1 等。 EXIST 可能是您唯一的非本地解决方案。

于 2014-09-11T14:53:37.287 回答