为什么在工作台中执行但不使用JDBC调用时,此mysql查询工作正常?

时间:2018-06-21 02:02:26

标签: mysql jdbc workbench

看起来我有一个类似的查询:

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(Spring-Java)中运行,我会感到奇怪

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;

1 个答案:

答案 0 :(得分:0)

您确定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源代码将帮助我们提供更好的解决方案。