LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-04-2008, 11:35 AM   #1
ShaqDiesel
Member
 
Registered: Jul 2005
Posts: 144

Rep: Reputation: 15
mysql loop


Can mysql loop through results?

I'd like to do something like:

for (select id from tablename)
select colName from tablename2 whereid="id_from_current_iteration";
 
Old 11-04-2008, 11:42 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,635

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by ShaqDiesel View Post
Can mysql loop through results?

I'd like to do something like:

for (select id from tablename)
select colName from tablename2 whereid="id_from_current_iteration";
Yes..you can either do it with a MySQL stored procedure (info here http://www.browardphp.com/mysql_manu...rocedures.html), or write a program to do it. Perl has great hooks into MySQL....
 
Old 11-04-2008, 01:29 PM   #3
nishamathew1980
Member
 
Registered: Oct 2008
Posts: 37

Rep: Reputation: 16
If you are looking for something akin to cursors in PL/SQL - the closest thing MySQL offers is Procedures. But please note - it is nowhere close to as powerful and useful as cursors.

If you have pressing need to iterate through resultsets - either do it at your program layer (whichever the language) or migrate to an Oracle DB.

Linux Archive

Last edited by nishamathew1980; 11-09-2008 at 04:55 AM.
 
Old 11-04-2008, 05:44 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Or a sub-select:

Code:
select colName from tablename2 whereid in (select id from tablename);
 
Old 11-04-2008, 08:39 PM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
SQL is all about using *sets*.

Even though most dialects of SQL give you ways to iterate through individual items in your "select", it's almost always a Bad Idea to fetch anything besides a complete set at the server (then feel free to iterate to your heart's content, if need be, once you get the result set back to the client).

As pointed out above:
Quote:
for (select id from tablename)
select colName from tablename2 whereid="id_from_current_iteration";
-- Different databases usually have different SQL extensions that let you do this ... but you *shouldn't* (at least not directly from SQL)
Quote:
select colName from tablename2 whereid in (select id from tablename);
-- Much, much better
Here's a good article that explains further:
http://www.paragoncorporation.com/Ar...x?ArticleID=27

Last edited by paulsm4; 11-04-2008 at 08:40 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM
converting shell while loop to for loop farkus888 Programming 8 09-12-2007 02:30 AM
for loop only works properly on first loop symo0009 Programming 1 12-25-2005 05:17 PM
PHP "for.. loop" to insert into mysql Boby Programming 10 05-29-2004 10:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration