[英]How to work with two large sets of data from one mySQL database table in PHP?
[英]How to match two sets of data using MySQL and PHP
我有多個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
這是反映這一點的代碼。
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));
}
刀片模板:
<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
但我遇到了同樣的問題(它正在梳理該userID的所有記錄)
我的嘗試
$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);
您需要按userId和預訂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上看到它
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.