douzhanlai4671 2013-12-18 19:08
浏览 86
已采纳

显示MYSQL数据库中多个表的数据

I know there's something wrong here. Can anyone point it out? I am trying to display question_body from questions and answers_body from answers.

EDIT : I got this error: Column 'question_id' in field list is ambiguous

 <?php
        $auctionSurvey = "SELECT question_id, survey_id, question_body, answer_body FROM questions
                          INNER JOIN answers ON answers.question_id = questions.question_id
                          WHERE survey_id='1'";
        $aucResult = mysql_query($auctionSurvey) or die (mysql_error());

        while($auctionRow = mysql_fetch_assoc($aucResult)){
            echo $auctionRow['question_body']. $auctionRow['answer_body'];
        }
?>
  • 写回答

3条回答 默认 最新

  • dongtan5555 2013-12-18 19:13
    关注

    It looks like one problem is ambiguous column...

    In the SELECT list, I don't think MySQL can figure out which table is referenced by question_id.

    The normative pattern whenever two (or more) tables (row sources) are referenced is to QUALIFY every column reference...

    SELECT q.question_id
         , q.survey_id
         , q.question_body
         , a.answer_body
      FROM questions q
      JOIN answers a 
        ON a.question_id = q.question_id
     WHERE q.survey_id='1'
    

    There's two big benefits to this pattern (qualifying column references):

    Firstly, it helps future readers "know" which columns are from which table, without having to look at the table definitions and figure out which table has which column name.

    Secondly, it helps avoid "breaking" a SQL statement when a new column with the same name as a column in another table is added... having the column references qualified avoids the potential for breaking a SQL statement (causing a working SQL statement to start raising an ambiguous column exception.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探