如何解决用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)){
$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('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
更不用说意义和可读性了。
您是要节省自己输入一些SELECT或FROM这样的SQL关键字吗?认真吗用有意义且易于理解的SQL弄得一团糟真的值得吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。