php - 如何使用MySQL和PHP匹配两组数据

标签 php mysql

我有多个 MySQL 表,其中有许多行需要使用 PHP 在表中返回。我遇到的问题是当其中一个表具有多个匹配 ID 时如何正确显示信息。

以此为例。这是一个表,其中包含用户 (userID) 已预订的时间表。

bk_schedule

id  userID  date    block   tos status
113 46  2013-12-31  3        yes    1
114 44  2013-12-26  1        yes    3
115 45  2013-12-31  1        yes    3
116 44  2013-12-31  2        yes    3
117 44  2013-12-31  1        yes    3

在保存此数据的同时,它还将数据保存到另一个表中,其中用户选择作为他们的“服务”的内容分隔到新行中 foreach 他们选择的服务。

bk_service

id  userID  bk_id   services
212 46       113    7
213 44       114    62
214 45       115    61
215 44       116    14
216 44       117    1
217 44       117    8
218 44       117    22
219 44       117    15

bk_id 与bk_schedule id 相关联以形成它们的关系。

现在,当我必须使用 Laravel 4 将这些信息提取到一个表中时,如果我使用不同的表变量,我会将所有结果组合到每一行中。如果我尝试使用使用 JOIN's 的同一个表集,我得到的行很好,但它们循环遍历每个服务而不是合并(我猜是因为它循环找到的每一行都将其计为新行)。

有点像。

userID  bk_id   services
44       116    14
44       114    62
44       117    8
44       117    22
44       117    15

这是反射(reflect)这一点的代码。

    public function showHistory($id) {

   $appointment = DB::table('bk_schedule')
    ->select('bk_schedule.id', 'bk_schedule.date', 'bk_timeslot.block', 'bk_status.status', 'pr_service.service')
    ->where('bk_schedule.userID', $id)      
    ->join('bk_status', 'bk_schedule.status', '=', 'bk_status.id')
    ->join('bk_timeslot', 'bk_schedule.block', '=', 'bk_timeslot.id')
    ->join('bk_service', 'bk_schedule.id', '=','bk_service.bk_id')
    ->join('pr_service', 'pr_service.id', '=', 'bk_service.services')
    ->orderBy('date', 'ASC')            
    ->get();

  // var_dump($appointment); die;

    $today = date('Y-m-d');

    foreach($appointment as $appointments) {

        $date = strtotime($appointments->date);     

        $appointments->date = date('l: F d, Y',$date);              
    }


    $service = DB::table('bk_service')
        ->select('pr_service.service', 'pr_service.price')
        ->join('pr_service', 'pr_service.id', '=', 'bk_service.services')
        ->where('bk_service.userID', $id)
        ->where('bk_service.bk_id', $appointments->id)
        ->get();

   return View::make('appointments.history', array('pageTitle' => 'Apppointment History',
                                'today' => $today, 'service' => $service,
                                'appointment' => $appointment)); 
}

Blade 模板:

        <table class="main-table">
            <thead class="main-table-head">
                <th>Status/Result</th>
                <th>Date</th>
                <th>Block</th>
                <th>Services</th>
                <th>Action</th>
            </thead>
            <tbody class="main-table-head">
            @foreach($appointment as $appointments)
                <tr>
                    <td>{{{ $appointments->status }}}</td>
                    <td>{{{ $appointments->date }}}</td>
                    <td>{{{ $appointments->block }}}</td>
                    <td>
                        @foreach($service as $services)
                        {{{ $services->service }}}
                        @endforeach
                    </td>
                </tr>
            @endforeach
            </tbody>
        </table>

这基本上就是我想要的样子。 (如果有帮助的话,这是一个约会历史页面)

userID  bk_id   services
44       117    1, 8, 22, 15
44       116    14
44       114    62

我试图尽可能详细地尝试让它工作是一件痛苦的事情。我尝试了 GROUP_CONCAT 但我遇到了同样的问题(它正在组合该用户 ID 的所有记录)

我的尝试

        $schedule = DB::table('bk_schedule')
            ->select( DB::raw('users_information.street_2, users_information.phone_2, users_information.apartment, bk_schedule.note, bk_schedule.date, bk_schedule.office, bk_status.status, bk_schedule.id, bk_schedule.userID, bk_timeslot.block, users_information.last_name, users_information.street_1, users_information.phone_1, users_information.user_zip_code, group_concat(pr_service.short_name SEPARATOR " | ") as group_service, group_concat(pr_service.service SEPARATOR ", ") as service_detail'))
            ->join('users_information', 'bk_schedule.userID', '=', 'users_information.id')
            ->join('bk_timeslot', 'bk_schedule.block', '=', 'bk_timeslot.id')
            ->join('bk_service', 'bk_schedule.userID', '=', 'bk_service.userID')
            ->join('pr_service', 'bk_service.services', '=', 'pr_service.id')
            ->join('bk_status', 'bk_schedule.status', '=', 'bk_status.id')
            ->orderBy('bk_schedule.date', 'asc')
            ->groupBy('bk_schedule.id')
            ->paginate(15);

如果有人对我的最终解决方案感到好奇。

        $schedule = DB::table('bk_schedule')
            ->select( DB::raw('bk_schedule.office, pr_service.short_name, bk_timeslot.block, bk_schedule.date, bk_status.status, users_information.last_name, users_information.street_1, users_information.phone_1, users_information.user_zip_code, users_information.street_2, users_information.phone_2, users_information.apartment, bk_schedule.userID, bk_service.id, group_concat(pr_service.service)as service_detail, group_concat(pr_service.short_name)as group_service '))
            ->join('bk_service', 'bk_schedule.id', '=', 'bk_service.bk_id')
            ->join('users_information', 'bk_schedule.userID', '=', 'users_information.id')
            ->join('bk_status', 'bk_schedule.status', '=', 'bk_status.id')
            ->join('bk_timeslot', 'bk_schedule.block', '=', 'bk_timeslot.id')
            ->join('pr_service', 'bk_service.services', '=', 'pr_service.id')
            ->groupBy('bk_service.userID', 'bk_service.bk_id')
            ->paginate(15);

最佳答案

您需要同时按用户 ID 和预订 ID 进行分组。

select sc.userId, sc.id, group_concat(services)
  from bk_schedule sc
  join bk_service se on (sc.id = se.bk_id)
 group by sc.userId, sc.id;

查看 sqlfiddle

关于php - 如何使用MySQL和PHP匹配两组数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20830114/

相关文章:

php - 根据值排序和显示数组值?

php - 在 MySQL 和 HTML 中使用相同的列名

mysql - 为什么 MySQL 插入比 JDBC 慢?

mysql - 使用 select 和 case 'when/then' 进行更新

java - 使用 java 和 mySQL 的泰米尔语 utf-8 编码

php - 如何在php中获取switch语句的用户输入?

php - Recaptcha V2.0 始终返回 bool(false)

php - Linux命令从所有目录和子目录中获取所有文件、文件大小和路径

php - 如何从自动增量字段获取字段值

php - Google Cloud SQL 数据类型