用pdo连接两张桌子



我想连接数据库中的两个表。它以"经典"的方式工作得很好,但我的脚本中有很多选择查询,所以我想缩短它…

这是联合sql:

SELECT
matches.id, matches.start_time, matches.category_id, 
matches.highl, first_team.tid, first_team.t_name, 
second_team.tid, second_team.t_name AS ft_name 
FROM matches 
INNER JOIN teams AS first_team ON matches.first_team_id = first_team.tid 
INNER JOIN teams AS second_team ON matches.second_team_id = second_team.tid 
ORDER BY matches.id DESC 
LIMIT 10

我想要实现的是这样的东西,但我不知道如何添加上面复制的所有值。

public function getMatches($table,$conditions = array()){
$sql = 'SELECT ';
$sql .= array_key_exists("select",$conditions)?$conditions['select']:'';
$sql .= ' FROM '.$table;
if(array_key_exists("where",$conditions)){
$sql .= ' WHERE ';
$i = 0;
foreach($conditions['where'] as $key => $value){
$pre = ($i > 0)?' AND ':'';
$sql .= $pre.$key." = '".$value."'";
$i++;
}
}
if(array_key_exists("inner_join",$conditions)){
$sql .= ' INNER JOIN '.$conditions['inner_join'];
}
if(array_key_exists("on",$conditions)){
$sql .= ' ON '.$conditions['on'];
}
if(array_key_exists("as",$conditions)){
$sql .= ' AS '.$conditions['as'];
}
if(array_key_exists("order_by",$conditions)){
$sql .= ' ORDER BY '.$conditions['order_by'];
}
if(array_key_exists("start",$conditions) && array_key_exists("limit",$conditions)){
$sql .= ' LIMIT '.$conditions['start'].','.$conditions['limit'];
}elseif(!array_key_exists("start",$conditions) && array_key_exists("limit",$conditions)){
$sql .= ' LIMIT '.$conditions['limit'];
}
$query = $this->db->prepare($sql);
$query->execute();
if(array_key_exists("return_type",$conditions) && $conditions['return_type'] != 'all'){
switch($conditions['return_type']){
case 'count':
$data = $query->rowCount();
break;
case 'single':
$data = $query->fetch(PDO::FETCH_ASSOC);
break;
default:
$data = '';
}
}else{
if($query->rowCount() > 0){
$data = $query->fetchAll();
}
}
return !empty($data)?$data:false;
}
}

这是输出:

<div class="panel-heading">Matches</div>
<table class="table">
<tr>
<th>#</th>
<th>First Team</th>
<th>Second Team</th>
<th>Start Time</th>
</tr>
<?php
include 'inc/functions.php';
$db = new DB();
$matches = $db->getMatches('matches', array('inner_join'=>'teams'), array('as'=>'first_team'), array('on'=>'matches.first_team_id = first_team.tid'), array('inner_join'=>'teams'), array('as'=>'second_team'), array('on'=>'matches.second_team_id = second_team.tid'), array('order_by'=>'matches.id'));
if(!empty($matches)){ $count = 0; foreach($matches as $result){ $count++;?>
<tr>
<td><?php echo $count; ?></td>
<td><?php echo $result['ft_name']; ?></td>
<td><?php echo $result['t_name']; ?></td>
<td><?php echo $result['start_time']; ?></td>
</tr>
<?php } }else{ ?>
<tr><td colspan="4">No entry found!</td>
<?php } ?>
</table>
</div>

老实说,在我的一生中,我永远不会理解这样的PHP阵列墙是如何的

array('inner_join'=>'teams'), array('as'=>'first_team'), 
array('on'=>'matches.first_team_id = first_team.tid'), 
array('inner_join'=>'teams'), 
array('as'=>'second_team'), 
array('on'=>'matches.second_team_id = second_team.tid'), 
array('order_by'=>'matches.id'));

甚至可以被视为";较短的";比优雅紧凑的SQL

SELECT
matches.id, matches.start_time, matches.category_id, 
matches.highl, first_team.tid, first_team.t_name, 
second_team.tid, second_team.t_name AS ft_name 
FROM matches 
INNER JOIN teams AS first_team ON matches.first_team_id = first_team.tid 
INNER JOIN teams AS second_team ON matches.second_team_id = second_team.tid 
ORDER BY matches.id DESC 
LIMIT 10

更不用说意义和可读性了。

您是在说键入一些SQL关键字(如SELECT或FROM(可以节省开支吗?认真地把一个有意义和可理解的SQL搞得一团糟真的值得吗?

最新更新