Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Temporary numbering in a query 1

Status
Not open for further replies.

kupe

Technical User
Sep 23, 2002
376
Is there a way to add a number column in a query, please, experts? As in

SELECT id, FName, SName, Date
FROM table
WHERE forum = 'TekTips';

But to add a column that acts as a sort of temporary id, 1 - 25 whatever, only for use on that one particular occasion.

 
You mean you want the returned records to be numbered from 1 onwards? You could use:
[tt]
@n=0;
SELECT @n:=@n+1 recnum,id,fname,sname,date
FROM tablename
WHERE forum='tektips';
[/tt]
 
Many thanks, Tony. This is somewhere I have not been before.

@n=0;
SELECT @n:=@n+1 recnum,id, about
FROM post;


My recnum column returns Null. Something I should add?


 
The @n:=0 statement initialises the counter. Maybe your interface doesn't handle multiple queries at once. If you issue two separate queries, the first being @n:=0; and the second being the SELECT, that should work.
 
Sorry, I'm half-asleep! You can forget my last post.

The syntax of the first line should have been:
[tt]
SET @n=0;
[/tt]
 
Excellent!

Stand on Thursday, and you'll get my vote. (Perhaps not quite as flattering as I mean when the others are considered.)

That is really good - and rather further on than my present level. (I can imagine I am advancing.)

Presumably, I would declare what I think must be a variable whenever I would like to use that numbering idea?

Many thanks, Master.
 
The @n (it doesn't have to be @n, you can call it anything you like) is a variable which you can manipulate. It doesn't need to be pre-declared, but is automatically initialised to NULL unless you assign it a value. Variables like that are local to the current session, so they retain their value between queries, and lose it when you disconnect.

(What's on on Thursday?)
 
General Election! I hope you are voting. England is in trouble and needs all voters (assuming, of course, you are based in this sceptre'd isle).

All noted about the variables. Much obliged.
 
Oh yes, I've heard about that.

I'm not based there though so I can't vote. I'm in Ireland.
 
Lucky man. Thursday evening will show whether any democracy remains in the world.

Thanks for all your help, Tony.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top