为什么这个mysql查询在workbench中执行但不使用jdbc调用时工作正常?

ygya80vv  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(426)

我有一个类似的问题:

INSERT INTO groups(
    participants, count1, count2, count3) 
SELECT 
0 as participants,
(count1/participants) as count1,
(count2/participants) as count2,
(count3/participants) as count3 WHERE id = 22;

现在,在表格中你有:

id    participants         count1       count2       count3
22    5                    10000        10000        10000

该查询将插入具有以下值的新行:

id    participants         count1       count2       count3
23    0                    2000         2000         2000

正如我所说的,它在工作台上工作得很好,但是如果我在jdbc(springjava)中运行,我会奇怪地发现这一点

id    participants         count1       count2       count3
23    0                    2000         NULL         NULL

这是实际查询或类似查询,这是原始查询,我只是通过上面的例子来帮助理解问题。

INSERT INTO registrations (
  numberOfParticipants,
  eventId,
  eventName,
  STATUS,
  createdOn,
  source,
  sourceDetail,
  paymentType,
  groupId,
  couponId,
  eventModality,
  eventHour,
  groupName,
  couponCode,
  additionalProductQuantities,
  productsPaid,
  discount,
  insurance,
  baseTotal,
  COMMENT,
  processingFee,
  isVolunteer,
  participantType,
  ipNumber,
  refererCodeId
) 
SELECT 
  0 AS numberOfParticipants,
  (SELECT 
    eventId 
  FROM
    groups 
  WHERE id = 27) AS eventId,
  (SELECT 
    lastEventName 
  FROM
    groups 
  WHERE id = 27) AS eventName,
  STATUS,
  createdOn,
  source,
  sourceDetail,
  paymentType,
  27,
  couponId,
  eventModality,
  eventHour,
  (SELECT 
    NAME 
  FROM
    groups 
  WHERE id = 27) AS groupName,
  couponCode,
  additionalProductQuantities,
  (
    productsPaid / numberOfParticipants
  ) AS productsPaid,
  (discount / numberOfParticipants) AS discount,
  (insurance / numberOfParticipants) AS insurance,
  (baseTotal / numberOfParticipants) AS baseTotal,
  NULL,
  (
    processingFee / numberOfParticipants
  ) AS processingFee,
  isVolunteer,
  participantType,
  ipNumber,
  refererCodeId 
FROM
  registrations 
WHERE id = 15787;
htrmnn0y

htrmnn0y1#

您确定mysql查询的语法正确吗?它应该在查询中包含一个“from”语句。“group”也是mysql中的保留关键字。

INSERT INTO group(
participants, count1, count2, count3) 
SELECT 
0 as participants,
(count1/participants) as count1,
(count2/participants) as count2,
(count3/participants) as count3 
FROM group 
WHERE id = 22;

没有提供足够的信息。这是我能给你的最好的了。也许共享java源代码可以帮助我们提供更好的解决方案。

相关问题