简体   繁体   中英

INNER JOIN not returning results

Good afternoon everyone! I've been working on this for two days and have no clue why this isn't working. I've tried several different ways to get the correct results and I either have too many returns or 0. I have two tables for tracking assets with the following layout:

Table: AssetManagment

id | serial | idcode
---|--------|-------
1  | 123    | 4
2  | 1234   | 4

Table: AssetManagement_History

id | rowID  | action    | occurred
---|--------|-----------|-------------
1  | 1      | shipping  | 2014-04-01
2  | 1      | stocked   | 2014-04-02
3  | 1      | installed | 2014-04-03
4  | 2      | stocked   | 2014-04-04

In this example, the AssetManagement_History.rowID relates to AssetManagment.id so you can see there are three history details for the first asset and only 1 for the second asset. I need to have returned all the assets where the last action corresponds to 'stocked' or 'restocked' - so only the second asset should be returned. Here's several mySQL statements I've tried:

SELECT tblASSET.id,tblASSET.serial
FROM AssetManagement tblASSET
INNER JOIN 
(SELECT occurred,max(id) id 
FROM AssetManagement_History GROUP BY occurred) tblAH ON tblASSET.id = tblAH.rowID
WHERE tblASSET.idcode='4' AND tblAH.action IN ('stocked','restocked')

another attempt:

SELECT tblASSET.id,tblASSET.serial
FROM AssetManagement tblASSET
INNER JOIN 
(SELECT max(rowID) rowID 
FROM AssetManagement_History GROUP BY occurred) tblAH ON tblAH.rowID = tblASSET.id
WHERE tblASSET.idcode='4'

I have several others, but they didn't work either. Any help would greatly be appreciated!

Thanks, Dave

SELECT
    A.id,
    A.serial
FROM AssetManagement A

INNER JOIN ( -- Join to get our latest history ID
  SELECT
      H.rowID,
      MAX(H.ID) ID
  FROM AssetManagement_History H
  GROUP BY H.rowID
) H1
ON H1.rowID = A.id

INNER JOIN AssetManagement_History H2 -- Join to get the row for the latest history
ON H2.ID = H1.ID

WHERE
  H2.action IN ('stocked', 'restocked')
  AND
  A.IDCode = 4
SELECT tblASSET.id,tblASSET.serial
FROM AssetManagement AS tblASSET
INNER JOIN AssetManagement_History AS tblAH
 ON tblAH.rowID = tblASSET.id
WHERE tblASSET.idcode='4'

What error messages are you getting? What do you want your result to look like?

Try using NOT EXISTS , (Explanation is in the comments)

SELECT tblASSET.id, tblASSET.serial
FROM AssetManagment AS tblASSET
    INNER JOIN AssetManagement_History AS tblAH
       ON tblAH.rowID = tblASSET.id
WHERE tblASSET.idcode = '4' 
    AND tblAH.action IN ('stocked','restocked')
    --/*And not exists occurred that is greater for the same rowID*/
    AND NOT EXISTS (SELECT 1 FROM AssetManagement_History AS his
                WHERE his.rowID = tblAH.rowID 
                    AND his.occurred > tblAH.occurred )

EDIT: Modified the error in the query and here is a working SQL FIDDLE .

This is more of a formatted comment than an answer

This:

INNER JOIN 
(SELECT occurred,max(id) id 
FROM AssetManagement_History GROUP BY occurred) tblAH ON tblASSET.id = tblAH.rowID

will throw an error because tblAH does not have a field called rowID. This:

INNER JOIN 
(SELECT max(rowID) rowID 
FROM AssetManagement_History GROUP BY occurred) tblAH ON tblAH.rowID = tblASSET.id

is probably giving strange results because of your goofy subquery. You are grouping by a field you are not selecting. Run that subquery by itself and see if it returns what you expect.

I think this is what you're looking for. You need to apply the where clause on the action inside the subquery.

SELECT tblASSET.id,tblASSET.serial
  FROM AssetManagement tblASSET
 INNER JOIN (SELECT occurred,max(id) id
               FROM AssetManagement_History
              where action in IN ('stocked','restocked') GROUP BY occurred
            ) tblAH ON tblASSET.id = tblAH.id
WHERE tblASSET.idcode='4' 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM