Matarishvan
Matarishvan

Reputation: 2432

Query for Summing the values based on text mysql

Basically i have this table . And i need the sum the crop_stage values based on crop_text

----------    ----------    
crop_stage    crop_text    
----------    ----------
12             Preflowering
3              Fertilizing
10             Sowing
10             Sowing
2              Preflowering
15             Fertilizing

And i need to update the crop_stage values so that i should get

----------    ----------    
crop_stage    crop_text    
----------    ----------
14             Preflowering
18             Fertilizing
20             Sowing

I have written my select query which isnt working good.

"SELECT crop_text, SUM(crop_stage) AS crop_stage FROM table GROUP BY crop_text"

EDIT

I am doing json_encode with my select query, which is actually giving me this.

[["Preflowering",12],["Fertilizing",3],["Sowing",10],["Sowing",10],["Preflowering",2],["Fertilizing",15]

Upvotes: 2

Views: 134

Answers (2)

Saif
Saif

Reputation: 7052

SELECT crop_text, SUM(crop_stage) AS crop_stage FROM table_name GROUP BY crop_text order by crop_stage ASC

Upvotes: 0

Nikos
Nikos

Reputation: 3297

The word table is reserved. Write the query like this:

SELECT crop_text, SUM(crop_stage) AS crop_stage FROM `table` GROUP BY crop_text;

Hope this helps

Upvotes: 5

Related Questions