Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MySQL Update Only the First Matching Entry 1

Status
Not open for further replies.

3dColor

Programmer
Jan 10, 2006
240
US
The code below copies information from one table to another (I am changing my database structure).

The code works, however as you can imagine it updates all entries in the zipcode table for matching "Denver" and "CO" records and I only want to update the first Denver it comes to.

Is there some SQL statement I can add to only update the first matching record it finds?

Code:
<!--- Copy coord, county and set topMetro to 1 in zipcode tabel from CS table --->

<cfquery datasource="#Application.DSN#" name="GetAllCs">
		SELECT csName, county, coords, stateFk
		FROM cs
		WHERE csid > 52
</cfquery>

<cfif GetAllCs.RecordCount GTE 1 >

	<CFLOOP QUERY="GetAllCs">
	<cfset cityCapsWithoutDashes = REReplace(GetAllCs.csName,'-',' ','All')>
	
	<cfquery name="GetStates" datasource="#Application.DSN#">
		SELECT 	stateAbv
		FROM 	state 
		WHERE 	stateId = #GetAllCs.stateFk#
	</cfquery>

	<!--- update the listing table with the info from the table above --->
	<CFQUERY DATASOURCE="#Application.DSN#" >
		UPDATE	zipcodes
		SET 	topMetro=<cfqueryparam value='1' cfsqltype='CF_SQL_INTEGER'>,
				county=<cfqueryparam value='#trim(GetAllCs.county)#' cfsqltype='cf_sql_varchar'>,
				coords=<cfqueryparam value='#trim(GetAllCs.coords)#' cfsqltype='cf_sql_varchar'>
		WHERE 	zipcodes.city = <cfqueryparam value='#cityCapsWithoutDashes#' cfsqltype='cf_sql_varchar'>
				AND zipcodes.stateAbv = '#GetStates.stateAbv#'
	</cfquery>
	
	<cfoutput>#cityCapsWithoutDashes#, #GetStates.stateAbv# #GetAllCs.county# #GetAllCs.coords#<br></cfoutput>

	</CFLOOP>

</cfif>

<h1>Completed <cfoutput>#GetAllCs.recordcount#</cfoutput></h1>
 
Use a Limit in your update query:

UPDATE mytable SET field=somevalue WHERE otherfield=somethingelse [red]LIMIT 1[/red];



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Wow, that was easy, why didn't i think of that!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top