Note that there are some explanatory texts on larger screens.

plurals
  1. POdate Subquery PHP and Mysql
    primarykey
    data
    text
    <p>Following is my sql query with included subquery. Essentially I'm looking to obtain the most recent meter readings for our equipment for a given month and year. I know it's in my subquery where the issue is coming up but I don't know how to properly fix it.</p> <p>Following is result if I delete the subquery and just select equipment with meter readings for that month.</p> <pre><code>eid eqid name pid hours date 70 C1 KOMATSU WA250 3YD BUCKET 27 1176 2013-10-07 70 C1 KOMATSU WA250 3YD BUCKET 27 1195 2013-10-28 70 C1 KOMATSU WA250 3YD BUCKET 27 1178 2013-10-14 73 C11 KOMATSU PC200 EXCAVATOR 27 1080 2013-10-14 73 C11 KOMATSU PC200 EXCAVATOR 27 1099 2013-10-28 73 C11 KOMATSU PC200 EXCAVATOR 27 1078 2013-10-07 92 C4 CATERPILLAR 304D MINI EX 27 646 2013-10-14 92 C4 CATERPILLAR 304D MINI EX 27 645 2013-10-07 92 C4 CATERPILLAR 304D MINI EX 27 649 2013-10-28 58 E14 BOBCAT-ATV 2300 Utility 8 522 2013-10-31 61 E17 SKYTRAK FORKLIFT 40 943 2013-10-10 62 E18 SKYTRAK FORKLIFT 5 1790 2013-10-30 62 E18 SKYTRAK FORKLIFT 5 1789 2013-10-29 62 E18 SKYTRAK FORKLIFT 5 1777 2013-10-13 62 E18 SKYTRAK FORKLIFT 5 1772 2013-10-07 62 E18 SKYTRAK FORKLIFT 5 1777 2013-10-13 62 E18 SKYTRAK FORKLIFT 5 1772 2013-10-04 62 E18 SKYTRAK FORKLIFT 5 1772 2013-10-07 62 E18 SKYTRAK FORKLIFT 5 1772 2013-10-04 67 E23 BOBCAT SKID STEER 27 1178 2013-10-28 </code></pre> <p>Following is result of full query including subquery.</p> <pre><code>92 C4 CATERPILLAR 304D MINI EX 27 649 2013-10-28 61 E17 SKYTRAK FORKLIFT 40 943 2013-10-10 62 E18 SKYTRAK FORKLIFT 5 1790 2013-10-30 </code></pre> <p>Following is query I am using.</p> <pre><code>SELECT e.eid, e.eqid, e.name, m.pid, m.hours, m.date FROM meter m JOIN equipment e ON m.eid = e.eid WHERE MONTH(date) = $month AND YEAR(date) = $year AND m.date = (SELECT MAX(m2.date) FROM meter m2 WHERE m2.eid = m.eid) ORDER BY e.eqid ASC </code></pre> <p>Any help is greatly appreciated.</p> <p>EDIT*** I would have never gotten there Sebas. I had to change one thing and it worked perfectly.</p> <pre><code>SELECT DISTINCT e.eid, e.eqid, e.name, m.pid, m.hours, m.date FROM equipment e JOIN ( SELECT eid, MAX(date) as date FROM meter WHERE MONTH(date) = $month AND YEAR(date) = $year GROUP BY eid ) maxdate ON maxdate.eid = e.eid JOIN meter m ON m.eid = e.eid AND m.date = maxdate.date ORDER BY e.eqid ASC </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload