我有以下project
条记录:
---------------------------------
|id|name |rate |start_date|
---------------------------------
|1 |project A |90.0 |01-01-2017|
|2 |project B |100.0 |01-01-2018|
|3 |project A |95.0 |01-01-2018|
---------------------------------
我想要所有记录,除非有多个同名记录,在这种情况下,我只想要具有最新start_date
的记录。在上面的示例中,我想获取记录2和3。
我该怎么做?到目前为止,我尝试使用distinct
,但没有成功。
答案 0 :(得分:0)
类似的事情可能会起作用:
<div class="grid">
<div class="box-wrapper">
<div class="box">
<div class="face front">front face</div>
<div class="face bottom">bottom-face</div>
</div>
</div>
<div class="box-wrapper">
<div class="box">
<div class="face front">front face</div>
<div class="face bottom">bottom-face</div>
</div>
</div>
<div class="box-wrapper">
<div class="box">
<div class="face front">front face</div>
<div class="face bottom">bottom-face</div>
</div>
</div>
<div class="box-wrapper">
<div class="box">
<div class="face front">front face</div>
<div class="face bottom">bottom-face</div>
</div>
</div>
<div class="box-wrapper">
<div class="box">
<div class="face front">front face</div>
<div class="face bottom">bottom-face</div>
</div>
</div>
<div class="box-wrapper">
<div class="box">
<div class="face front">front face</div>
<div class="face bottom">bottom-face</div>
</div>
</div>
<div class="box-wrapper">
<div class="box">
<div class="face front">front face</div>
<div class="face bottom">bottom-face</div>
</div>
</div>
<div class="box-wrapper">
<div class="box">
<div class="face front">front face</div>
<div class="face bottom">bottom-face</div>
</div>
</div>
</div>
但是,如果项目确实相同,则可能需要重新考虑设计。记录重复的原因是什么?
答案 1 :(得分:0)
查询:
SELECT project.*
FROM project
LEFT JOIN project as p
ON
p.name = project.name
AND
project.start_date < p.start_date
WHERE p.start_date IS NULL
该怎么做!(我去过那里)
等等,但是如何在轨道上做呢!!
Project.joins(%Q|
LEFT JOIN projects as p
ON
p.name = projects.name
AND
projects.start_date < p.start_date
|).where(p: { start_date: nil })