how to select two columns from two tables in mysql

后端 未结 3 1578
长情又很酷
长情又很酷 2021-01-16 23:44

I am trying to execute this query but i got error \" Undefined index: lname\".I want to count row from one column(fname) from table a and select column(ln

相关标签:
3条回答
  • 2021-01-17 00:10

    Try this code:

    $result = mysql_query("SELECT COUNT(a.fname) as fname,b.lname as lname FROM a,b");
    while ($row = mysql_fetch_array($result))
    {
      echo "<tr><td>";
      echo $row['lname'];
      echo "</td>";
      echo "<td>";
      echo $row['COUNT(fname)'];
      echo "</td></tr>";
    }
    
    0 讨论(0)
  • 2021-01-17 00:15

    You need to use an alias. Use this:

    $result = mysql_query("SELECT COUNT(fname) AS countfname,lname FROM a,b");
    while ($row = mysql_fetch_array($result))
    {
      echo "<tr><td>";
      echo $row['lname'];
      echo "</td>";
      echo "<td>";
      echo $row['countfname'];
      echo "</td></tr>";
    }
    
    0 讨论(0)
  • 2021-01-17 00:21

    If you still get an error you can try to fetch both separately:

    $result = mysql_query("SELECT COUNT(fname) FROM a");
    while ($row = mysql_fetch_array($result))
      {
        echo "<tr><td>";
        echo $row['COUNT(fname)'];
        echo "</td></tr>";
    
      }
    
    $result1 = mysql_query("SELECT lname FROM b");
    while ($row = mysql_fetch_array($result1))
        {
          echo "<tr><td>";
          echo $row['lname'];
          echo "</td></tr>";
       }
    
    0 讨论(0)
提交回复
热议问题