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 > Update from Select Count

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-11-09, 10:55
daraghk daraghk is offline
Registered User
 
Join Date: May 2009
Posts: 3
Update from Select Count

Posted - 05/07/2009 : 13:31:42
--------------------------------------------------------------------------------

I am looking to update a column in a table with a count of duplicate entries. The example below will clarify as bit better

Table name - items

name toy thecount
tom box
dave truck
tom block
mike car
dave house

The query should update the count field with the number of entries for tom, dave etc so for each entry for tom the count value will be 2, for dave will be 2 and mike will be 1.

I can get the count returned using the following query
SELECT items.name, Count(items.toy) AS countoftoys
FROM items
GROUP BY items.name;

I have been unable to take this to the next stage where the results are updated back into the thecount column

This is a simplified example of a real world application I have but is too complicated to get into.

Any insight anyone can provide will be much appreciated
Reply With Quote
  #2 (permalink)  
Old 05-11-09, 11:04
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
CREATE TABLE itemcounts
SELECT items.name, Count(items.toy) AS countoftoys
FROM items
GROUP BY items.name;

UPDATE items
INNER
  JOIN itemcounts
    ON itemcounts.name = items.name
   SET items.thecount = itemcounts.countoftoys;
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 05-11-09, 11:11
daraghk daraghk is offline
Registered User
 
Join Date: May 2009
Posts: 3
Wow, quick reply

This however does not give the result I am looking for as it involves sending the results to a 'New' table. I am looking to have the results stored in the original table in the 'thecount' column.

I can achieve this by using your suggested method and adding another step to update the items table but I am trying to avoid having that 2 steps process and having that intermediate temporary table
Reply With Quote
  #4 (permalink)  
Old 05-11-09, 11:16
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
you have to use a 2-step process like i showed you

mysql will not let you update the same table you're selecting from
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 05-11-09, 11:18
daraghk daraghk is offline
Registered User
 
Join Date: May 2009
Posts: 3
appreciate your help

Thanks for that. I guess that explains why I have been having SO much difficulty with this
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