MySQL Limit 2如何选择第二行

时间:2018-04-17 03:56:05

标签: mysql sql

$sMonOperationHours = "SELECT * FROM ".COMPANY_OPERATION_HOURS_TBL." WHERE 
f_company_id = '$g_companyid' AND f_day = 'MON' LIMIT 2";
$rsMonOperationHours = mysql_query($sMonOperationHours);
$rowMonOperationHours = mysql_fetch_array($rsMonOperationHours);
$iMonStartTime = $rowMonOperationHours['f_start_time'];
$iMonStartTime2 = ???;
$iMonEndTime = $rowMonOperationHours['f_end_time'];
$iMonEndTime2 = ???;

将我的SQL查询结果限制为2后,如何为第二行选择f_start_time和f_end_time?

1 个答案:

答案 0 :(得分:0)

您可以在LIMIT和OFFSET中使用获取第二个结果

$sMonOperationHours = "SELECT * FROM ".COMPANY_OPERATION_HOURS_TBL." WHERE 
f_company_id = '$g_companyid' AND f_day = 'MON' LIMIT 1 OFFSET 1";
$rsMonOperationHours = mysql_query($sMonOperationHours);
$rowMonOperationHours = mysql_fetch_array($rsMonOperationHours);
$iMonStartTime = $rowMonOperationHours['f_start_time'];
$iMonEndTime = $rowMonOperationHours['f_end_time'];

另一种方法是,如果你想要两个结果,那就是:

$sMonOperationHours = "SELECT * FROM ".COMPANY_OPERATION_HOURS_TBL." WHERE 
f_company_id = '$g_companyid' AND f_day = 'MON' LIMIT 2";
$rsMonOperationHours = mysql_query($sMonOperationHours);
$rowsData = array();
$i = 0;
while ($row = mysql_fetch_array($rsMonOperationHours)) {
    $rowsData[$i] = array();
    $rowsData[$i][] =  $row['f_start_time'];
    $rowsData[$i][] =  $row['f_end_time'];
    $i++;
}

所有数据都将保存在rowsData中,其中包含数组中的结果。