简体   繁体   English

将mysql查询结果加入数组

[英]Join mysql query results to an array

I am trying to generate a table list from data held in 2 tables. 我试图从2个表中保存的数据生成表列表。 One table is called PrimaryEvents and some sample data looks like: 一个表称为PrimaryEvents,一些示例数据如下所示:

|id|event   |validity|eventsrequired
|1 |event1  |7       |10
|1 |event2  |25      |1
|1 |event3  |12      |50

id here is just the user id of whoever created the data. id here只是创建数据的用户ID。

The second table is called portfolio, some sample data looks like: 第二个表称为组合,一些示例数据如下所示:

|id|name    |number|datacompleted
|21|event1  |3     |2014-07-07
|15|event1  |5     |2014-07-05
|21|event2  |5     |2014-05-08
|15|event1  |1     |2013-05-05
|15|event1  |1     |2014-05-05
|21|event1  |13    |2014-08-07
|21|event1  |6     |2014-07-08

id here is the id of the user that has completed the event. id here是已完成事件的用户的id。

I have a query that populates an array to allow a table to be shown to the user, the table shows a list of ALL the events from PrimaryEvents with a left join of data from portfolio if they have completed an event by the same name. 我有一个填充数组以允许向用户显示表的查询,该表显示了来自PrimaryEvents的所有事件的列表,如果他们已经使用相同的名称完成了一个事件,那么左边的数据来自投资组合。

I would like to change the functionality slightly to allow for the following: Only events that are within the validity period date range are merged, AND the merged data automatically SUMs the number (from portfolio.number). 我想略微更改功能以允许以下内容:仅合并有效期日期范围内的事件,并且合并的数据会自动对数字进行求和(来自portfolio.number)。

I am not sure how to progress this really. 我不确定如何进步。 Currently, I have managed to extract the data range using the following code: 目前,我已设法使用以下代码提取数据范围:

$currentDate = DATE("Y-m-d");

$eventList = query("SELECT event,validity FROM PrimaryEvents ORDER BY event");

foreach ($eventList as $row)
    {
    //
    $event["validity"] = $row["validity"];
    $validityDate = Date("Y-m-d", strtotime("-". $row["validity"] . "days"));
    $eventsDateValidCompleted = query("SELECT * FROM portfolio WHERE id = ? AND datecompleted BETWEEN ? AND ? ORDER BY name", $_SESSION["id"], $validityDate , $currentDate);
    }   

What I am missing is how to do something useful with the daterange sorted array data. 我缺少的是如何使用daterange排序的数组数据做一些有用的事情。

Questions: 1) How can I SUM the event data within the valid date range (IE returned in $eventsDateValidCompleted array)? 问题:1)如何在有效日期范围内汇总事件数据(在$ eventsDateValidCompleted数组中返回的IE)?

2) How can I LEFT JOIN this array of data to each line of my current query? 2)如何将此数组数据LEFT JOIN到我当前查询的每一行?

    $allEvents = query("SELECT * FROM PrimaryEvents LEFT JOIN portfolio ON (PrimaryEvents.event = portfolio.name) WHERE PrimaryEvents.role = ? AND (portfolio.id = ? Or portfolio.id is null) ORDER BY PrimaryEvents.event", $currentRole, $_SESSION["id"]);

3) Is there a better way to make this happen? 3)有没有更好的方法来实现这一目标?

Thanks for any help that can be offered on this. 感谢您提供的任何帮助。

You can sum values from a query in MySQL using the SUM() function along with a GROUP BY clause. 您可以使用SUM()函数和GROUP BY子句对MySQL中的查询中的值求和。 For example if you wanted to know the sum of all relevant portfolio.number values for a given user id and date range you could change this line: 例如,如果您想知道给定用户ID和日期范围的所有相关portfolio.number值的总和,您可以更改此行:

$eventsDateValidCompleted = query("SELECT * FROM portfolio WHERE id = ? AND datecompleted BETWEEN ? AND ? ORDER BY name", $_SESSION["id"], $validityDate , $currentDate);

to this: 对此:

$eventsDateValidCompleted = query("SELECT SUM(number) AS total_number FROM portfolio WHERE id = ? AND datecompleted BETWEEN ? AND ? GROUP BY id", $_SESSION["id"], $validityDate , $currentDate);

And if you wanted to get this sum value by event, and as part of the original query you could do something like this: 如果你想按事件得到这个和值,并且作为原始查询的一部分,你可以这样做:

SELECT e.event,e.validity, SUM(p.number) AS total_number
FROM PrimaryEvents e 
LEFT JOIN portfolio p ON p.name = e.event
GROUP BY e.id
ORDER BY e.event

I'm not sure I understand exactly what you want, but I suggest using SQL SUM() and GROUP BY along these lines: 我不确定我到底想要什么,但我建议在这些行中使用SQL SUM()GROUP BY

SELECT pe.id, pe.event, sum(p.number) FROM PrimaryEvents pe
LEFT JOIN portfolio p ON (pe.event = p.name) AND p.id = pe.role AND p.datecompleted BETWEEN ? AND ?
WHERE pe.role = ? 
GROUP BY pe.id, pe.event

But, as I said, your (reduced) data model and the queries are not quite consistent, which is why I cannot tell if the above will do anything for you. 但是,正如我所说,你的(减少的)数据模型和查询不是很一致,这就是为什么我不知道上面是否会为你做任何事情。

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

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