doxp30826 2016-01-22 13:11 采纳率: 0%
浏览 88

查询显示php mysql两个不同列的值

i have two tables with fields and values:

TABLE respostas

Id      Corp_resp            Id_perg     Id_quest 
12       0,2                 1             1
13       0,5                 2             1
14       0,7                 3             1
15       0,2                 4             1
16       0,3                 5             1
17       0,6                 6             1

Table menu

Id_perg         percentagemadministrador Id_quest
1                             0.5        1    
2                             0.4        1         
3                             0.3        1          
4                             0,2        1
5                             0,1        1
6                             0,8        1

I want to associate the id_quest to id_perg and multiply the corp_resp with percentagemadministrador of the last 10 rows of the table respostas

total=SUM(corp_resp*percentagemadministrador) of the last 10 rows of the table.

<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("teste") or die(mysql_error()); // selecciona a database do server escolhido

//something like that

$query ="SELECT SUM(menu.percentagemadministrador*respostas.corp_resp) as TOTAL 
         FROM respostas 
         INNER JOIN menu 
         WHERE respostas.id_perg=menu.id_perg 
         AND respostas.id_quest=menu.id_perg 
         LIMIT id_perg=10";

$resultado = mysql_query($query) or die(mysql_error());

if($row = mysql_fetch_array($resultado)){

    echo " <center> Final: </center> ".$row['TOTAL'];
} ?>

Can you help with that?

  • 写回答

1条回答 默认 最新

  • doufang2228 2016-01-22 13:27
    关注

    I think you should use a subselect for getting the 10 firts id only

    SELECT SUM(menu.percentagemadministrador*respostas.corp_resp) as TOTAL 
    FROM respostas INNER JOIN menu 
    WHERE respostas.id_perg=menu.id_perg 
    AND respostas.id_quest=menu.id_perg 
    AND id_perg in (select id_perg from respostas order by id_perg limit 10);
    

    if don't work in we can try with inner join

    SELECT SUM(menu.percentagemadministrador*respostas.corp_resp) as TOTAL 
    FROM respostas 
    INNER JOIN menu on ( respostas.id_perg=menu.id_perg 
         AND respostas.id_quest=menu.id_perg)  
    INNER JOIN  ( select id_perg from  respostas order by id_perg limit 10) as t2 
     on (respostas.id = t2. id)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog