willichan Posted April 4, 2016 Posted April 4, 2016 (edited) Hopefully someone a little more solid in SQL can give me a hand. I have a many-to-many-to-many setup. a.field1 a.field2 a.field3 x.afield1 x.bfield1 b.field1 b.field2 b.field3 y.bfield1 y.cfield1 c.field1 c.field2 c.field3 In essence, I need all a.field1 where c.field3="stringvalue". I could do it with multiple queries, and looping, but I am hoping there is a simpler, single query I can make. Thanks in advance for any help. Edited April 5, 2016 by willichan clarity My UDFs: Barcode Libraries, Automate creation of any type of project folder, File Locking with Cooperative Semaphores, Inline binary files, Continue script after reboot, WinWaitMulti, Name Aggregator, Enigma, CornedBeef Hash
jchd Posted April 5, 2016 Posted April 5, 2016 Are you looking for a join or rather reorganizing your schema to use foreign keys? Your question doesn't make any sense to me as it is. Are a, b, c, x, y tables? What are the relationships you want to modelize? This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)
willichan Posted April 5, 2016 Author Posted April 5, 2016 (edited) Sorry. I should have explained the tables better. A, B, and C are the tables containing the data. X and Y are the tables that contain the many-to-many links between the data tables. I need to identify any A that are linked by X to any B that are linked by Y to any C that hold a particular value. Yes, I believe this would need to be done with joins. SQL is unfortunately a weak area for me, especially where joins are concerned. I definitely need to study up on the subject. --- I just noticed the other point of confusion I created. I will fix the schema in the first post. Edited April 5, 2016 by willichan My UDFs: Barcode Libraries, Automate creation of any type of project folder, File Locking with Cooperative Semaphores, Inline binary files, Continue script after reboot, WinWaitMulti, Name Aggregator, Enigma, CornedBeef Hash
Bowmore Posted April 5, 2016 Posted April 5, 2016 You did not mention which database you are using, but the following should give you the required result on most databases that follow the latest ANSI SQL standard. SELECT a.field1, c.field3 FROM a JOIN x ON (a.field1 = x.afield1) JOIN b ON (x.bfield1 = b.field1) JOIN y ON (b.field1 = y.bfield1) JOIN c ON (y.cfield1 = c.field1) WHERE c.field3 = 'stringvalue' willichan 1 "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook
jchd Posted April 5, 2016 Posted April 5, 2016 As Bowmore showed this can be achieved thru a simple join, yet I'd recommend you normalize your schema by using foreign keys. The query would be essentially the same but linking fields would now be made by row IDs, almost always 64-bit integers. willichan 1 This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)
willichan Posted April 5, 2016 Author Posted April 5, 2016 Thank you both. @Bowmore, That will be far simpler than what I was trying to concoct. @jchd, I agree, unfortunately, I don't have control over the database. I am building a migration tool from some old, screwball system developed by an overseas affiliate company to a new system we are implementing. At some point, I should hit you up for recommendations to read up on SQL syntax. I seem to be getting more tasks that need it lately. Thanks again to both of you. My UDFs: Barcode Libraries, Automate creation of any type of project folder, File Locking with Cooperative Semaphores, Inline binary files, Continue script after reboot, WinWaitMulti, Name Aggregator, Enigma, CornedBeef Hash
jchd Posted April 6, 2016 Posted April 6, 2016 Ask anytime. Good luck in the meantime. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now