$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?

最佳答案

您可以在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中,该数据包含数组中的结果。

关于mysql - MySQL Limit 2如何选择第二行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49869665/

10-16 13:46