We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Looping through a database query

Medium Priority
186 Views
Last Modified: 2012-05-06
Hi Experts

I have two tables
keywordmodifiers
uk_england = cities and towns

I want to write a query that takes a keywords and adds then to the county of the uk_england table.

The result should end up being [keyword modifier county]
I want this to loop all the way through the uk_england table and add each keywordmodifier keyword till completion.
The table structures are below

How do you write a looping staement in mysql to do this?

Thanks
keywordmodifiers
 
SELECT
keywordmodifiers.keyword,
keywordmodifiers.pg_id
FROM
keywordmodifiers
 
uk_england
 
 
SELECT
uk_england.id_eng,
uk_england.county,
uk_england.town,
uk_england.postcode,
uk_england.Country
FROM
uk_england

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2004

Commented:
Your explanation is not clear to me.  Can you provide a specific example of what you would like to happen?

Author

Commented:
HI routinet

I have a table with keywords. For the example say 10 keywords.
I have another table with cities in it. For the example say it have 100 cities.

I want to take write a query that will add the 100 cities to each of the 10 keywords.

for example: keywords = porche
                     city           =  new york

I would like the result to join the two into a phrase "porche new york", "porche miami", "porche california" and so forth till all cities are added. Then it loops and starts with the next keyword.

I hope this is clearer

Thanks

Matt
CERTIFIED EXPERT
Top Expert 2004
Commented:
Try this example:

CREATE TABLE `table1` (
  `t1` INTEGER(11) NOT NULL AUTO_INCREMENT,
  `t2` VARCHAR(20) NOT NULL DEFAULT '',
  PRIMARY KEY (`t1`)
) ENGINE=MyISAM;
 
INSERT INTO `table1` (`t2`) VALUES 
  ('Miami'),
  ('New York'),
  ('Chicago'),
  ('Washington'),
  ('Kalamazoo'),
  ('Phoenix'),
  ('Augusta'),
  ('Dallas'),
  ('Boulder'),
  ('Boise'),
  ('Minneapolis'),
  ('Ozarks');
 
CREATE TABLE `table2` (
  `t3` INTEGER(11) NOT NULL AUTO_INCREMENT,
  `t4` VARCHAR(20) DEFAULT NULL,
  PRIMARY KEY (`t3`)
) ENGINE=MyISAM;
 
INSERT INTO `table2` (`t4`) VALUES 
  ('red'),
  ('orange'),
  ('yellow'),
  ('green'),
  ('blue'),
  ('purple'),
  ('brown'),
  ('black');
 
select * from table1 inner join table2;

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
I great I see how you do it. Thanks

Author

Commented:
Thanks for the help
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.