简体   繁体   English

SQL内部联接获取列中的所有数据

[英]SQL inner join getting all data in column

This is my first time using an inner join so i'm very confused. 这是我第一次使用内部联接,所以我很困惑。

I have two tables. 我有两张桌子。

第一个:(我已将详细信息舍去)

This is my first table called members 这是我的第一张桌子,叫做members

在此处输入图片说明

This is my other table called donations. 这是我的另一张称为捐款表。 The userID from the members is linked up with the userID on the donations table. 来自成员的userID与捐赠表上的userID链接在一起。

Right so what i'm trying to do is select all of the data from members and from the donations table and assiotate each Id with the donation amount. 正确,所以我想做的是从成员和捐赠表中选择所有数据,并用捐赠金额来辅助每个ID。 So what i'm trying to do is echo all of the names along side their donation amount if that makes sense. 因此,我想做的是在所有捐赠名称旁边附上所有捐赠名称,如果可以的话。

This is my code at the moment 这是我目前的代码

$connect - contains my config $ connect-包含我的配置

  //Connection info.
  global $connect;


  //inner join
  $sql = "SELECT members.firstname, members.lastname 
  FROM members INNER JOIN   donations ON members.userID = donations.userID WHERE donations.amount !='' ORDER BY members.userID ASC ";

  $result = mysqli_query( $connect, $sql);


  while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){

    $list .= $row["firstname"];
    echo $list;
}

I'm getting this error back: mysqli_fetch_array() expects parameter 1 to be mysqli_result boolean 我收到此错误:mysqli_fetch_array()期望参数1为mysqli_result布尔值

UPDATE: Thanks for all your help, i'm running the SQL query and just getting the first and last name back. 更新:感谢您的所有帮助,我正在运行SQL查询,只是获得了名字和姓氏。

  SELECT members.firstname, members.lastname FROM members INNER JOIN   donations ON members.userID = donations.userID WHERE donations.amount !='' ORDER BY members.userID ASC

I think i'm doing something wrong here !='' the donation amount is a decimal am i targeting it right? 我认为我在这里做错了!=''捐款金额是十进制,我的目标是对吗?

You have typo in column name 'fisrname' => 'firstname'. 您在列名'fisrname'=>'firstname'中有错字。 Check the query first in phpmyadmin or the other tool. 首先在phpmyadmin或其他工具中检查查询。 Also read about mysqli_error and later about other means of accessing the DB (like PDO, Doctrine etc.). 另请阅读有关mysqli_error的信息,以及以后有关访问数据库的其他方法(如PDO,Doctrine等)的信息。

Replace members.id by members.userID in your query. 在查询中用members.userID替换members.id

SELECT members.firstname, members.lastname, donations.amount 
FROM members
INNER JOIN donations ON members.userID = donations.userID
WHERE donations.amount != ''
ORDER BY members.userID ASC

As for the SQL error, it's because $result is false when there's a problem with the query or no result has been found. 至于SQL错误,这是因为当查询有问题或未找到结果时,$ result为false

mysqli_fetch_array() expects parameter 1 to be mysqli_result boolean mysqli_fetch_array()期望参数1为mysqli_result布尔值

To prevent the error, add a simple if case because your while. 为防止错误,请添加一个if情况,因为您需要一段时间。

if($result){
    while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
       $list .= $row["firstname"];
       echo $list;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM