If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > INSERT counter field to a table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-24-07, 11:38
bono56 bono56 is offline
Registered User
 
Join Date: May 2004
Posts: 133
INSERT counter field to a table

hi
i add a field name `priority` to myTable.
i want to save priority of records in this field when select them by sepecfic criteria & order by. like a counter 1...n.
this means priority field for the first selected record is 1 & for n`th selected record is n.
is it possible to make it by mySQL? doing this task by php becase of number of records is so slow.
Reply With Quote
  #2 (permalink)  
Old 09-25-07, 04:25
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
How do you define a records priority?
Date for the record to be actioned by?

A little more explanation needed please
__________________
George
Twitter | Blog
Reply With Quote
  #3 (permalink)  
Old 09-25-07, 05:44
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
if the priority is going to be predetermined then you can use a column, called say SortSeq, and use that as an indexed column to display rows in a predetermined order. You cna set the SortSeq values and then using that as an indexed column (perhaps in conjunction with an Alphabetic column you can vary the display sequence as required

if however you need to create this sort sequence on the fly then I'm not sure how you could do that. how can you create an index based on a sort sequence the user wants to pick seemingly at random. unless you have some whizzy algolrhytm which could create a sort value on the fly.. even if you do that it will be horrendously slow as the result would be unindexed, unless you put the values into say a temporary table

before you start delving into this are you certain you are using indexes appropriately, are you certain your joins are formed correctly or most efficiently, are you applying the where criteria in the right manner. The slowness of the app may be down to other factors, such as server load (web or data), poor / non optimised design.

HTH
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #4 (permalink)  
Old 09-26-07, 07:45
bono56 bono56 is offline
Registered User
 
Join Date: May 2004
Posts: 133
hi
sorry if i couldnt explain well, maybe a sample can make it more clear:

suppose table with this data:

id fname age
-- ----- ---
1 fred 26
2 chris 19
3 sara 36
4 john 23
5 billy 31


now i select them like this:

SELECT * FROM people ORDER BY age
id fname age
-- ----- ---
2 chris 19
4 john 23
1 fred 26
5 billy 31
3 sara 36

& i want to add a field (priority) & set it like this:

id fname age priority
-- ----- --- --------
2 chris 19 1
4 john 23 2
1 fred 26 3
5 billy 31 4
3 sara 36 5
Reply With Quote
  #5 (permalink)  
Old 09-26-07, 07:57
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
SELECT people.* 
     , ( select count(*) + 1
           from people
          where age < t.age )
  FROM people AS t
ORDER BY age
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 09-26-07, 08:07
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Sounds like you want what's called a "running total"...

Why does age denote priority? I know it's probably just an example but it's very misleading. Real sample data will more than likely help!
__________________
George
Twitter | Blog
Reply With Quote
  #7 (permalink)  
Old 09-26-07, 09:38
bono56 bono56 is offline
Registered User
 
Join Date: May 2004
Posts: 133
tanX r937, ur statement gave me a soultion, what do u think about it?

Code:
#create a mirror table with priority field:
CREATE TABLE `peopleWithPriority` (
  `id` int(10) unsigned NOT NULL,
  `fname` varchar(100) NOT NULL default '',
  `age` varchar(20) NOT NULL default '0',
  `priority` int(10) unsigned NOT NULL auto_increment,
  PRIMARY KEY  (`priority`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

#insert to new table(with priority):
INSERT INTO `peopleWithPriority` (`id`, `fname`, `age`) 
SELECT `id`, `fname`, `age` from people ORDER BY age;

#change primary
#remove autoincreament
ALTER TABLE `peoplewithpriority` CHANGE `priority` `priority` INT( 10 ) UNSIGNED NOT NULL;
#change primary
ALTER TABLE `peoplewithpriority` DROP PRIMARY KEY ,
ADD PRIMARY KEY ( `id` ) ;
ALTER TABLE `peoplewithpriority` CHANGE `id` `id` INT( 10 ) UNSIGNED NOT NULL AUTO_INCREMENT;
ALTER TABLE `peoplewithpriority` ADD INDEX ( `priority` );

Last edited by bono56; 09-26-07 at 11:44.
Reply With Quote
  #8 (permalink)  
Old 09-26-07, 09:50
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
ur? u?

eye think ewe will have a problem as soon as ewe add another person to that table

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 09-26-07, 11:40
bono56 bono56 is offline
Registered User
 
Join Date: May 2004
Posts: 133
why? beacause of change in primary key?
Reply With Quote
  #10 (permalink)  
Old 09-27-07, 09:43
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
I am wondering why ewe would not utilise the statement that has already been granted to yourself.

Last edited by aschk; 09-27-07 at 09:49.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On