0

i am having trouble in writing SQL query

I have a database mysql CRICKET:

id  cricketer run cricketer2 run2 cricketer3 run3
9    dhoni    100   ashwin    50    raina     25
10    ABD     100   gayle     99    virat     15

Using the above database i have to take cricketers and add n=3;

$sql = "select (cricketer+cricketer2+cricketer3) as crick, 
               (run+run2+run3/n) as run 
        from CRICKET 
        WHERE id ='9' "
mysql_query($sql) or die(mysql_error());

I need the data look like this after using select query

crick  run
dhoni  58
ashwin 58
raina  58*

can anyone adjust the query my above query is not working

4
  • can you tell me, run showing in the field is avg. value ? and You may need to display like this ? crick run dhoni 58 ashiwn 58 raina 58 ABD 71.33 gayle 71.33 virat 71.33 Am I right ? Commented Jan 28, 2014 at 6:42
  • @sleepy , you should explain what you are trying to achieve. Not just the final outcome Commented Jan 28, 2014 at 6:53
  • oh sorry i am doing one project and i cant reveal the secret so i will post only the snippet @srinath Commented Jan 28, 2014 at 7:42
  • @miqdad all the run must add i.e run+run2+run3 and divide by 3 dynamically it must get the avg value Commented Jan 28, 2014 at 7:43

2 Answers 2

2
SELECT `cricheter`, `cricketer2`, `cricketer3`, 
         ROUND ( (`run` + `run2` + `run3`)/3 ) AS result
                FROM `run` WHERE `id` = 9;

This will return:

 cricheter   cricketer2   cricketer3   result
   dhoni       ashwin       raina        58

If that's what you want...

P.S. The structure of the table can and should probably be improved.

Sign up to request clarification or add additional context in comments.

Comments

0

Here is the code

<?php
    $sql = "SELECT
            *
        FROM
            CRICKET
        WHERE
            id = 9
    ";
    $result = mysql_query($sql) or die(mysql_error());
    $row = mysql_fetch_array($resul,MYSQL_ASSOC);
    $avg_run = ($row['run'] + $row['run2'] + $row['run3'])/3
?>
<table>
    <tr>
        <td>Crick</td>
        <td>Run</td>
    </tr>
    <tr>
        <td><?php echo $row['cricketer']; ?></td>
        <td><?php echo $avg_run; ?></td>
    </tr>
    <tr>
        <td><?php echo $row['cricketer2']; ?></td>
        <td><?php echo $avg_run; ?></td>
    </tr>
    <tr>
        <td><?php echo $row['cricketer3']; ?></td>
        <td><?php echo $avg_run; ?></td>
    </tr>
</table>

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.