Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have table name student as below:
+---------------------------+
| PCLASS   |   GEN  | CASTE |
+---------------------------+
|5th Class | Male   | BC    |
|2nd Class | Female | SC    |
|5th Class | Female | ST    |
|2nd Class | Female | BC    |
+---------------------------+

I want to display the query like this:
+----------------------------------------------------------------------+
| CASTE    |        BC     |       SC      |       ST      |    GTOT   | 
+----------------------------------------------------------------------+
| PCLASS   |Male|Female|TOT|Male|Female|TOT|Male|Female|TOT|Male|Female|
+----------------------------------------------------------------------+
|2nd Class |  0 |   1  | 1 |  0 |  1   | 1 |  0 |  0   | 0 |  0 |  2   |
|5th Class |  1 |   0  | 1 |  0 |  0   | 0 |  0 |  1   | 1 |  1 |  1   |
+----------------------------------------------------------------------+
|Total                                                     |  1 |  3   |
+----------------------------------------------------------------------+

this code displaying all the same the data. please any suggestion for this one.

What I have tried:

PHP
$result = $conn->query("select PCLASS, sum(case when GEN = 'Male' and  CASTE='SC' or CASTE='ST' or CASTE='BC' or CASTE='OC' or CASTE='Minority' then 1 else 0 end) Males,
sum(case when GEN = 'Female' and CASTE='SC' or CASTE='ST' or CASTE='BC' or CASTE='OC' or CASTE='Minority' then 1 else 0 end) Females, count(*) as Total from student
where OID='{$_SESSION["OID"]}' 
group by PCLASS"); 
				
while($row = mysqli_fetch_array($result)) {
Posted
Updated 15-Jan-21 1:25am
v2
Comments
Richard Deeming 15-Jan-21 7:25am    
If the user has any control over the OID session variable at all, then your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

PHP: SQL Injection - Manual[^]
[no name] 15-Jan-21 8:15am    
This falls under the "Excel is not a database" category. "SQL is not a report writer".
CHill60 15-Jan-21 8:20am    
You state "this code displaying all the same data" - no it's not. It returns
PCLASS	Males	Females	Total
2nd Class	1	2	2
5th Class	2	2	2
Your SQL query will never reproduce the results in the format in your "picture" - each row must contain the full set of data- that's something you sort out in your presentation layer after you get your results back.
In the meantime, it looks as if you are trying to pivot on two columns - have a read of this article PIVOT on two or more fields in SQL Server | Microsoft Docs[^]
GCSHEKAR 16-Jan-21 0:50am    
thank you all for your suggestions... I am getting good result today about my code. but I missing total that showing same total result...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900