$date = $this->db->select('crv.date')
            ->from('commodities com')
            ->where('com.id = ' . $row->id)
            ->join('curves crv', 'com.curve_id = crv.profile_id', 'left')
            ->group_by('crv.date')
            ->having('COUNT(DISTINCT crv.job_id) = ' . $nr_commodities)
            ->order_by('crv.date DESC')
            ->limit(1)
            ->get()->row();


我需要在mysql中转换以上查询:

我尝试了以下方法。但出现一些语法错误。任何帮助,将不胜感激。

select crv.date
from commodities com
  where com.id = 100
left join curves crv on com.curve_id = crv.profile_id
group by crv.date
  having COUNT(DISTINCT crv.job_id) = 4
order by crv.date DESC
           limit 1

最佳答案

采用,

select crv.date
from commodities com
left join curves crv on com.curve_id = crv.profile_id
where com.id = 100
group by crv.date
  having COUNT(DISTINCT crv.job_id) = 4
order by crv.date DESC
           limit 1

10-08 12:02