It is currently Sun Dec 16, 2018 8:35 pm




Post new topic Reply to topic  [ 4 posts ] 
Finding MySQL next free name 
Author Message

Joined: Fri Aug 15, 2008 4:05 am
Posts: 3
Post Finding MySQL next free name
Hello folks.
I have been trying to do somthing like find next free name but just cant manage to work it.
The idea is i have one Table where i collect computer names like xf0258 xf0259... and kk0012 k0013...
Those xf an kk mean server or workstation and the number goes on...
How can i make an sql query that can figure out what is the next free number for example if i select server.

Is this possible with dbqwiksite 6 or do i have to add a query in mysql somewhere?

Thanks.


Tue Feb 08, 2011 7:42 pm
Profile E-mail

Joined: Sun Jun 07, 2009 8:54 am
Posts: 65
Post Re: Finding MySQL next free name
YEs it is possible. You generate a string and pass it to the MySQL processor.

If needed, I can program it in custom code for dbqwiksite6 for approx. 200


Wed Feb 09, 2011 12:59 am
Profile E-mail

Joined: Thu May 29, 2008 4:49 pm
Posts: 16
Post Re: Finding MySQL next free name
It seems only the number changes.
So why not use two fields for each entity (server /workstation).
One contains the text id and the other the unique number.
Retrieval should be trivial as you are just selecting unique id for last inserted row.

MySql site - How to Get the Unique ID for the Last Inserted Row

SELECT LAST_INSERT_ID() statement with mysql_query()

or if using MyISAM tables

from http://dev.mysql.com/doc/refman/5.5/en/ ... ement.html

For MyISAM tables you can specify AUTO_INCREMENT on a secondary column in a multiple-column index. In this case, the generated value for the AUTO_INCREMENT column is calculated as MAX(auto_increment_column) + 1 WHERE prefix=given-prefix. This is useful when you want to put data into ordered groups.

CREATE TABLE animals (
grp ENUM('fish','mammal','bird') NOT NULL,
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL,
PRIMARY KEY (grp,id)
) ENGINE=MyISAM;

INSERT INTO animals (grp,name) VALUES
('mammal','dog'),('mammal','cat'),
('bird','penguin'),('fish','lax'),('mammal','whale'),
('bird','ostrich');

SELECT * FROM animals ORDER BY grp,id;

Which returns:

+--------+----+---------+
| grp | id | name |
+--------+----+---------+
| fish | 1 | lax |
| mammal | 1 | dog |
| mammal | 2 | cat |
| mammal | 3 | whale |
| bird | 1 | penguin |
| bird | 2 | ostrich |
+--------+----+---------+


Wed Feb 09, 2011 5:32 pm
Profile E-mail

Joined: Fri Aug 15, 2008 4:05 am
Posts: 3
Post Re: Finding MySQL next free name
@sleavy:

Thank you very much this helps me alot :)


Wed Feb 09, 2011 6:12 pm
Profile E-mail
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Copyright © 2004-2008 TheDevShop Ltd. All Rights Reserved