TapeGun007 Posted August 31, 2015 Share Posted August 31, 2015 I’ve used SQL for years, but only very simple one table queries. I have two tables Prospect and Notes: Prospects ProspectID ProspectName ProspectCode ProspectStatus … Notes NoteID NoteProspectID Note Here is what I want, but I don’t know how to write it in MySQL All prospects with the following ProspectCode = “123abc” ProspectStatus = “Contacted” I also want only the last Note from Notes where ProspectID = NoteProspectID. I hope this makes sense. Quote Link to comment https://forums.phpfreaks.com/topic/298001-two-table-query/ Share on other sites More sharing options...
Solution Psycho Posted August 31, 2015 Solution Share Posted August 31, 2015 Barand may come up with something more efficient. But, give this a try: SELECT p.ProspectID, p.ProspectName, p.ProspectCode, p.ProspectStatus, n.NoteID, n.Note FROM Prospects p LEFT JOIN Notes n ON p.ProspectID = n.NoteProspectID AND n.NoteID IN ( SELECT MAX(NoteID) FROM Notes GROUP BY NoteProspectID ) WHERE p.ProspectCode = “123abc” AND p.ProspectStatus = “Contacted” Quote Link to comment https://forums.phpfreaks.com/topic/298001-two-table-query/#findComment-1520035 Share on other sites More sharing options...
TapeGun007 Posted August 31, 2015 Author Share Posted August 31, 2015 (edited) EDIT: Yes, this does work. I had posted that it did not, but it was because I didn't realize most of my "contacted" leads in the db were marked as something else. Edited August 31, 2015 by TapeGun007 Quote Link to comment https://forums.phpfreaks.com/topic/298001-two-table-query/#findComment-1520036 Share on other sites More sharing options...
TapeGun007 Posted August 31, 2015 Author Share Posted August 31, 2015 This also helped me greatly because I had a misunderstanding about using the WHERE clause with a LEFT JOIN that confused me greatly. Quote Link to comment https://forums.phpfreaks.com/topic/298001-two-table-query/#findComment-1520038 Share on other sites More sharing options...
Barand Posted August 31, 2015 Share Posted August 31, 2015 Barand may come up with something more efficient. But, give this a try: Challenge declined One comment I would make is that it would be better to put a timestamp in the notes tables to identify the latest rather than relying on id sequence. (Treat ids purely as unique row identities used for internal relationships. A database should function the same if ids were allocated randomly instead of sequentially) Quote Link to comment https://forums.phpfreaks.com/topic/298001-two-table-query/#findComment-1520040 Share on other sites More sharing options...
TapeGun007 Posted September 1, 2015 Author Share Posted September 1, 2015 Good idea, and I do have a timestamp on each note already. Quote Link to comment https://forums.phpfreaks.com/topic/298001-two-table-query/#findComment-1520051 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.