2

I have a database with area details like state name, district, city, area size, ...

Now I would like to count a districts value from state name and assign that value to some variable for showing html page.

Example: The tamil nadu state includes total of 30 districts. 

Query for showing states list,

$qry = "SELECT DISTINCT state_Name FROM area_info ORDER BY state_Name";

How to do this.. anyone please help me..

5 Answers 5

2

You should try this, surely will solve your query.

$qry = "SELECT state_name, COUNT(district) As TotalData FROM area_info GROUP BY state_name";
Sign up to request clarification or add additional context in comments.

6 Comments

@Dinesh Dinu, try this solution
thanks. im not showing this count value in my html table. i will use this as html <p> tag. For an example The tamil nadu state includes total of 30 districts. In that example tamil nadu is not a mandatory. total of 30 districts is a dynamic count value. my html code will be 'The tamil nadu state includes total of <?php echo $district_Cont; ?> districts.'
Modified my answer, check it now.
Count value will return you the number of districts.
@DineshDiNu Check it now
|
2

Try this

$qry = "SELECT DISTINCT state_Name, COUNT(*) AS district  FROM area_info Group BY state_Name" ;

1 Comment

@Vickrant Example: The tamil nadu state includes total of 30 districts. State name is required. In addition Your answer is correct.
2

To get the count of the record, you can use below query

$totaldist = "SELECT count(DISTINCT) state_Name FROM area_info GROUP BY state_Name";

and then to assign this variable you can write like :

echo "The tamil nadu state includes total of $totaldist districts.";

1 Comment

Guys.. Thanks for the replay.. but not works for me. I would to count total districts from my db and im willing to store that value to $district_Cont. when i add <?php echo $district_Cont; ?> the count value should be replaced.
2

Try this Query:

SELECT count(district) 
FROM area_info 
GROUP BY state_Name

Comments

1

List with State name's and number of districts in it:

SELECT state_Name, COUNT(district)
FROM area_info
GROUP BY state_Name;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.