Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Query

New Here ,
Nov 23, 2006 Nov 23, 2006

Copy link to clipboard

Copied

Hello. How do I write the query for:

SELECT * FROM shirt;
+----+---------+--------+-------+
| id | name | num1| num2 |
+----+---------+--------+-------+
| 1 | nancy | 2 | 2 |
| 1 | bill | 1 | 4 |
| 2 | nancy | 3 | 16 |
| 2| bill | 7 | 2 |
| 2| steve | 7 | 6 |
| 3 | pam |8 | 7 |
| 3 | nancy | 1 | 8 |
| 3 | bill | 2 | 7 |
+----+---------+--------+-------+

Which will return this:

+----+---------+--------+-------+
| count | name | num1| num2 |
+----+---------+--------+-------+
| 3 | nancy | 6 | 26 |
| 3 | bill | 10 | 13 |
| 1 | steve | 7 | 6 |
| 1| pam | 8 | 7 |
+----+---------+--------+-------+

Primary Key (ID, unique)

I don't know if I've written everything correctly. I need to count the occurrance of ID for each Name and then show the sum of the values in Num1 and Num2 for each name.

Can anyone help me?

Thank you so much.
TOPICS
Server side applications

Views

752
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 23, 2006 Nov 23, 2006

Copy link to clipboard

Copied

.oO(Anhialator)

> I don't know if I've written everything correctly. I need to count the
>occurrance of ID for each Name and then show the sum of the values in Num1 and
>Num2 for each name.

Try

SELECT
COUNT(*) AS count,
name,
SUM(num1) AS num1,
SUM(num2) AS num2
FROM shirt
GROUP BY name

Micha

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 23, 2006 Nov 23, 2006

Copy link to clipboard

Copied



Micha! THANK YOU! It worked! I'm soooooooooo happy! You're brilliant, and you make it look so easy, which for you, it was, I'm sure!
Thank you!

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 24, 2006 Nov 24, 2006

Copy link to clipboard

Copied

LATEST
.oO(Anhialator)

>Thank you!

You're welcome.

Micha

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines