Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

nesting select statements in MYSQL stored procedure

Posted on 2009-07-01
2
Medium Priority
?
563 Views
Last Modified: 2012-06-27
I am trying to create a stored procedure that uses the output of one select statement as the input for another. I need to nest these 3 or 4 levels deep, but I cant find anything about this in the manuals.

For example, I want to select a country, then find all the regions within that country then the cities within each region.

I have some specific code to execute at each level to create index pages, so I want to be able to do this as nested loops of some type (foreach, 'while not eof'?) if possible.

The reason that I do not want to use joins here is that i will be creating output to different files at each level.
I will be creating a 'country page' that will link to each city region page and a region page for each country that will link to all the cities within the region.

I need to grab each of the values to create the various strings for each of the outfiles.

I am creating HTML pages. I have created views that output the proper html code, and it works one page at a time. What I am trying to do is create the procedure to update these pages once a month.

(originally posted in wrong category)
Roughly:
Begin;
#get country
SELECT cc1.Fips_10_4 as cntry FROM cc1;
 
#for each country get region codes
SELECT  rgn FROM camwme.rgn_center where Fips_10_4 =cntry;
 
#get cities
SELECT city from masterlist where region=rng and Fips_10_4 =cntry;
END;

Open in new window

0
Comment
Question by:CameraWithMe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 2

Assisted Solution

by:wcoka2
wcoka2 earned 2000 total points
ID: 24760684
To use a value of one select you can store the output in a variable

declare var1 integer;

#get country
SELECT cc1.Fips_10_4 into var1 FROM cc1;
0
 
LVL 2

Accepted Solution

by:
wcoka2 earned 2000 total points
ID: 24760707
for the loop you could do something like this:

declare tempcntry integer;
 
declare cur1 cursor for
SELECT cc1.Fips_10_4 as cntry FROM cc1;
 
declare continue handler for not found set done=1;
 
open cur1;
  loop1:loop
 
      fetch cur1 into tempcntry ;
 
        if done=1 then
          leave loop1;
       end if;
 
    BEGIN
      DECLARE not_found INT DEFAULT 0;
      DECLARE CONTINUE HANDLER FOR 1339 SET not_found=1;
 
#for each country get region codes
SELECT  rgn FROM camwme.rgn_center where Fips_10_4 =tempcntry;
 
 
    END;
 
  end loop loop1;
close cur1;

Open in new window

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question