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
Solved

nesting select statements in MYSQL stored procedure

Posted on 2009-07-01
6
461 Views
Last Modified: 2013-11-23
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.


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
  • 3
  • 3
6 Comments
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24753226
You can (and should) do this with joins. Try something like this:
SELECT cc1.Fips_10_4 as cntry, r.rgn, m.city
FROM cc1, camwme.rgn_center as r, masterlist as m
where 
  cc1.Fips_10_4 = r.Fips_10_4 AND 
  cc1.Fips_10_4 = m.Fips_10_4 AND
  r.rng = m.region 

Open in new window

0
 

Author Comment

by:CameraWithMe
ID: 24753347
Cxr- thanks for the superfast response.

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.
0
 
LVL 39

Assisted Solution

by:Roger Baklund
Roger Baklund earned 200 total points
ID: 24753688
When you say "page" I assume you mean HTML pages. You can not (without considerable effort) create HTML pages from within a stored procedure. You need some scripting/programming language. You post in a Delphi zone, so I assume you are using Delphi for this. :)

If your Deplhi code generates multiple static HTML pages, you could use that single join statement, loop over the results, and write to multiple pages at the same time. See meta code below.

If your Deplhi code is running like a CGI, i.e. it executes for each page click, I would split it in three different SQL statements, similar to those you had in the original question. There is nothing to gain from having this in a one large loop.
currentCountry = false
currentRegion = false
for each country/region/city record:
  if currentCountry != country:
    if currentCountry != false: save currentCountry page
    currentCountry=country
    create/open new country page
  if currentRegion != region:
    if currentRegion != false: save currentRegion page 
    currentRegion = region
    create/open new region page
    write region to country page
  write city to region page

Open in new window

0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Accepted Solution

by:
CameraWithMe earned 0 total points
ID: 24753784
I thought I posted it in a MYSQL zone. I will see what I can do to recatagorize.
Yes 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.
0
 

Author Comment

by:CameraWithMe
ID: 24753890
Reposted in proper category
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24753891
>> I thought I posted it in a MYSQL zone.

You did, this question is in two zones: "Internet and Delphi Programming" and "MySQL Server".

>> I have created views that output the proper html code

SQL views? How did you do that...?

>> What I am trying to do is create the procedure to update these pages once a month.

Using mysql only?

I am sorry for asking so many questions, but I need to understand your situation to be able to help you.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi Yen format 3 45
Mysql Left Join Case 10 70
Delphi Seattle StructureView color 1 14
java mysql insert application 14 26
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

840 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