【问题标题】:MySQL Limit not setting to a specific rangeMySQL限制未设置为特定范围
【发布时间】:2013-08-19 15:32:18
【问题描述】:

我有一个查询如下。

$tests = DB::select('devices.id')
 ->select(array('devices.description', 'dev_desc'))
 ->select(array('device_types.description', 'devtype_init'))
 ->select(array('actions.description', 'test_desc'))
 ->select(array('history.dt_logged', 'last_test'))
 ->select(array(DB::expr('history.dt_logged + (devices.frequency * 7 * 24 * 60 * 60)'), 'next_test'))
 ->from('history')
 ->join('actions')->on('history.action_id', '=', 'actions.id')
 ->join('devices')->on('history.device_id', '=', 'devices.id')
 ->join('device_types')->on('devices.device_type_id', '=', 'cal_device_types.id')
 ->and_where(DB::expr('history.dt_logged + (devices.frequency * 7 * 24 * 60 * 60)'), '>=', $start)
 ->and_where(DB::expr('history.dt_logged + (devices.frequency * 7 * 24 * 60 * 60)'), '<=', $end)
 ->where('device_types.description', '=', 'Calibration')
 ->order_by('history.dt_logged', 'desc')->limit(1)
 ->execute();

我想拉回一条记录,因此是“limit(1)”,但它拉回了多条记录。代码对我来说看起来很完美。任何帮助将不胜感激。

干杯。

相关子查询会起作用吗?!

【问题讨论】:

    标签: php mysql kohana


    【解决方案1】:

    我们需要查看不同表之间的关系才能得到确切的原因,但我的猜测是这些表之间存在一对多关系,并且需要GROUP BY 来总结它们。

    我建议使用 WHERE 子句运行您的查询,该子句将主行限制为一个结果(例如 devices.id = x)。然后,您将看到针对该 ID 返回的多行,使用 GROUP BY 之类的聚合函数将这些结果合并到一行中。

    【讨论】:

      猜你喜欢
      • 2011-08-11
      • 1970-01-01
      • 2023-01-13
      • 2021-12-19
      • 1970-01-01
      • 2020-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多