Thursday, March 22, 2018

php - How do I combine these two queries into a single INSERT INTO?



Can someone tell me how can I combine these two queries into a single INSERT INTO query?



$query1 = "INSERT INTO `monthly_income`(`Year`, `Month`) VALUES (2017, 'September')";
$query2 = "INSERT INTO `monthly_income`(`totalincome`) SELECT SUM(Income) FROM `eventincome` WHERE eventDate BETWEEN '2017-09-01' AND '2017-09-30'";


Here I want to insert all those values into a single row in the table "monthly_income". In Query2, i generate the total income between two dates from a seperate table called "eventincome". The "monthly_income" table has columns [Year, Month, totalincome]. And the "eventincome" table has columns [eventDate, eventTitle, Earnings, Expenses, Income]. So how can I join these two querys to use it in a





mysqli_query($conn, parameter)




:)
PS: I want to set those values from those queries in the same row, without creating two seperate rows becuse of those two INSERT INTO's. That's why I wanna join/combine/merge(whatever) those two queries :)


Answer



Give this a try




$query2 = "INSERT INTO `monthly_income`(`totalincome`,`Year`, `Month`) SELECT SUM(Income),2017 as Year, 'September' as Month FROM `eventincome` WHERE eventDate BETWEEN '2017-09-01' AND '2017-09-30'";

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...