我的数据库中有2个日期表,student_info和student_payment …
在student_info我有:
id,student_id,student_mail,student_pass,student_name,…
并且在student_payment中有:
ID,student_id数据,student_payment_id,student_payment_date,…
所以我的问题在这里,我想选择student_name,其中student_id形成student_info,但我有问题,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中使用,因为我有2行数据.
解决方法:
您可以改为连接表以获取所需的行,而不是两次查询数据库.尝试在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
要进一步了解联接,请访问以下链接:
> Visual Representation of SQL Joins
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。