How to write this complex SQL statement - php

I have three identical tables in my MySQL table namely
first_term_result, second_term_result and third_term_result
this are the columns in it
exam_type_id | student_id | subject_id | mark |
or example with dummy data
NOTE: there are three different exam type for each subjects (CA1, CA2, CA3 and Exam),
there are three table like this with same thing but different data as it hold data for first term another for second term and the last for third term.
first_term_result:
exam_type_id | student_id | subject_id | mark |
1 | 6 | 7 | 12 |
2 | 6 | 7 | 9 |
3 | 6 | 7 | 13 |
4 | 6 | 7 | 45 |
1 | 4 | 7 | 7 |
2 | 4 | 7 | 5 |
3 | 4 | 7 | 10 |
4 | 4 | 7 | 34 |
second_term_result:
exam_type_id | student_id | subject_id | mark |
1 | 6 | 7 | 15 |
2 | 6 | 7 | 6 |
3 | 6 | 7 | 10 |
4 | 6 | 7 | 50 |
1 | 4 | 7 | 6 |
2 | 4 | 7 | 3 |
3 | 4 | 7 | 9 |
4 | 4 | 7 | 44 |
third_term_result:
exam_type_id | student_id | subject_id | mark |
1 | 6 | 7 | 17 |
2 | 6 | 7 | 8 |
3 | 6 | 7 | 15 |
4 | 6 | 7 | 67 |
1 | 4 | 7 | 12 |
2 | 4 | 7 | 8 |
3 | 4 | 7 | 12 |
4 | 4 | 7 | 50 |
Now what i want to achieve is get the SUM() of first_term_result.mark second_term_result.mark and third_term_result.mark of each students WHERE subject_id=7 group by students name.
another very important problem is i will be calculating the grand sum for each students for first_term+second_term+third_term and also want to be able to order the grand total for that student and the subjects in DESC so i can position them accordingly please if it will be easier on php please let me know.
Thanks
it seems very complex to me but i know there are gurus here who ca achieve this, i read somewhere that it is possible to order by even when rollup is used.
below is my code which doesn't work obviously.
SELECT CONCAT(s.fname,' ',s.mname,' ',s.lname) AS sname,
SUM(f.mark) AS first_total,
SUM(se.mark) AS second_total,
SUM(t.mark) AS third_total
SUM(f.first_total,second.total,third_total) as GT // just to show my intention
FROM students s, first_term_result f, second_term_result se, third_term_result t
WHERE s.studentID=f.student_id AND
s.studentID=se.student_id AND
s.studentID=t.student_id AND
f.subject_id=7 AND
se.subject_id=7 AND
t.subject_id=7
GROUP BY sname ORDER BY GT DESC

SELECT CONCAT(MS.fname,' ',MS.mname,' ',MS.lname) AS sname,
M.FTotal, M.STotal, M.TTotal,
(M.FTotal + M.STotal + M.TTotal) AS GrandTotal
FROM (
SELECT st.studentID,
(
SELECT SUM(f.mark)
FROM first_term_result AS f
WHERE f.subject_id = 7
AND f.student_id = st.studentID
) AS FTotal,
(
SELECT SUM(s.mark)
FROM second_term_result AS s
WHERE s.subject_id = 7
AND s.student_id = st.studentID
) AS STotal,
(
SELECT SUM(t.mark)
FROM third_term_result AS t
WHERE t.subject_id = 7
AND t.student_id = st.studentID
) AS TTotal
FROM students AS st
WHERE st.studentID IN (
SELECT studentID
FROM first_term_result AS fs
WHERE fs.subject_id = 7
AND fs.student_id = st.studentID)
GROUP BY st.studentID
) AS M
JOIN students MS ON M.studentID = MS.studentID
ORDER BY (M.FTotal + M.STotal + M.TTotal) DESC

Related

SQL SELECT row with highest value where problem_id is the same

I'm working on a project where I have to list the solutions with the highest votes per problem.
Every problem has two solutions and the users can vote on one solution per problem. This is my database at the moment.
+----------+------------+---------+
| id | id_problem | vote |
+----------+------------+---------+
| 1 | 1 | 25 |
| 2 | 1 | 10 |
| 3 | 2 | 18 |
| 4 | 2 | 2 |
| 5 | 3 | 6 |
| 6 | 3 | 7 |
| 7 | 4 | 11 |
| 8 | 4 | 4 |
| 9 | 5 | 5 |
| 10 | 5 | 2 |
+----------+------------+---------+
I would like to get this result:
(The row with the highest vote per id_problem)
+----------+------------+---------+
| id | id_problem | vote |
+----------+------------+---------+
| 1 | 1 | 25 |
| 3 | 2 | 18 |
| 6 | 3 | 7 |
| 7 | 4 | 11 |
| 9 | 5 | 5 |
+----------+------------+---------+
SELECT
id,
id_problem,
max(vote)
from
tablename
group by
id_problem
order by
id_problem ASC
The max(vote) determines greater vote, but it aggregate the result, then you need to group by id_problem and then order it asc.
You can use group by clause with max aggregation function to get the expected result, e.g.:
select id, id_problem, max(vote) as vote
from result
group by id_problem
order by id_problem
Here's SQL Fiddle.

how to fetch this query using 2 table

