结合join、group by、having和order by子句的mysql查询

gopyfrb3  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(366)
$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
kg7wmglp

kg7wmglp1#

使用,

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

相关问题