如何将这两个查询合并为一个INSERT INTO?

时间:2021-09-16 15:15:59

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

有人能告诉我如何将这两个查询组合成一个INSERT INTO查询?

$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

在这里,我想将所有这些值插入表“monthly_income”中的单行。在Query2中,我从一个名为“eventincome”的单独表中生成两个日期之间的总收入。 “monthly_income”表包含[Year,Month,totalincome]列。并且“eventincome”表包含[eventDate,eventTitle,Earnings,Expenses,Income]列。那么我如何加入这两个查询来使用它

mysqli_query($conn, parameter)

mysqli_query($ conn,参数)

:) 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 :)

:) PS:我想在同一行中的那些查询中设置这些值,而不是因为这两个INSERT INTO而创建两个单独的行。这就是为什么我想加入/合并/合并(无论如何)这两个查询:)

2 个解决方案

#1


0  

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'";

#2


0  

Add the values from the first query as constants in the second query...

将第一个查询中的值作为常量添加到第二个查询中...

"INSERT INTO monthly_income(Year, Month,totalincome) SELECT 2017, 'September',SUM(Income) FROM eventincome WHERE eventDate BETWEEN '2017-09-01' AND '2017-09-3'";

“INSERT INTO monthly_income(Year,Month,totalincome)SELECT 2017,'September',SUM(Income)FROM eventincome WHERE eventDate BETWEEN'2017-09-01'and'2017-09-3'”;

#1


0  

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'";

#2


0  

Add the values from the first query as constants in the second query...

将第一个查询中的值作为常量添加到第二个查询中...

"INSERT INTO monthly_income(Year, Month,totalincome) SELECT 2017, 'September',SUM(Income) FROM eventincome WHERE eventDate BETWEEN '2017-09-01' AND '2017-09-3'";

“INSERT INTO monthly_income(Year,Month,totalincome)SELECT 2017,'September',SUM(Income)FROM eventincome WHERE eventDate BETWEEN'2017-09-01'and'2017-09-3'”;