Greg and John, Many thanks to you both.
John's SQL produced exactly the same results as my tortuous attempt, and went a long way toward showing how to write better SQL. Greg, to broaden my SQL experience even further, I will have to take a little time to understand yours and get it to produce the same results, which I will do. Thanks again, Mark -----Original Message----- From: Greg Morphis [mailto:[email protected]] Sent: Thursday, May 26, 2011 11:48 AM To: cf-talk Subject: Re: cfquery select question If they all return 1 row use this.. select * from table3 t3 where t3.ID = ( select t2.ID from table2 t2 where t2.productcode = ( select t1.productcode from table1 t1 where t1.ordernumber = [whatever] ) ) If they return multiple rows you can change the = to IN () And if they return many rows, may want to use WHERE EXISTS.. Let us know On Thu, May 26, 2011 at 11:41 AM, Mark Atkinson <[email protected]> wrote: > > Hallo all, > > Forgive my SQL "injection" into this list - perhaps an answer to my question > might also prove useful to others. > > I'm using three tables. There is no relationship between table1 and table3. > > I first query table1 for ordernumber and productcode, based on the > productcode containing a certain string. > > Outputting the results, I query table2 for ordernumber and t2_ID based on > table2.ordernumber matching table1.ordernumber. > > Looping those results, I query table3 for ID and Company based on > table2.t2_ID. > > This, rather painfully I do admit, finds the table3.Company(ies) that > has(have) purchased table1.productcode. > > Have Googled and read about how a single select with JOIN and/or UNION would > achieve these results, but they also mention how I should essentially "start > at the end" and work my way backwards, so to speak. Can't quite get my head > around this. > > TIA for any advice. > Mark > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| Order the Adobe Coldfusion Anthology now! http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:344931 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm

