mysql工作台查询返回1行,但jdbc中的同一查询返回0

xpszyzbs  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(275)

在mysql workbench中运行时,我编写了一个返回1行的查询:

SELECT lot_num, block_num, base_price, SUM(price) AS options_price FROM lots AS l 
INNER JOIN models AS m ON l.model_id = m.id 
INNER JOIN lot_options AS lo ON l.id = lo.lot_id 
INNER JOIN options AS o ON lo.option_id = o.id 
GROUP BY l.id

当我尝试在jdbc中运行它时,得到的结果是0。下面是我如何格式化jdbc的字符串:
选择lot\u num、block\u num、base\u price、sum(price)作为选项\u price from lots as l inner join models as m on l.model\u id=m.id inner join lot\u options as lo on l.id=lo.lot\u id inner join options as o on lo.option\u id=o.id group by l.id;
这并没有给我一个错误或任何东西。只是一个空的结果集。
我不知道我在这个问题上哪里出错了!请赐予我你的sql技能!

wixjitnu

wixjitnu1#

我想出来了。我对join语句了解不够。多对多表需要左外联接。我不知道为什么我最初的查询在workbench中工作,但在我的jdbc连接中不工作。这两种方法都有效!

SELECT lot_num, block_num, models.base_price, SUM(o.price) FROM lots 
INNER JOIN models ON models.id = lots.model_id
LEFT OUTER JOIN lot_options AS lo ON lots.id = lo.lot_id 
LEFT OUTER JOIN options AS o ON lo.option_id = o.id
GROUP BY lots.id

相关问题