2

Is it possible, in one query to insert a row, into a MYSQL table and also values returned from MySQL function in other fields?

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.

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.

Please can you advise. I need to do this at the MySQL level, not via higher level tools. Thank you.

presentational update:

(question content remains the same)

  • I tried to enhance the formatting to be more readable on request from 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. Thanks!
therobyouknow
  • 6,604
  • 13
  • 56
  • 73

1 Answers1

2

Your query is invalid because you didn't use the parentheses correctly. When you use INSERT...VALUES(), the syntax must be:

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.

Also, you matched your MONTH() expression to your Year column and your YEAR() expression to your Month column.

But it's simpler than you're making it. You can put constant expressions into a SELECT, so the SELECT has four columns.

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.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • (1 of 2) +1 upvote and accepted answer, thank you so much Bill, so quick to respond as well. I corrected my original query so that `YEAR(CURRENT_YEAR())` should be `YEAR(CURRENT_DATE())`. – therobyouknow Jan 20 '17 at 16:09
  • (2 of 2) ...so that the whole query reads (forgive the limited formatting in the comment): `INSERT INTO time_monthly_hours ( ProjectID, monthTotalTime ) 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';` – therobyouknow Jan 20 '17 at 16:16
  • 1
    Very good. I have edited by answer to fix CURRENT_DATE() too. I didn't even notice that before. Glad to help. – Bill Karwin Jan 20 '17 at 16:21
  • 1
    I've enhanced the date range of the operation to MySQL functions to determine the beginning of the first day (at 00:00 hours) and current time (now): `INSERT INTO support_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 DATE_FORMAT(NOW() ,'%Y-%m-01 00:00:00') AND NOW();` - credit: http://stackoverflow.com/a/11808253/227926 – therobyouknow Jan 23 '17 at 10:57
  • Update ( 1 of 2 ) I've enhanced it further to use in a trigger: `CREATE TRIGGER worklog_update AFTER INSERT ON worklog FOR EACH ROW REPLACE INTO support_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 = (SELECT PROJECT FROM jiraissue WHERE NEW.issueid = jiraissue.ID ) AND worklog.startdate BETWEEN DATE_FORMAT(NOW() ,'%Y-%m-01 00:00:00') AND NOW();` – therobyouknow Jan 30 '17 at 01:24
  • ( 2 of 2 ) Credit for `REPLACE` - http://stackoverflow.com/questions/4205181/insert-into-a-mysql-table-or-update-if-exists hope this adds value to others looking here. Thanks. – therobyouknow Jan 30 '17 at 01:26