Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

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, 12:38
bono56 bono56 is offline
Registered User
 
Join Date: May 2004
Posts: 69
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, 05:25
georgev georgev is offline
SQL Apprentice
 
Join Date: Jan 2007
Location: hiding
Posts: 8,144
How do you define a records priority?
Date for the record to be actioned by?

A little more explanation needed please
__________________
George
You only stop learning when you stop asking questions.
Reply With Quote
  #3 (permalink)  
Old 09-25-07, 06:44
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 5,460
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
__________________
Warning
May! contain traces of NUT. people with NUT allergies should not pay attention to any of the above
Reply With Quote
  #4 (permalink)  
Old 09-26-07, 08:45
bono56 bono56 is offline
Registered User
 
Join Date: May 2004
Posts: 69
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, 08:57
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,556
Code:
SELECT people.* , ( select count(*) + 1 from people where age < t.age ) FROM people AS t ORDER BY age
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon
Reply With Quote
  #6 (permalink)  
Old 09-26-07, 09:07
georgev georgev is offline
SQL Apprentice
 
Join Date: Jan 2007
Location: hiding
Posts: 8,144
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
You only stop learning when you stop asking questions.
Reply With Quote
  #7 (permalink)  
Old 09-26-07, 10:38
bono56 bono56 is offline
Registered User
 
Join Date: May 2004
Posts: 69
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 12:44.
Reply With Quote
  #8 (permalink)  
Old 09-26-07, 10:50
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,556
ur? u?

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

__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon
Reply With Quote
  #9 (permalink)  
Old 09-26-07, 12:40
bono56 bono56 is offline
Registered User
 
Join Date: May 2004
Posts: 69
why? beacause of change in primary key?
Reply With Quote
  #10 (permalink)  
Old 09-27-07, 10:43
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 734
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 10: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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On