简体   繁体   English

将行插入 MySQL 表的查询结果插入一个字段,SQL 函数将返回值插入其他字段

[英]Insert row into MySQL table result from query into one field and SQL function returned values into other fields

Is it possible, in one query to insert a row, into a MYSQL table and also values returned from MySQL function in other fields?是否有可能在一个查询中将一行插入到 MYSQL 表中以及从 MySQL 函数返回的其他字段中的值?

For example, I have the following table:例如,我有下表:

CREATE TABLE monthly_hours
(
  ProjectID int,
  Year int,
  Month int,
  monthTotalTime int
);
  • which shows the time worked on a project for a given month in a given year.它显示给定年份中给定月份的项目工作时间。

And I have a query that sums the total time spent on a project:我有一个查询,它汇总了一个项目所花费的总时间:

INSERT INTO time_monthly_hours ( `ProjectID`, `monthTotalTime` ) 
 SELECT jiraissue.PROJECT, SUM(worklog.timeworked) 
 FROM worklog, jiraissue 
 WHERE worklog.issueid = jiraissue.ID 
  AND jiraissue.PROJECT = 13262 
  AND worklog.startdate BETWEEN '2017-01-01 00:00:00' AND '2017-01-18 23:59:59';

And I want to insert the projectID and the monthTotalTime into a row in monthly_hours table, with the month and year.我想将 projectID 和 monthTotalTime 插入到monthly_hours 表中的一行中,包括月份和年份。

I tried:我试过:

INSERT INTO time_monthly_hours ( ProjectID, monthTotalTime,  Year, Month ) VALUES
( 
 SELECT jiraissue.PROJECT, SUM(worklog.timeworked) 
 FROM worklog, jiraissue 
 WHERE worklog.issueid = jiraissue.ID 
  AND jiraissue.PROJECT = 13262 
  AND worklog.startdate BETWEEN '2017-01-01 00:00:00' AND '2017-01-18 23:59:59' 
), 
  MONTH(CURRENT_DATE()), YEAR(CURRENT_YEAR() );

But this was an invalid query.但这是一个无效的查询。 I've searched quite a bit but can't find a solution like this to put both an SQL query result AND values from a SQL function into a row at the same time.我已经搜索了很多,但找不到这样的解决方案来同时将 SQL 查询结果和来自 SQL 函数的值放入一行。

Please can you advise.请你指教。 I need to do this at the MySQL level, not via higher level tools.我需要在 MySQL 级别执行此操作,而不是通过更高级别的工具。 Thank you.谢谢你。

presentational update:演示更新:

(question content remains the same) (问题内容不变)

  • I tried to enhance the formatting to be more readable on request from BeNice我尝试根据 BeNice 的要求增强格式以使其更具可读性
  • I corrected the grammar in the title slightly (needed to use plural for fields) also "queryto" in first line of question was corrected to query to.我稍微更正了标题中的语法(需要对字段使用复数),第一行问题中的“queryto”也被更正为 query to。 Thanks!谢谢!

Your query is invalid because you didn't use the parentheses correctly.您的查询无效,因为您没有正确使用括号。 When you use INSERT...VALUES() , the syntax must be:当您使用INSERT...VALUES() ,语法必须是:

INSERT INTO <table> (col, col, col, col) VALUES (val, val, val, val);

But you essentially had invalid syntax, something like:但是您基本上有无效的语法,例如:

INSERT INTO <table> (col, col, col, col) VALUES (val, val), val, val;

There's another problem with using one subquery that returns two columns.使用一个返回两列的子查询还有另一个问题。 That can't be used in place of a scalar value for an INSERT statement.不能用于代替 INSERT 语句的标量值。

Also, you matched your MONTH() expression to your Year column and your YEAR() expression to your Month column.此外,您将 MONTH() 表达式与 Year 列匹配,并将 YEAR() 表达式与 Month 列匹配。

But it's simpler than you're making it.但它比你制作它更简单。 You can put constant expressions into a SELECT, so the SELECT has four columns.您可以将常量表达式放入 SELECT,因此 SELECT 有四列。

INSERT INTO time_monthly_hours ( ProjectID, monthTotalTime,  Year, Month )     
 SELECT jiraissue.PROJECT, SUM(worklog.timeworked),
  YEAR(CURRENT_DATE()), MONTH(CURRENT_DATE())
 FROM worklog, jiraissue 
 WHERE worklog.issueid = jiraissue.ID 
  AND jiraissue.PROJECT = 13262 
  AND worklog.startdate BETWEEN '2017-01-01 00:00:00' AND '2017-01-18 23:59:59' 

When you use INSERT...SELECT you don't need the VALUES keyword.当您使用INSERT...SELECT您不需要VALUES关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM