Count and sum from table - php

I have this table below that I need to get the number of student that get A in their exam. This is what I want to achieve from table below:
3 A's = 0 student. 2 A's = 3 student.
+--------------+------------+------+
| student_ID | kod_subjek | gred |
+--------------+------------+------+
| 746123096687 | 02 | A |
| 746123096687 | 12 | B |
| 746123096687 | 21 | A |
| 860206145454 | 12 | A |
| 860206145454 | 02 | A |
| 881012085535 | 02 | A |
| 881012085535 | 21 | A |
+--------------+------------+------+
I try with:
mysql> SELECT student_ID, COUNT(gred) FROM data_exam GROUP BY student_ID;
The output is:
+--------------+-------------+
| student_ID | COUNT(gred) |
+--------------+-------------+
| 746123096687 | 3 |
| 860206145454 | 2 |
| 881012085535 | 2 |
+--------------+-------------+
It not work. It will just count all the grade for specific student. Please help me solve this. Thank you.

SELECT a_count, COUNT(*) AS cnt
FROM
(
SELECT COUNT(*) AS a_count
FROM data_exam
WHERE gred = 'A'
GROUP BY student_id
) x
GROUP BY a_count
ORDER BY a_count
Example on SQL-Fiddle returns:
a_count cnt
2 3

You can use a subquery:
SELECT num_as, COUNT(1) AS num_students
FROM (
SELECT student_ID, COUNT(gred) AS num_as
FROM data_exam
WHERE gred = 'A'
GROUP BY student_ID
) counts_by_student
GROUP BY num_as

Related

How to update mysql table in PHP on the basis of another table

I have the following tables in my database:
Table order
|------------------------------------------------------|
| Id | uid | order | price | City_to_be_update
|------------------------------------------------------|
| 1 | 25 | someFoods | 10025 |
|------------------------------------------------------|
| 2 | 30 |Some veggies| 2015|
|------------------------------------------------------|
| 3 | 12 |Milk | 145 |
|------------------------------------------------------|
Table Users:
|------------------------------------------------|
| Id | uid | city_id | Address|
|------------------------------------------------|
| 1 | 30 | 1 | myAddress |
|------------------------------------------------|
| 2 | 12 | 2 | Asdfads |
|------------------------------------------------|
| .... | .. | .. | ....... |
|------------------------------------------------|
Table City:
|------------------------------------------------|
| Id |city_id| city_name | Country|
|------------------------------------------------|
| 1 | 2 | Abc | Uae |
|------------------------------------------------|
| 2 | 3 | Def | Ghi |
|------------------------------------------------|
| .... | .. | .. | ....... |
|------------------------------------------------|
What I want to do is
Select * from `order`
$user_id [ uid ];
Select * from `users` where `uid` = ‘$user_id’;
$city_id [ city_id ];
Select * from city where city_id = ‘$city_id’;
$city_name [ city_name ];
Finally want to update the order table as below
Update `order` SET ` City_to_be_update`= $city_name’;
How to do this, please can anyone help me?
Try this Query with Joins,
UPDATE
< OrderTable > AS a LEFT JOIN < UsersTable > AS b ON a.uid=b.uid
LEFT JOIN < CityTable > AS c ON b.city_id=c.city_id
SET
a.City_to_be_update=c.city_name

how to calculate unique column values in mysql

Here is my data
cardNo| userName| tablename| hours | date
1 | a | a | 12 | 12-06-2015
1 | a | a | 5 | 11-06-2015
2 | b | b | 3 | 15-06-2015
1 | a | a | 8 | 12-06-2015
2 | b | b | 3 | 21-06-2015
1 | a | a | 12 | 14-06-2015
2 | b | b | 10 | 8-06-2015
cardNo is unique. I need to display all details and total hours for each card, like:
cardNo | userName | tablename | totalhours
1 | a | a | 37
2 | b | b | 16
It's simple SUM() with GROUP BY:
SELECT cardNo,sum(hours)
FROM yourtable
GROUP BY cardNo;
I left it as an exercise for the OP to include userName and tablename columns into the query
SELECT cardNo,userName, tablename, sum(hours) hours
FROM Table_1 GROUP BY cardNo,userName,tablename

MYSQL select recent record of each from

See my table(sample_table),
-----------------------------
id | from | to |
-----------------------------
1 | 2 | 1 |
3 | 2 | 1 |
4 | 2 | 4 |
5 | 3 | 2 |
9 | 3 | 1 |
11 | 4 | 1 |
12 | 4 | 3 |
-----------------------------
For each from, I would like the row holding the most recent to, where to = 1
I mean I want only following,
-----------------------------
id | from | to |
-----------------------------
3 | 2 | 1 |
9 | 3 | 1 |
11 | 4 | 1 |
-----------------------------
I Try following Query,
SELECT * FROM sample_table WHERE to = 1 GROUP BY from
It's giving first row of each. Help me.
Thanks,
There are many ways to do it and here is one way
select t1.* from sample_table t1
join(
select max(id) as id,`from` from
sample_table where `to` = 1
group by `from`
)t2
on t1.id= t2.id and t1.`from` = t2.`from`
https://dev.mysql.com/doc/refman/5.0/en/example-maximum-column-group-row.html
Try this
select t1.id, t1.from, t1.to from table as t1 inner join
(
select to, from,min(id) as id from table
where to=1
group by to,from
) as t2
on t1.to=t2.to and t1.id=2.id and t1.from=t2.from

MySQL getting total number of enquiries for each branch

I have three tables and they are the following
User Table
+---------+-----------+--------+
| user_id | user_name | branch |
+---------+-----------+--------+
| 1 | John | 1 |
| 2 | Jim | 2 |
| 3 | Jern | 3 |
| 4 | Jack | 1 |
| 5 | Jery | 2 |
| 6 | Tom | 3 |
| 7 | Sona | 1 |
| 8 | Tina | 3 |
+---------+-----------+--------+
Branch Table
+-----------+----------------+
| branch_id | branch_name |
+-----------+----------------+
| 1 | IT |
| 2 | SALES |
| 3 | Administration |
+-----------+----------------+
Enquiry Table
+------------+---------------+---------+
| enquiry_id | enquiry_name | user_id |
+------------+---------------+---------+
| 1 | enqury_test1 | 1 |
| 2 | enqury_test2 | 2 |
| 3 | enqury_test3 | 1 |
| 4 | enqury_test4 | 3 |
| 5 | enqury_test5 | 2 |
| 6 | enqury_test6 | 5 |
| 7 | enqury_test7 | 1 |
| 8 | enqury_test8 | 2 |
| 9 | enqury_test9 | 4 |
| 10 | enqury_test10 | 6 |
| 11 | enqury_test11 | 2 |
| 12 | enqury_test12 | 7 |
+------------+---------------+---------+
From the above tables its clear that, each branch contains a number of users.
These users post multiple enquiries.
I need to get the total number of enquiries in each branch as
branch id => number of enquiries
I have tried various queries. But i couldn't get the result. Any one can help?
I am using MySQL and i need a single query to do this.
Thanks in advance
You need count and group by
select
b.branch_id,
count(e.user_id) as `total_enq`
from Branch b
left join User u on u.branch = b.branch_id
left join Enquiry e on e.user_id = u.user_id
group by b.branch_id
The query you have to perform to get you desired result is like this :-
$query = "SELECT u.branch, COUNT(u.user_id) AS `total_enquires`
FROM enquiry e INNER JOIN user u ON e.user_id = u.user_id
GROUP BY u.branch"
This will help you,and i think you don't need to join branch table as user table already contain branch_id.
This is the query
SELECT `branch`,`branch_name`,count(`user`.`user_id`),count(`enquiry_id`) FROM `user` inner join `branch` on `user`.`branch`=`branch`.`branch_id` inner join `enquiry` on `user`.`user_id`=`enquiry`.`user_id` group by `branch`
try it here
http://sqlfiddle.com/#!9/cf3eb/1
SELECT
bt.branch_id
,COUNT(enquiry_id) AS total_enquiry
FROM
enquiry_table et
INNER JOIN user_table ut on ut.user_id = et.user_id
INNER JOIN branch_table bt ON bt.branch_id = ut.branch
WHERE
1=1
GROUP BY
bt.branch_id
you can try this

Selecting random triplicates (or more) from database

I have two tables:
Students Student_Grades
V------------------------V
+----+------+ +----+------------+---------+-------+
| id | name | | id | student_id | subject | grade |
+----+------+ +----+------------+---------+-------+
| 0 | Dave | | 0 | 0 | Math | 100 |
+----+------+ +----+------------+---------+-------+
| 1 | John | | 1 | 0 | Chem | 90 |
+----+------+ +----+------------+---------+-------+
| 2 | Kate | | 2 | 0 | CompSCI | 95 |
+----+------+ +----+------------+---------+-------+
| 3 | Mimi | | 3 | 1 | ELA | 98 |
+----+------+ +----+------------+---------+-------+
| 4 | 2 | Biology | 92 |
+----+------------+---------+-------+
| 5 | 2 | Chem | 94 |
+----+------------+---------+-------+
| 6 | 2 | Math | 98 |
+----+------------+---------+-------+
| 7 | 3 | Math | 100 |
+----+------------+---------+-------+
I would like to select all subjects and grades from a random student that is enrolled in more than three subjects. (Either Dave or Kate)
Students John and Mimi would not be even considered because they are not enrolled in three subjects.
I know I can achieve this with PHP but I would like this to be done with one query to the database.
SELECT * FROM Students t JOIN (SELECT CEIL(MAX(ID)*RAND()) AS ID FROM Students) AS x ON t.ID >= x.ID LIMIT 1
With the above query, I have selected a random student, with that I can go in and check if they have three subjects with SELECT count(subjects) FROM Students WHERE id=random_id.
If the count returned is below three, then I throw away the results and run the first query again.
How would I attempt this in one query?
This is tested and working:
SELECT *
FROM Students s
JOIN (
SELECT student_id
FROM Student_Grades
GROUP BY student_id
HAVING COUNT(*) >= 3
ORDER BY RAND()
LIMIT 1
) rs
ON rs.student_id = s.id
JOIN
Student_Grades sg
ON sg.student_id = s.id
Here's the SQL Fiddle: http://sqlfiddle.com/#!2/e5b5b/1

Categories