Results 1 to 7 of 7

Thread: [RESOLVED] Fetch most recent record

Threaded View

  1. #1

    Thread Starter
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Resolved [RESOLVED] Fetch most recent record

    Hey all,

    I need a little help with a query on a MySQL server

    I have 5 tables that I am trying to join; line, tank, chemicals, chemistries and result.

    The line table has a one-many relation with the tank table (one line has many tanks)
    The tank table has a one-many relation with the chemistries table (one tank has many chemistries)
    The chemical table has a one-one relation with the chemistries table. (a chemistry references a chemical)
    The result table holds daily measurement results for each chemistry.

    I can use the following SQL to pull the lines, tanks and chemicals and chemistries together,
    Code:
    	SELECT L.Name AS Line,
    			 T.Name AS Tank, 
    			 CHEM.Name AS ChemicalName
    	
    	FROM line_table L
    	
    	JOIN tank_table T
    		ON T.LineID = L.id
    	
    	JOIN chemistries_table CMST
    		ON T.id = CMST.TankId
    	
    	JOIN chemicals_table CHEM
    		ON CMST.ChemicalId = CHEM.id
    What I am having trouble with is pulling the most recent result for each chemistry that is returned in the above SQL. I have tried adding the following, but simply adds information for the chemistry that is in the last record of the result table,
    Code:
    	LEFT JOIN 
    		(
    			SELECT *
    			FROM titration_results_table
    			WHERE id = (SELECT MAX(id) FROM titration_results_table)
    		) R
    		ON CMST.id=R.ChemistryID
    What I need is to get the last record for each chemistry added to the result table.

    Does anyone know how to do this?

    Thanks
    Kevin
    Last edited by kebo; Nov 30th, 2024 at 09:38 AM.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width