在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技能!
1条答案
按热度按时间wixjitnu1#
我想出来了。我对join语句了解不够。多对多表需要左外联接。我不知道为什么我最初的查询在workbench中工作,但在我的jdbc连接中不工作。这两种方法都有效!