php – 如何从mysql中的2个数据库表中选择数据…?
作者:互联网
我的数据库中有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行数据.
(我是PHP / MySql的初学者)
解决方法:
您可以改为连接表以获取所需的行,而不是两次查询数据库.尝试在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
标签:php,sql,mysql,multi-select 来源: https://codeday.me/bug/20190901/1780553.html