Jump to content

[SOLVED] Count the rows


santrowithu

Recommended Posts

Hello friends,

 

I have a table named myTable with the column names NAME and Degree.

 

The table looks similar to this:

 

NAME | Degree

------------------

AQW | (null)

AQW | (null)

AQW | (null)

PPR | (null)

PPR | (null)

RES | (null)

MEN | (null)

MEN | (null)

MEN | (null)

MEN | (null)

 

I want to get the count of name and update the degree. The table then should look like..

 

NAME | Degree

------------------

AQW | 3

AQW | 3

AQW | 3

PPR | 2

PPR | 2

RES | 1

MEN | 4

MEN | 4

MEN | 4

MEN | 4

 

Can somebody help me please?

 

Santro!

 

I tried the following query

UPDATE mytable AS a SET degree = (SELECT COUNT(NAME) FROM mytable b GROUP BY NAME) WHERE a.name <> NULL;

 

the error is: Error Code : 1093

You can't specify target table 'a' for update in FROM clause

 

Link to comment
Share on other sites

Okay. Thanks for the help. I will figure out some other option.

Like a temporary table?

 

Ya, I tried it and it worked.

 

For others with the similar issue, I tried this here..

 

CREATE VIEW bufferView(name,degree)

AS SELECT name,COUNT(*)

FROM myTable

GROUP BY name;

 

UPDATE myTable t

SET degree = (

SELECT degree

FROM bufferView

WHERE name = t.name);

 

DROP VIEW bufferView;

 

Once again, thanks everyone for your help

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.