我有两张表都有' name'作为专栏。我认为我的搜索是成功的,但是如何在数组中作为索引来区分这两者?
SQL查询:
$result = $pdo->prepare('SELECT * FROM `profs`
INNER JOIN `universities`
ON `profs`.`school_id` = `universities` . `id`
AND `profs` . $searchtype LIKE :query');
$result->bindValue(':query', "%" . $_GET['query'] . "%");
$result->execute();
我注意到我可以使用句点来指定哪个表(比如教授.school_id或大学.id)
我尝试过类似的东西在表格中显示结果:
foreach ($result as $record) {
echo "<tr><td>", '<a href="individual_sample.php?id=', $record['id'], '">', $record['name'], '</a></td>';
echo "<td>", $record['universities.name'], "</td>";
echo "<td>", $record['subject'], "</td>";
echo "<td>", $record['latitude'], ",", $record['longitude'], " </td>";
echo "<td>", $record['avg_rating'], "</td></tr>";
}
echo '</table>
我尝试了$ record [&#39; Universities.name&#39;]但我收到以下错误:
SQLSTATE [42S22]:未找到列:1054未知列&#39; profs.school&#39;在&#39; on子句&#39;您的搜索没有返回任何结果
答案 0 :(得分:2)
尝试这样的事情:
SELECT profs.*, universities.name as university_name FROM `profs`
INNER JOIN `universities` ON `profs`.`school_id` = `universities` . `id`
AND `profs` . $searchtype LIKE :query
然后像
一样访问它$record['university_name']
虽然
$searchtype
不应该是“学校”,因为它不是“教授”中的一个领域