To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Database

Database Conversation regarding PHP and SQL

Reply
 
Thread Tools Rate Thread Display Modes
Old 11-04-2009, 02:42 PM   #1
sneakyimp
Senior Member
 
Join Date: Apr 2003
Location: LA
Posts: 3,210
generate row number dynamically with query?

I'm wondering if it might be possible to run a query that joins a couple of databases but also numbers each row. something like:
Code:
select BLAH() as row_number, t1.foo, t2.bar FROM table1 t1, table2 t2
which would return results like
Code:
1   record1foo   record1bar
2   record2foo   record2bar
3   record3foo   record3bar
Is this possible?
__________________
FlashMOG - make multiplayer games in Flash/PHP!
http://flashmog.net
Version 0.3 now available.
sneakyimp is offline   Reply With Quote
Old 11-04-2009, 02:50 PM   #2
dagon
RTFM it's a way of life.
 
dagon's Avatar
 
Join Date: Nov 2001
Location: N.Z
Posts: 3,099
SELECT @row :=0;# Rows: 1
SELECT @row := @row +1 AS rowNumber, t1.foo, t2.bar FROM table1 t1, table2 t2
dagon is offline   Reply With Quote
Old 11-05-2009, 08:50 AM   #3
Roger Ramjet
Senior Member
 
Roger Ramjet's Avatar
 
Join Date: Jul 2004
Location: Leeds, UK
Posts: 4,293
Quote:
Originally Posted by dagon View Post
SELECT @row :=0;# Rows: 1
SELECT @row := @row +1 AS rowNumber, t1.foo, t2.bar FROM table1 t1, table2 t2
In a stored procedure of course: and looking at your syntax, in an MS SQL database as well.

For the method to do this all in 1 query in any dialect of sql have a read of my blog
__________________
David Soussan
Roger Ramjet is offline   Reply With Quote
Old 11-05-2009, 02:15 PM   #4
dagon
RTFM it's a way of life.
 
dagon's Avatar
 
Join Date: Nov 2001
Location: N.Z
Posts: 3,099
Quote:
Originally Posted by Roger Ramjet View Post
In a stored procedure of course: and looking at your syntax, in an MS SQL database as well.
Actually it was from the mysql forums. And tested before posting.
dagon is offline   Reply With Quote
Old 11-05-2009, 02:32 PM   #5
Roger Ramjet
Senior Member
 
Roger Ramjet's Avatar
 
Join Date: Jul 2004
Location: Leeds, UK
Posts: 4,293
Quote:
Originally Posted by dagon View Post
Actually it was from the mysql forums. And tested before posting.
Just the @: usual for sql server but not so much for others
__________________
David Soussan
Roger Ramjet is offline   Reply With Quote
Old 11-05-2009, 03:25 PM   #6
sneakyimp
Senior Member
 
Join Date: Apr 2003
Location: LA
Posts: 3,210
Thanks for the help. I've run into this problem several times before. This does in fact work for me in mysql 4 and 5:
Code:
SET @row =0;
SELECT @row := @row +1 AS row_number, c.customer_id, u . *
FROM users u
LEFT JOIN customers c ON c.user_id = u.user_id
WHERE c.user_id IS NULL ;
I can find user records that have no associated customer records and give them a sequence number.

Roger, I'm very curious about your approach and was having a bit of trouble figuring out how to apply it when I've already got a join on two tables when it occurred to me that this might be a pretty inefficient query if I have large numbers of records. Seems to me the COUNT bit has to be run for each id.

THANKS both of you, btw.
__________________
FlashMOG - make multiplayer games in Flash/PHP!
http://flashmog.net
Version 0.3 now available.
sneakyimp is offline   Reply With Quote
Old 11-06-2009, 09:10 AM   #7
Roger Ramjet
Senior Member
 
Roger Ramjet's Avatar
 
Join Date: Jul 2004
Location: Leeds, UK
Posts: 4,293
Yes, sneakyimp, it does.

The purpose of my approach is to solve problems that can not be done any other way: eg

delete all but the last 5 posts in every thread/category/sub-set

display the first 3 posts in each thread in all forums

Sequence complex grouping of an arbitrary number of nested subsets stored as hiearchical data (which is what I was solving when I devised this method).

That is, my method is devised to solve those problems that can not be solved by LIMIT (even when over partitions), GROUP BY with MAX(id)/MIN(id) etc because we wont something other than the first or last item in the sub-sets.

The sequence problem itself can be solved more efficiently in many databases with vendor specific code, but this is vendor specific and not universal; and remember, not all versions of any database support stored procedures and some don't support them at all.
__________________
David Soussan
Roger Ramjet is offline   Reply With Quote
Old 11-07-2009, 01:34 AM   #8
Roger Ramjet
Senior Member
 
Roger Ramjet's Avatar
 
Join Date: Jul 2004
Location: Leeds, UK
Posts: 4,293
Whatever happened to the edit button? Can't even correct my spelling any more

I realise that I did not answer your question fully sneakyimp. The COUNT has to be run of course, but it is the GROUP BY that create the overhead in a query like this and that is quite normal no matter what method one uses.
__________________
David Soussan
Roger Ramjet is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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
Forum Jump


All times are GMT -4. The time now is 04:53 PM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.