2016-09-01 2 views
1

Я хочу дать оценку учащемуся, поэтому необходимо разделить на самые высокие оценки, достигнутые с помощью верхнего уровня, соответствующего предмета. один раз я хочу делить на наименьший нет тоже. Я не хочу делать несколько запросов., делясь с наибольшим значением из ячейки mysql

здесь таблица

id name  physics math 
1 Adam  68  80 
2 Mark  55  97 
3 Luis  78  44 
4 Philip  99  62 
5 James  59  89 

мой PHP код

$re99 = "SELECT * FROM `tableA` order by id desc limit 10"; 
while($row2 = $res2->fetch_assoc()){ 

$name=$row2['name']; 
$physics=$row2['physics']; 
$math=$row2['math']; 

$grade=$physics/99+$math/97; 

$poor='poor performer in math is mark with 55 marks and Luis with 44 marks'; 
} 

я не знаю, как получить эти 4 значения (99,97,55,44) из приведенных выше таблиц в одном запросе

ответ

0

насчет select *, max(physics) as high_physics, max(math) as high_math, min(physics) as min_physics, min(math) as min_math from tableA order by id desc limit 10;

Затем вы можете сделать что-то вроде $grade = $physics/$row2['high_physics'] + $math/$row2['high_math'];

+0

пытается. кажется ур мастер sql – tumbin

+0

вы хотите, чтобы у вас было 10 строк в вашем результате или только 2 беднейших –

+0

@BerndBuffen я хочу дать оценку всем, поэтому я хочу все 10 строк – tumbin

0

это то, что вы ищете. В качестве примера я добавил несколько столбцов. вы можете удалить их, если вы его не используете.

Запрос

SELECT 
    ta.* 
    , m.* 
    , ta.physics/m.max_physics as grade_physics 
    , ta.math/m.max_math as grade_math 
    , (ta.physics/m.max_physics) + (ta.math/m.max_math) as grade 
FROM tableA ta 
CROSS JOIN (SELECT max(math) as max_math, max(physics) as max_physics from 
      (SELECT * from tableA order by id LIMIT 10) as x) as m 
ORDER BY id 
LIMIT 10; 

Sample Таблица

mysql> select * from tableA; 
+----+--------+---------+------+ 
| id | name | physics | math | 
+----+--------+---------+------+ 
| 1 | Adam |  68 | 80 | 
| 2 | Mark |  55 | 97 | 
| 3 | Luis |  78 | 44 | 
| 4 | Philip |  99 | 62 | 
| 5 | James |  59 | 89 | 
+----+--------+---------+------+ 
5 rows in set (0,01 sec) 

Результат

mysql> SELECT 
    ->  ta.* 
    ->  , m.* 
    ->  , ta.physics/m.max_physics as grade_physics 
    ->  , ta.math/m.max_math as grade_math 
    ->  , (ta.physics/m.max_physics) + (ta.math/m.max_math) as grade 
    -> FROM tableA ta 
    -> CROSS JOIN (SELECT max(math) as max_math, max(physics) as max_physics from (SELECT * from tableA order by id LIMIT 10) as x) as m 
    -> ORDER BY id 
    -> LIMIT 10; 
+----+--------+---------+------+----------+-------------+---------------+------------+--------+ 
| id | name | physics | math | max_math | max_physics | grade_physics | grade_math | grade | 
+----+--------+---------+------+----------+-------------+---------------+------------+--------+ 
| 1 | Adam |  68 | 80 |  97 |   99 |  0.6869 |  0.8247 | 1.5116 | 
| 2 | Mark |  55 | 97 |  97 |   99 |  0.5556 |  1.0000 | 1.5556 | 
| 3 | Luis |  78 | 44 |  97 |   99 |  0.7879 |  0.4536 | 1.2415 | 
| 4 | Philip |  99 | 62 |  97 |   99 |  1.0000 |  0.6392 | 1.6392 | 
| 5 | James |  59 | 89 |  97 |   99 |  0.5960 |  0.9175 | 1.5135 | 
+----+--------+---------+------+----------+-------------+---------------+------------+--------+ 
5 rows in set (0,00 sec) 

mysql> 
Смежные вопросы