Hy I have 2 table
1.application
id | name | status
====================
1 | morvick | complete
2 | siti | prosess
3 | boby | complete`
2.application_test
id | application_id | test_id | result
======================================
1 | 1 | 1 | 70
2 | 1 | 2 | 80
3 | 1 | 3 | 90
4 | 2 | 1 | 60
5 | 2 | 2 | 80
6 | 2 | 3 | 70
7 | 3 | 1 | 90
8 | 3 | 2 | 70
9 | 3 | 3 | 60
10| 3 | 4 | 80
my Question is :
==================
1. how to find the maximum value at each test_id
2. how I can to get or total applicant_id where status complete
for example to be like this :
test_id | result_max | total_applicant_status(complete)
1 | 90 | 2
2 | 80 | 2
3 | 90 | 2
4 | 80 | 1
SELECT MAX(value) FROM table WHERE test_id = 1;
or perhaps SELECT value, test_id FROM table ORDER BY value DESC;
and for the next part, this may give what you want.
SELECT at.test_id, MAX(at.result), COUNT(IF(status='complete', 1, 0)) FROM application a LEFT JOIN application_test at ON a.id = at.application_id GROUP BY application_id;

How to copy mysql data from table one to table two [php]? [duplicate]

This question already has answers here:
How to copy data from one table to another new table in MySQL?
(13 answers)
Closed 9 years ago.
How to copy mysql data from table one to table two [php] ?
I want to copy data from table one to table two WHERE type=1 , How can I do ?
Table one
| type | number | name | date |
| 1 | 1 | a | 12 |
| 1 | 2 | b | 13 |
| 2 | 6 | c | 14 |
| 1 | 8 | x | 17 |
| 2 | 8 | e | 19 |
| 3 | 6 | f | 11 |
| 2 | 4 | h | 10 |
| 1 | 7 | i | 11 |
| 1 | 9 | p | 13 |
| 2 | 5 | r | 17 |
| 1 | 3 | t | 12 |
table two (out put)
| number | name |
| 1 | a |
| 2 | b |
| 8 | x |
| 7 | i |
| 9 | p |
| 3 | t |
This is the basic standard query for the MySQL.
INSERT INTO TARGET_TABLE SELECT * FROM SOURCE_TABLE WHERE Type = 1
Using MySQLi in PHP you can do like this..
$mysqli->query("INSERT INTO `TARGET_TABLE` SELECT * FROM `SOURCE_TABLE` WHERE `Type` = 1")
You can do this with one SQL statement:
insert into two(number, name)
select number, name
from one
where type = 1;

I need to query 1 table to obtain the total number of rows pertaining to a value in a second and third table

I have 3 Tables:
user_subscriptions
ID | SUB_ID | TYPE | Type 1 = scripts | Type 2 = packages
1 | 1 | 1 |
2 | 2 | 1 |
3 | 3 | 1 |
4 | 4 | 1 |
5 | 1 | 2 |
6 | 2 | 2 |
7 | 3 | 2 |
8 | 4 | 2 |
9 | 5 | 1 |
10 | 6 | 2 |
11 | 7 | 1 |
12 | 8 | 1 |
13 | 9 | 2 |
scripts
ID | AUTHOR_ID |
1 | 58 |
2 | 58 |
3 | 58 |
4 | 58 |
5 | 52 |
6 | 53 |
7 | 58 |
8 | 55 |
9 | 58 |
10 | 56 |
packages
ID | AUTHOR_ID |
1 | 58 |
2 | 58 |
3 | 58 |
4 | 58 |
5 | 56 |
6 | 57 |
7 | 58 |
8 | 58 |
9 | 56 |
10 | 58 |
In the 'user_subscriptions' table, the 'SUB_ID' column corresponds with a script ID or package ID, depending on the type that is set for that subscription.
My goal is to query for the total number of subscriptions to scripts & packages created by a particular author. Author 58 has created 6 Scripts and 7 Packages (total 13) but there are only 9 user subscriptions that are related to author_id 58.
I am having trouble writing a query that will return the user_subscriptions pertaining to author_id 58.
Any and all assistance is greatly appreciated. I am making this query from PHP and will likely use mysqli_num_rows in order to get the count that I need. Thanks again!
SELECT author_id
, COUNT(*) total
FROM
( SELECT id, author_id FROM scripts
UNION ALL
SELECT id, author_id FROM packages
) x
GROUP
BY author_id;
Note that this solution has fewer words in it than webnoobs comment above ;-)

PHP - I don't want to use table with loops anymore

I need to build one table that show all the sales for one day that i've choosen.
I've allways used the dummy way, selecting the table products that had a sale in one day, then do a loop on a php table do get the other informations from others tables.
I wanna know if theres is a simple way getting the data.
TABLES:
table_products
pr_id | pr_name | pr_price | ..
------|---------|----------|
1 | TV | 299.99 |
2 | RADIO | 59.99 |
3 | DVD | 49.99 |
.. | .. | .. |
table_sales
sa_id | sa_coupon | sa_day | sa_month | ..
------|-----------|----------|----------|
1 | 101 | 4 | 9 |
2 | 102 | 5 | 9 |
3 | 103 | 5 | 9 |
.. | .. | .. |.. |
80 | 211 | 2 | 12 |
.. | .. | .. |.. |
table_sales_dt
sa_dt_coupon | sa_dt_product_id | sa_qtd |
-------------|------------------------|-------------|..
101 | 1 | 10 |
101 | 2 | 5 |
102 | 1 | 5 |
103 | 3 | 8 |
211 | 1 | 15 |
211 | 2 | 10 |
211 | 3 | 5 |
.. | .. | .. |
i want to show the results like this:
Sales on month 9 (september)
N PRODUCT QUANTITY PRICE TOTAL
1 - TV - 15 - 299.99 - 4499.85
2 - RADIO - 5 - 59.99 - 299.95
3 - DVD - 8 - 49.99 - 399.92
THANKS!!!
Something like this might do it:
SELECT p.* FROM table_products p
LEFT JOIN table_sales_dt dt ON p.id = dt.product_id
LEFT JOIN table_sales s ON dt.sa_dt_coupon = s.sa_coupon
WHERE s.sa_month = 9

Categories