MySQL Forums
Forum List  »  Connector/C++

Access Results Set Columns When Multiple Instances Of Same Table Aliased
Posted by: Eric Peterson
Date: June 20, 2019 10:48AM

DB noob here!

I am building a MySQL-driven data management system for inspection reports and associated data created on a production floor as subassemblies are built up. Any part number can be a parent assembly or a subassembly. When a part appears as a parent, there's one set of revision specs. When a part appears as a subassembly, there's a different set of revision specs. A unique part specification appears as a reference to manufacturer, part number, and revision level. For obvious reasons, I want to use a single table for all possible part numbers, rather than allowing the potential for the same part number to appear in either or both part or subassembly tables.

I have a great deal of the system working. I'm at a point where I'm listing out available inspection forms for the administrator or operator of an inspection station. This means summarizing the data from an inspection form and all the referenced tables into a single line of data in a list in the UI. I'm using C++ because C++ is the basis of the control code for final assembly equipment control software that integrates the subassemblies, and which has to access the same data base and merge the quality records from the subassembly stations into the master quality audit record for each final assembly. This means I can use the same libraries I develop for this terminal for the control systems -- a great labor savings.

In this code fragment, I build up the query that joins the inspection form record to the various dependent table data, execute it, and then load each results record's elements of interest into an element of a vector of structures ready to pass to my UI.

It's working. As you can see, the query ultimately references the table 00400_assemblynumbers twice -- once to retrieve the parent assembly part number, and once to retrieve the subassembly's part number. These are aliased.

I'm aware that I could use SQL to organize and concatenate the results into a single string, but I have to do some comparisons with the data that makes it more convenient to keep it organized into separate fields.

I found the rule that says "the alias' scope is limited to the sql squery".

I found a rule that says "If two or more columns of the result have the same field names, the last column will take precedence. To access the other column(s) of the same name, you must use the numeric index of the column or make an alias for the column. For aliased columns, you cannot access the contents with the original column name."

I tried the lines that are commented out. Clearly they violate both of the above.

I picked apart the results set to find the columns in which the two instances of the part number string appears. I hate hardwiring the column numbers in there, but, as I said, it works.

If I were to instead apply aliases to the relevant column names, how would I do that?

stmt = m_pconConnection->createStatement();

// let the data base do the work of tracking down the references
string strStatement = "SELECT * FROM 00510_inspectionforms ";
strStatement = strStatement + "INNER JOIN (00410_assemblyspecs ";
strStatement = strStatement + "INNER JOIN 00300_manufacturers ON 00410_assemblyspecs.mfrref = 00300_manufacturers.mfridx ";
strStatement = strStatement + "INNER JOIN 00400_assemblynumbers AS ASSYPARENT ON 00410_assemblyspecs.assyref = ASSYPARENT.assynumberidx ";
strStatement = strStatement + "INNER JOIN 00200_revs ON 00410_assemblyspecs.revref = 00200_revs.revidx) ";
strStatement = strStatement + "ON 00510_inspectionforms.parentassyref = 00410_assemblyspecs.assyspecidx ";
strStatement = strStatement + "INNER JOIN (00451_subassemblyspecs ";
strStatement = strStatement + "INNER JOIN 00400_assemblynumbers AS ASSYSUB ON 00451_subassemblyspecs.subassyref = ASSYSUB.assynumberidx ";
strStatement = strStatement + "INNER JOIN 00450_formrevisions ON 00451_subassemblyspecs.revref = 00450_formrevisions.revidx) ";
strStatement = strStatement + "ON 00510_inspectionforms.subassyspecref = 00451_subassemblyspecs.subassyspecidx ";
strStatement = strStatement + "INNER JOIN 00500_disposalinstrs ";
strStatement = strStatement + "ON 00510_inspectionforms.dispinstrref = 00500_disposalinstrs.dispinstridx ";

// make the query call
results = stmt->executeQuery(strStatement);

// traverse the results
while (results->next())
// a temp struct
AllFormData afdTemp;

// load it up
afdTemp.iFormIdx = results->getInt("formidx");
afdTemp.strMfr = results->getString("assymfr");
// afdTemp.strAssyNumber = results->getString("ASSYPARENT.assynumber");
afdTemp.strAssyNumber = results->getString(15);
afdTemp.strAssyRev = results->getString("revdesc");
// afdTemp.strSubAssyNumber = results->getString("ASSYSUB.assynumber");
afdTemp.strSubAssyNumber = results->getString(23);
afdTemp.strSubAssyRev = results->getString("revlevel");
afdTemp.bBulkInsp = (BOOL)results->getInt("bulkinspform");
afdTemp.bMilSpec = (BOOL)results->getInt("milspec");
afdTemp.strDisposalInstructions = results->getString("dispsummary");

Options: ReplyQuote

Written By
Access Results Set Columns When Multiple Instances Of Same Table Aliased
June 20, 2019 10:48AM

Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.