Skip to main content
Participating Frequently
January 17, 2011
Question

Looping the same answer from the 1st row in the table

  • January 17, 2011
  • 1 reply
  • 692 views

I have a database with 3 tables: (1) employeeTable (2) cityTable (3)stateTable.

I reference the employee's city name and state code by each id number from the cityTable and stateTable.

My Employee table has about 10 records, 3 has an idcity and idstate input, the rest are blank.

(1st row) id 9 for Henderson, id 28 for NV (6th row) id 9 for Henderson, id 28 for NV, and (10th row) id 10 for Las Vegas, id 28 for NV

I run a query:

<cfquery datasource="dsnName" name="qEmployees">SELECT * FROM users ORDER BY lastName ASC</cfquery>
<cfquery datasource="dsnName" name="qState">SELECT code FROM states WHERE idstate=#qEmployees.idstate#</cfquery>
<cfquery datasource="dsnName" name="qCity">SELECT name FROM cities WHERE idcities=#qEmployees.idcity#</cfquery>

a run the table output:

<table>
    <cfoutput query="qEmployees">
    <tr>
        <td>#qEmployees.lastName#, #qEmployees.firstName#</td>
        <td>#qCity.name#</td>
        <td width="40">#qState.code#</td>
        <td width="60">#qEmployees.zipCode#</td>
    </tr>

     </cfoutput>

</table>

PROBLEM:

All 10 records output in the table as the same answer from the 1st row (Henderson, NV).

What's making this loop having the same answer from the 1st row and affects all the rows with blank info or different info?

I've been trying to figure this out for weeks and I hope the community can help me out. Thanks!

This topic has been closed for replies.

1 reply

Inspiring
January 17, 2011

Learn to join tables.  If you don't understand that statement, I've heard good things about the book, Teach Yourself SQL in 10 Minutes by Ben Forta.

Regarding what is happening to you now, when you have a query that returns more than one record (qEmployees in your case), and you ask for a value from that query, as you do in the next two queries, you get the value from the first record.

Participating Frequently
January 17, 2011
Regarding what is happening to you now, when you have a query that returns more than one record (qEmployees in your case), and you ask for a value from that query, as you do in the next two queries, you get the value from the first record.

Yes! So how do I stop the following queries from copying the previous query? I'm using MySQL Workbench.

Owainnorth
Inspiring
January 17, 2011

As Dan rightly says, take a look into table joins; there's no point just giving you the answer as you'll just get stuck next time and these forums are to help people rather than to do their jobs for them.

What you're looking to do is a relatively simple join, so you shouldn't have to read up for long before you've mastered them. Plus your site will be significantly more efficient