473,385 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Update Table from Another Table

Hi..

I have 2 tables: country and ticket
country table contains countryId and countries
ticket table contains many fields, and a country field

the country table is new and consists of all countries to be used in a drop down on the ui, that is joined with the ticket table to display the correct country based on the id.

that being said.. the current ticket table also contains a country field which will be eliminated in the future. what I want to do is update the ticket table by finding the closest match to ticket.country in the countries.country table and then update the ticket.countryId to the countries.countryId.

The only problem is, users have entered in varying names for countries. Example: USA, U.S.A, America instead of United States of America.

This is what i have been doing country by country:
Expand|Select|Wrap|Line Numbers
  1. update [Ticket].[Ticket]
  2. set [Ticket].[Ticket].[CountryId] = '1'
  3. where [Ticket].[Ticket].[Country] like 'USA'
I know there has to be an easier way to do this. Or is this pretty much the only way, and am I going to have to write a separte update for each country?
Feb 20 '09 #1
1 6397
Atli
5,058 Expert 4TB
Hi.

You can write a UPDATE query that updates all rows in the Ticket table so that the CountryID matches the CountryName in the Country table.

For example:
Expand|Select|Wrap|Line Numbers
  1. UPDATE `Ticket` AS t
  2. SET t.`CountryID` = (
  3.     SELECT c.`ID` FROM `Country` AS c
  4.     WHERE c.`Name` = t.`CountryName`
  5.     LIMIT 1
  6. );
However, this will not take into account names that have been entered incorrectly, or in an alternate way, like in your example.

To fix that, you could try simply editing the CountryName field in the Ticket table, replacing known alternatives with the real value before updating the ID.

For example:
Expand|Select|Wrap|Line Numbers
  1. UPDATE `Ticket` AS t
  2. SET t.`CountryName` = 'USA'
  3. WHERE t.`CountryName` IN(
  4.     'U.S.A', 'US', 'The United States',
  5.     'United States', 'America'
  6. );
And then run the previous update again.

And as always, before doing any of this, you should back up your data. Just in case.
Feb 20 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: shottarum | last post by:
I currently have 2 tables as follows: CREATE TABLE . ( mhan8 int, mhac02 varchar(5), mhmot varchar(5), mhupmj int )
12
by: jimserac | last post by:
I had previously posted this in an Access forum with negative results so will try here. Although this question specifies an Access database, I also wish to accomplish this with a large MS SQL...
16
by: robert | last post by:
been ruminating on the question (mostly in a 390/v7 context) of whether, and if so when, a row update becomes an insert/delete. i assume that there is a threshold on the number of columns of the...
18
by: Bill Smith | last post by:
The initial row is inserted with the colPartNum column containing a valid LIKE pattern, such as (without the single quotes) 'AB%DE'. I want to update the column value with the results of a query...
3
by: rrh | last post by:
I am trying to update a field in one table with data from another table. The problem I'm running into is I need to base the update on a range of data in the 2nd table. Table 1 has: date field...
8
by: Maxi | last post by:
There is a lotto system which picks 21 numbers every day out of 80 numbers. I have a table (name:Lotto) with 22 fields (name:Date,P1,P2....P21) Here is the structure and sample data: ...
6
by: FayeC | last post by:
I really need help figuring this out. i have a db with mostly text fields but 2. The user_id field is an autonumber (key) and the user_newsletter is a number (1 and 0) field meaning 1 yes the ...
2
by: Miro | last post by:
I will ask the question first then fumble thru trying to explain myself so i dont waste too much of your time. Question / Statement - Every mdb table needs a PrimaryKey ( or maybe an index - i...
13
by: shookim | last post by:
I don't care how one suggests I do it, but I've been searching for days on how to implement this concept. I'm trying to use some kind of grid control (doesn't have to be a grid control, whatever...
16
by: ARC | last post by:
Hello all, So I'm knee deep in this import utility program, and am coming up with all sorts of "gotcha's!". 1st off. On a "Find Duplicates Query", does anyone have a good solution for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.