我想在查询中连接2个表来显示结果
我在第一张表中有lastname
,firstname
等
在第二个表格中,我有training
,date
...
我在第二个表格中还有一个ID
引用,它将姓氏和名字与培训和日期相关联。
因此,每当我在数据库中创建培训记录时,它都会为同一个人使用相同的ID
我希望能够在查询中连接两个表,并在搜索lastname
时显示结果
如果我在搜索结果中点击该名称,它还会列出他们已完成的所有培训。
我是MySQL数据库的新手
我正在使用phpmyadmin for MySQL并运行localhost数据库进行测试......
我目前可以搜索单个表并显示结果,但我无法找出联接以链接它们并将名称列表作为显示那里训练的链接。并且让它在顶部保留打印输出的名称......这是我从教程中获得的代码。
<form>
<div align="right">
<input type="button" value="Print Results" onClick="myprint()">
</div>
</form>
<?php
// TAKE THE INFORMATION FROM FORM.
$search = $_GET['search'];
// IF THERE IS NOT A KEYWORD GIVE AN ERROR
if (!$search)
echo "You didn't enter a keyword";
else
{
echo "<td>You searched for: <strong>$search </strong></td>";
mysql_connect('localhost','loginid','password');
mysql_select_db('trainingrecords');
$id=@$_GET['id'];
//QUERY IS THE CODE WHICH WILL MAKE THE SEARCH IN YOUR DATABASE.
//I WROTE AN EXPLANATION ABOUT IT AFTER THE CODE.
$query="SELECT * FROM username
WHERE MATCH( lastname, firstname, location, created)
AGAINST('%$search%' IN BOOLEAN MODE)";
$result1 = MySQL_query($query);
if(!$result1) {
echo MySQL_error()."<br>$query<br>";
}
if (MySQL_num_rows($result1) > 0) {
echo "<table width='750' align='center' border='1'
cellspacing='2' cellpadding='2'>";
while($result2 = MySQL_fetch_array($result1)) {
//A short description from category.
$description = $result2['location'];
$searchPosition = strpos($description, $search);
$shortDescription = substr($description, $searchPosition, 150);
echo "<td><strong>{$result2['lastname']}
{$result2['firstname']}</td></strong><td>
$shortDescription</td><td>{$result2['created']}</td><tr/>";
}
echo "</table>";
} else {
echo "No Results were found in this category.<br>";
}
echo "<br>";
}
?>
答案 0 :(得分:1)
我想你想要这样的东西:
SELECT * FROM username, training
where username.lastname = $lastname and username.firstname = $firstname
and username.id = training.id;
要求您将$lastname
和$firstname
值作为单独的变量传递。
答案 1 :(得分:1)
它不是你想要的联盟
Select a.id,a.firstname,a.lastname,a.location,a.date,b.trainingdate
from username a
inner join
training b on a.id=b.id
在最后添加你的where子句
答案 2 :(得分:1)
您需要的是JOIN
$query="SELECT * FROM username u LEFT JOIN training t on (t.userid = u.id) WHERE MATCH( lastname, firstname, location, created) AGAINST('%$search%' IN BOOLEAN MODE)";
答案 3 :(得分:1)
1-您有一个SQL注入孔
确保使用mysql_real_escape_string
转换所有变量
并利用在查询_中正确引用字符串_(否则mysql_real_escape_string将无效)_
$search = mysql_real_escape_string($GET['search']);
....
$query="SELECT * FROM username
WHERE MATCH( lastname, firstname, location, created)
AGAINST('%".$search."%' IN BOOLEAN MODE)";
// ^ ^ quotes were already there, good!
2-如果您想从多个表中获取结果,则必须进行加入
$search = "SELECT u.*, t.* FROM username u
INNER JOIN training t ON (u.id = t.user_id)
WHERE MATCH( lastname, firstname, location, created)
AGAINST('%".$search."%' IN BOOLEAN MODE)";
通常您不想选择所有字段。如果您明确说明要向用户显示的字段,查询将运行得更快。
这是关于php和MySQL的一个不错的教程:http://www.tizag.com/mysqlTutorial/
如果你有SQL注入漏洞会发生什么:How does the SQL injection from the "Bobby Tables" XKCD comic work?
答案 4 :(得分:0)
-- something basic like this?
SELECT u.id, u.name, t.link
FROM users u
LEFT JOIN trainings t ON u.id = t.user_id
WHERE u.id = 5
答案 5 :(得分:0)
也许你想要这样的东西:
SELECT username.* FROM username
JOIN training ON (training.id = username.id)
WHERE MATCH( lastname, firstname, location, created)
AGAINST('%$search%' IN BOOLEAN MODE)