如何在MySQL中从两个数据库表中选择数据?

3
我有两张数据表,student_infostudent_payment,在我的数据库中...

student_info 表中有:

id、student_id、student_mail、student_pass、student_name 等字段...

student_payment 表中有:

id、student_id、student_payment_id、student_payment_date 等字段...

所以我的问题是,在从 student_info 中选择 student_id 的情况下,我想要选择 student_name,但我遇到了问题,MySQL 给出了错误提示:

$db->connect();

$sql = "SELECT * FROM `student_payment`";
$rows = $db->fetch_all_array($sql);
$student_id = $rows['student_id'];

$sql2 = "SELECT * FROM `student_info` WHERE student_id=$student_id";
$rows2 = $db->fetch_all_array($sql2);

$db->close();

foreach($rows as $record ){ 
        // i wanna to use student_name in first line 
    echo "\n<tr>
            <td>$record[student_id]</td> 
            <td dir=\"ltr\">$record[student_payment]</td>
            <td dir=\"ltr\">$record[student_payment_id]</td>
            <td dir=\"ltr\">$record[student_payment_bank]</td>
            <td dir=\"ltr\">$record[student_payment_type]</td>
            <td dir=\"ltr\">$record[student_payment_date]</td>
            <td dir=\"ltr\"></td>
            </tr>\n"; 
}

但是我不知道如何连接student_id和student_name并在foreach中使用,因为我有两行数据。(我是PHP/MySQL的初学者)

$student_id = $rows['student_id']; 将会给你数据集中最后一条记录的ID。 - asprin
8个回答

1

可以使用INNER JOIN来修复它,您可以连接2个表并在1个查询中使用两个值。

http://www.w3schools.com/sql/sql_join_inner.asp

或者您可以使用面向对象编程的方式,不确定这是否符合您的需求。从两个查询中创建2个对象,并将它们放入一个foreach循环中。

1
尝试这个。
  $sql2 = "SELECT * FROM `student_info` WHERE student_id IN ($student_id)";

这是与当前语句相同的代码:$sql2 = "SELECT * FROM \student_info` WHERE student_id=$student_id";` - John Woo
我做了那个,但是我有两行数据,不知道怎样一起在 foreach 中使用。 - Mohsen Rasouli

1

不要两次查询数据库,可以使用联接表的方式获取所需行。尝试在PhpMyAdmin或直接在MySQL浏览器中执行以下查询。

SELECT  a.*, b.*
FROM    student_info a
        INNER JOIN student_payment b
            ON a.student_ID  = b.student_ID
-- WHERE ...if you have extra conditions...
ORDER   BY b.student_payment_date DESC

为了更深入地了解关联操作,请访问下面的链接:


1
尝试这个。
 $sql2 = " SELECT * FROM `student_info` WHERE student_id= '$student_id'   ";

1
foreach($rows as $record ){ 
    // i wanna to use student_name in first line 
echo "\n<tr>
        <td>$record[student_id]</td> 
        <td dir=\"ltr\">".$record['student_payment']."</td>
        <td dir=\"ltr\">".$record['student_payment_id']."</td>
        <td dir=\"ltr\">".$record['student_payment_bank']."</td>
        <td dir=\"ltr\">".$record['student_payment_type']."</td>
        <td dir=\"ltr\">".$record['student_payment_date']."</td>
        <td dir=\"ltr\"></td>
        </tr>\n"; 
}

0

使用Mahmoud Gamal的代码

但是始终只选择所需的列,而不是全部列,因为

将来表中的列数可能会增加,这可能会降低应用程序的性能。 此外,它可能包含一些重要信息,不应泄漏。


0
尝试将表连接并使用单个查询代替两个 - $sql =“SELECT * FROM student_info,student_payment WHERE student_info.student_id = student_payment.student_id”

0

看起来你想要一个所有付款的报告。学生姓名应与付款信息一起显示。结果可能会有一个学生有多个付款记录。

这个结果按照学生姓名和付款日期排序(最近的在前)。

SELECT s.student_name, sp.*
  FROM student_payment sp
  INNER JOIN student_info s ON s.student_ID=sp.student_ID
  ORDER BY s.student_name ASC, sp.student_payment_date DESC

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接