非常感谢你花时间看看我的问题。我对mysql驱动的网络应用程序很陌生,我在查找查询时遇到了一些麻烦。
我有3张桌子:
事件:
event_id(INT) | event_fee(INT) | start_time(DATE_TIME) | stop_time(DATE_TIME)
表演者:
performer_id(INT) | performer_name(VARCHAR) | performer_fee(INT)
event_performers:
event_performers_id(INT) | event_id(INT) | performer_id(INT)
检索我使用的事件数据。
SELECT *
FROM events
JOIN event_performers
ON events.event_id = event_performers.event_id
我想运行一个测试查询,以确定是否有任何表演者同时对不同的事件采取行动。
我正在尝试检索所有表演者ID,所有事件ID并循环通过它们中的每一个来比较日期,但我得到堆叠做foreach循环并且不能真正让它工作
include('app/config.php');
function return_all_performers(){
include(PATH . '/core/model/db_connect.php');
try {
$query_object = $db->query("
SELECT performer_id
FROM performers
");
} catch (Exception $e){
include_once(PATH . "/core/view/error.php");
exit();
}
return $query_object->fetchAll(PDO::FETCH_COLUMN, 0);
}
function return_all_performer_events($performer_id){
include(PATH . '/core/model/db_connect.php');
try {
$query_object = $db->prepare("
SELECT *
FROM events
JOIN event_performers
ON events.event_id = event_performers.event_id
WHERE event_performers.performer_id = :performer_id
");
$query_object->bindParam(':performer_id', $performer_id);
$query_object->execute();
} catch (Exception $e){
include_once(PATH . "/core/view/error.php");
exit();
}
return $query_object->fetchAll(PDO::FETCH_ASSOC);
}
$performers_ids = return_all_performers();
$overlaps = array();
foreach ($performers_ids as $performer_id) {
$performer_events = return_all_performer_events($performer_id);
foreach ($performer_events as $event => $value) {
// Isolate Single event and compare it to others
// TO DO
// Check If times overlaps
/*
foreach () {
if(
strtotime($event['start_time']) >= strtotime($isolated_event['start_time']) &&
strtotime($event['start_time']) <= strtotime($isolated_event['stop_time'])
){
return true;
}
if(
strtotime($event['stop_time']) >= strtotime($isolated_event['start_time']) &&
strtotime($event['stop_time']) <= strtotime($isolated_event['stop_time'])
){
$overlaps[] = $event;
}
}
*/
}
}
var_dump($overlaps);
非常感谢你的帮助, 干杯
答案 0 :(得分:0)
MySQL有一个between子句,将您的查询呈现为:
SELECT *
FROM events
JOIN event_performers
ON events.event_id = event_performers.event_id
WHERE start_time BETWEEN ? OR ? AND stop_time BETWEEN ? and ?
希望有帮助...
答案 1 :(得分:0)
您可以使用类似
的内容SELECT
COUNT(*) AS amount_of_overlaps
FROM
events_table AS e1
JOIN (SELECT * FROM events_table) AS e2 ON (e2.id > e1.id)
WHERE
(e2.start_date BETWEEN e1.start_date AND e1.end_date)
OR
(e2.end_date BETWEEN e1.start_date AND e1.end_date)
获取重叠事件的数量。 你只需要为特定的人添加你的限制。
答案 2 :(得分:0)
SELECT ep.performer_name, ep.performer_id, ep.performer_fee, e.event_id, e.event_fee,
e.start_time, e.stop_time, group_concat(e2.event_id SEPARATOR ',')
FROM events as e
JOIN event_performers ep ON e.event_id = ep.event_id
join events e2 on e2.event_id != e.event_id and
greatest(e2.start_time, e.start_time) < least(e2.stop_time,e.stop_time)
join event_performers ep2 on ep2.event_id=e2.event_id and ep2.performer_id=ep.performer_id
group by ep.performer_name, ep.performer_id, ep.performer_fee, e.event_id, e.event_fee,
e.start_time, e.stop_time
这将返回每个有冲突的事件,并列出与之冲突的每个事件。它可以产生有趣的数据,因为如果A,B和C都冲突,你将获得3行。 1说A与B和C冲突,另一种说B与A和C冲突,有人说C与A和B冲突。也许你可以&#34;清理它&#34;但是你喜欢PHP,但这个数据可能是一个好的开始。