【问题标题】:Why this mysql query works fine when is executed in Workbench but not using JDBC call?为什么这个 mysql 查询在 Workbench 中执行但不使用 JDBC 调用时工作正常?
【发布时间】:2018-06-21 02:02:25
【问题描述】:

我有一个类似的查询:

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

正如我所说,它在 Workbench 中运行良好,但如果我在 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;

【问题讨论】:

  • 我的猜测是您的 JDBC 调用有问题。你能分享那个代码吗?
  • 不要调用你的 SQL 表group,这是一个保留关键字。由于这个和其他几个原因,您的原始 MySQL 代码甚至不会执行。
  • 请向我们展示您实际运行的确切代码。 Java 代码也不错,但您可以从原始 MySQL 代码开始。

标签: mysql jdbc workbench


【解决方案1】:

你确定你的 MySQL 查询的语法是正确的吗?它应该在查询中包含“FROM”语句。 “组”也是 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源代码可以帮助我们提供更好的解决方案。

【讨论】:

    猜你喜欢
    • 2023-03-22
    • 2014-03-30
    • 2012-12-02
    • 2017-11-08
    • 1970-01-01
    • 1970-01-01
    • 2011-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多