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)
    
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?