显示mysql查询的部分结果

时间:2012-08-07 23:49:55

标签: php mysql arrays

我有一个sql查询,它从3个不同的表中提取所有类型的数据,我现在想要在数组中显示。我可以让它输出sql查询中的所有数据,但我只想根据表中列的值是否相同来一次显示数组的一部分。将尝试在下面演示。

我的sql结果如下所示:

Client  |  Order  |  Exc1  |  Exc2  |  Rest  |  Reps  |
-------------------------------------------------------
Steve   |  1A     | this   |  That  |  This  |  that  |
-------------------------------------------------------
Mike    |  1A     | this   |  That  |  This  |  that  |
-------------------------------------------------------
Jax     |  1A     | this   |  That  |  This  |  that  |
-------------------------------------------------------
Steve   |  1B     | this   |  That  |  This  |  that  |
-------------------------------------------------------
Mike    |  1B     | this   |  That  |  This  |  that  |
-------------------------------------------------------
Jax     |  1B     | this   |  That  |  This  |  that  |

我希望我的数组在page1

上输出
Steve   |  1A     | this   |  That  |  This  |  that  |
-------------------------------------------------------
Mike    |  1A     | this   |  That  |  This  |  that  |
-------------------------------------------------------
Jax     |  1A     | this   |  That  |  This  |  that  |

这是第2页,依此类推。每个页面将包含所有客户端的列表,每页只有一个订单值。订单列值将因每个用户而异,因为他们可以输入订单字段的自己的文本。这是可能的,如果是这样,最好的方法怎么可能?

@Gordon Linoff这是我当前的查询。我真的不明白你写的是什么,或者更多的是它与我的查询有什么关系。低于我的当前查询。我没有完成输出到阵列。我正在测试,直到得到我想要的结果。

function get_workout_class(){
$workout_class = array();

$workout_query = mysql_query("
SELECT *
FROM `movements`
LEFT JOIN  `classes` 
ON `movements`.`class_id` = `classes`.`class_id`
LEFT JOIN `clients`
ON `movements`.`class_id` = `clients`.`class_id`
WHERE `classes`.`class_id` = '$class_id' AND `user_id` = ".$_session['user_id']."
ORDER BY `movements`.`order`, `clients`.`first_name`

");

}

以下是我的查询结果的屏幕截图,以便您可以看到我希望它们如何分组。要按不同“顺序”值拆分的页面。客户数量因班级而异。

http://custommovement.com/help/query.png

这是我的新查询。第一部分工作正常,但子查询给我的问题。它没有任何结果。 @GordonLinoff

function get_workout_class($class_id){
$class_id = (int)$class_id;

$workout_class = array();

$workout_query = mysql_query("
WITH `workouts` as (
SELECT  
 `movements`.`movement_id`,
 `movements`.`order`,  
 `movements`.`mv_00`,  
 `movements`.`mv_01`,  
 `movements`.`mv_02`,  
 `movements`.`mv_03`,  
 `movements`.`mv_04`,
 `movements`.`rep_set_sec`,
 `movements`.`rest`, 
 `classes`.`class_name`,
 `clients`.`client_id`,
 `clients`.`first_name`, 
 `clients`.`last_name`, 
 `clients`.`nickname` 
 FROM  `movements` 
 LEFT JOIN  `classes` ON  `movements`.`class_id` =  `classes`.`class_id` 
 LEFT JOIN  `clients` ON  `movements`.`class_id` =  `clients`.`class_id` 
 WHERE  `classes`.`class_id` = '$class_id'
)

SELECT `wo`.* 
(SELECT COUNT(DISTINCT `order`) FROM `workouts` `wo2` WHERE `wo2`.`order` <= `wo`.`order`) as `pagenum`
FROM `workouts` `wo`
ORDER BY `pagenum`

");

echo mysql_num_rows($workout_query); 
}

2 个答案:

答案 0 :(得分:1)

试试这个。它使用子查询作为订单的最大值。

SELECT  a.*
FROM    myTable a INNER JOIN
            (
                SELECT  Client, 
                        Max(`Order`) as MaxOrder
                FROM    myTable
                GROUP BY Client
            ) c
                ON a.Client = c.Client AND
                   a.`Order` = c.MaxOrder

或最简单的如果我没有弄错

SELECT *
FROM   myTable
WHERE  `Order` = '1B'

答案 1 :(得分:1)

我认为您可以通过在查询中添加页码来解决问题。不幸的是,这在mysql中有点痛苦,因为你必须使用自联接或相关子查询。

以下是如何使用相关子查询执行此操作的示例:

select t.*,
       (select count(distinct order) from t t2 where t2.order <= t.order) as pagenum
from t
order by pagenum

根据您的原始查询(但不将其放入字符串中):

with workouts as (
     SELECT *
     FROM `movements` LEFT JOIN
          `classes`
           ON `movements`.`class_id` = `classes`.`class_id` LEFT JOIN
           `clients`
          ON `movements`.`class_id` = `clients`.`class_id`
      WHERE `classes`.`class_id` = '$class_id' AND `user_id` = ".$_session['user_id']." 
    )
select wo.*,
       (select count(distinct order) from workouts wo2 where wo2.order <= wo.order) as pagenum
from workouts wo
order by pagenum

这几乎可行。 。 。只是一个警告。您可以将“SELECT *”放入with子句中,因为您有多个具有相同名称的列。放入你需要的列。

我忘记了mysql不支持“with”语句。我为错误的语法道歉。一种解决方法是使用视图或临时表。否则,查询会有点复杂,因为逻辑必须重复两次:

select wo.*,
       (select count(distinct order)
        from (SELECT *
              FROM `movements` LEFT JOIN
                   `classes`
                   ON `movements`.`class_id` = `classes`.`class_id` LEFT JOIN
                   `clients`
                   ON `movements`.`class_id` = `clients`.`class_id`
              WHERE `classes`.`class_id` = '$class_id' AND `user_id` = ".$_session['user_id']." 
             ) wo2
        where wo2.order <= wo.order
       ) as pagenum
from (SELECT *
      FROM `movements` LEFT JOIN
           `classes`
            ON `movements`.`class_id` = `classes`.`class_id` LEFT JOIN
            `clients`
           ON `movements`.`class_id` = `clients`.`class_id`
       WHERE `classes`.`class_id` = '$class_id' AND `user_id` = ".$_session['user_id']." 
      ) wo
order by pagenum