简体   繁体   English

合并表中的行

[英]Merge rows in table

My table consists of availability status data for each day of the month where a row represents a given month and each column is a day. 我的表由该月每一天的可用性状态数据组成,其中一行代表给定的月份,每一列代表一天。 I stores statuses like 'Available', 'Unavailable', etc. 我存储“可用”,“不可用”等状态。

This works great until I want to display data by week. 在我想按星期显示数据之前,此方法非常有效。 The issue I run into is when I get to a week that crosses into the next month. 我遇到的问题是到下个月进入一个星期。 Right now I'm using a query that looks like: 现在,我正在使用如下查询:

$query = "SELECT pilots.id AS pid, "
       . "pilots.last_name AS last_name, "
       . "pilots.first_name AS first_name, "
       . "rt.* FROM pilots "
       . "LEFT JOIN ("
       . "SELECT * FROM availability "
       . "WHERE date ='"  
       . $first_date_of_month->format("Y-m-d") 
       . $next_month_str . "')rt "
       . "ON pilots.id = rt.pilot_id "
       . "ORDER BY pilots.last_name, pilots.first_name, pid";

Which looks like: 看起来像:

SELECT p.id pid
     , p.last_name
     , p.first_name
     , rt.* 
  FROM pilots p
  LEFT 
  JOIN 
     ( SELECT * 
         FROM availability 
        WHERE date ='2017-06-01' 
         OR date = '2017-07-01'
     )rt 
    ON p.id = rt.pilot_id 
 ORDER 
    BY p.last_name
     , p.first_name
     , pid

The problem with this is let's say I have 1 user and 2 months of data. 问题是,我有1个用户和2个月的数据。 It returns 2 rows. 它返回2行。 I really only want 1 row where the data is merged using columns from the end of one month and beginning of another month. 我真的只想要1行,其中从一个月末到另一个月初使用列合并数据。

For example: 例如:

//Pilots
-------------------------
| id|first_name|last_name|
-------------------------
| 1 | "myTest" |"myTest" |
-------------------------

// Months
----------------------------------------------------------------------
| id|pilot_id|  date  |   1_status  |   2_status  |....|  30_status  |
|--------------------------------------------------------------------|
| 1 |   1    |3/1/2017| "Available" |"Unavailable"|....| "Available" |
|--------------------------------------------------------------------|
| 2 |   1    |4/1/2017|"Unavailable"|"Unavailable"|....|"Unavailable"|
----------------------------------------------------------------------

In the first example (not within 7 days of the end of the month), it's simple: I will have 1 record because I know I do not need the next month. 在第一个示例中(不是在月底的7天内),这很简单:我将有1条记录,因为我知道下个月不需要。 Let's say it's "3/21/2017"; 假设是“ 2017年3月21日”; I just output the statuses for "3/21/2017", "3/22/2017", and so on. 我只输出“ 3/21/2017”,“ 3/22/2017”等的状态。

However, let's say the week I want to display starts at "3/28/2017". 但是,假设我要显示的一周从“ 2017/3/28”开始。 I need the data from March and for April. 我需要3月和4月的数据。 In this case, it generates 2 rows of data for pilot_id 1. But I really only need 28_status, 29_status, 30_status, 31_status, and THEN need 1_status, 2_status (all from April). 在这种情况下,它将为pilot_id 1生成两行数据。但是我确实只需要28_status,29_status,30_status,31_status,然后需要1_status,2_status(均从4月开始)。

Attached $next_month_str below: 随附以下$ next_month_str:

            $next_month_str = "";
            if (!is_last_day_of_month($sunday_date))
            {
                $next_month = get_next_month($monday_date);
                $next_month_str = "' OR date = '";
                $next_month_str = $next_month_str . $next_month->format("Y-m-d");
                $next_month_str = $next_month_str;
            }

$query_inner = "SELECT pilot_id, 26_status, 27_status, 28_status, 29_status, 30_status FROM availability WHERE date = '2017-06-01'";
$query = "SELECT availability.pilot_id AS pid, rt.26_status, rt.27_status, rt.28_status, rt.29_status, rt.30_status, availability.1_status, availability.2_status FROM availability INNER JOIN (" . $query_inner . ")rt ON rt.pilot_id = pid WHERE date = '2017-07-01'";

There are two queries I came up with. 我提出了两个查询。 Separate they work fine, and they work fine when I test them out using a SQL checker and they do exactly what I want. 分开它们可以正常工作,当我使用SQL检查器对其进行测试时,它们也可以正常工作,并且它们可以完全满足我的要求。 However, when I run it in my site, they give me a error querying the database. 但是,当我在自己的站点中运行它时,它们给我查询数据库的错误。

Based on your question, you need to use GROUP BY , HAVING and possibly GROUP_CONCAT() to essentially "flatten" your results set. 根据您的问题,您需要使用GROUP BYHAVING以及可能的GROUP_CONCAT()本质上“展平”您的结果集。 Here are some resources so you may learn how and then apply that knowledge. 这里有一些资源,您可以学习如何使用这些知识。

The above resources have a sandbox for you try play around with the SQL statements. 上面的资源有一个沙箱供您尝试使用SQL语句。

Solved it. 解决了。

I can build the query string programatically, but using JOINs I was able to solve it. 我可以以编程方式构建查询字符串,但是使用JOIN可以解决它。 Does exactly what I want it to. 正是我想要的。 Here is an example of a string: 这是一个字符串示例:

SELECT availability.pilot_id, rt.26_status, rt.27_status, rt.28_status, rt.29_status, rt.30_status, availability.1_status, availability.2_status FROM availability INNER JOIN (SELECT pilot_id, 26_status, 27_status, 28_status, 29_status, 30_status FROM availability WHERE date = '2017-06-01')rt ON rt.pilot_id = availability.pilot_id WHERE date = '2017-07-01'

EDIT: Although I was able to solve the question using this, ultimately it was a design issue that forced this. 编辑:尽管我可以使用此方法解决问题,但最终是一个迫使该问题的设计问题。 I decided to refactor and use a table like availability {id, pilot_id, date, status}. 我决定重构并使用诸如可用性{id,pilot_id,日期,状态}之类的表。

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

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