MySQL如何打印此查询的结果?

时间:2014-07-23 21:32:26

标签: php mysql subquery

我有这个问题:

$sql = $link->query("SELECT m.id_rel, (SELECT SUM(visita) as total FROM icar_mas_vistas WHERE id_rel = m.id_rel), icar_categorias.nombre
FROM icar_mas_vistas AS m, icar_categorias WHERE m.id_rel = icar_categorias.id_categoria");
    if($sql->num_rows){
        while($row = $sql->fetch_object()){
            echo '<a href="?go=14&action=1&do=ver&id='.$row->id_rel.'" onClick="parent.frames[0].location.href=\'menu.php?do=14\'" class="vincAl">'.$row->total.' '.$row->nombre.'</a>';
        }

它在SQL控制台中工作得很好,但是在使用PHP打印时,total的值没有显示,我该如何打印它?

由于

1 个答案:

答案 0 :(得分:1)

尝试在括号后添加 AS total 的更改查询:

SELECT m.id_rel, 
(SELECT SUM(visita) as total FROM icar_mas_vistas WHERE id_rel = m.id_rel) AS total, icar_categorias.nombre 
FROM icar_mas_vistas AS m, icar_categorias 
WHERE m.id_rel = icar_categorias.id_categoria