[英]After query from one table get more info from other table and join result
I have two tables "BANDS" & "adres". 我有两个表“ BANDS”和“ adres”。 In table "BANDS" there is a BOEKERID which is the same ID as the corresponding ID in "adres". 在表“ BANDS”中,有一个BOEKERID,该ID与“ adres”中相应的ID相同。
The bands is always different ofcourse and the BOEKER can be the same. 乐队的路线总是不同的,BOEKER可以相同。 Also because of adress change i have the 2 dfferent tables. 另外由于地址的更改,我有2张不同的桌子。
Now, i tried this to get the info: 现在,我尝试这样做以获得信息:
$sql1 = "SELECT BANDID, NAAMBAND, CONTACTBAND, BOEKERID FROM `BANDS` ORDER BY $field $sort";
$sql2 = "SELECT ID, NAAM FROM `adres` WHERE ID = $BOEKERID";
$result1 = mysql_query($sql1) or die(mysql_error());
$result2 = mysql_query($sql2) or die(mysql_error());
while($row1 = mysql_fetch_array($result1))
while($row2 = mysql_fetch_array($result2))
echo'<tr>
<td>'.$row1['BANDID'].'</td>
<td>'.$row1['NAAMBAND'].'</td>
<td>'.$row1['CONTACTBAND'].'</td>
<td>'.$row1['BOEKERID'].'</td>
<td>'.$row2['NAAM'].'</td>';
Somebody can help me ? 有人可以帮助我吗?
Try Sql join, 尝试Sql join,
SELECT
b.BANDID, b.NAAMBAND, b.CONTACTBAND, b.BOEKERID, a.id, a.naam
FROM BANDS b
LEFT JOIN adres a ON b.boekerid=a.id
ORDER BY $field $sort
Also don't try to use mysql since it's deprecated, instead try switching to MySQLi or PDO 也不要尝试使用已弃用的mysql,而是尝试切换到MySQLi或PDO
If i use your sql : 如果我使用您的sql:
$sql1 = "SELECT BANDID, NAAMBAND, CONTACTBAND, BOEKERID FROM `BANDS` ORDER BY $field $sort";
$result1 = mysql_query($sql1) or die(mysql_error());
while($row1 = mysql_fetch_array($result1)){
echo'<tr>
<td>'.$row1['BANDID'].'</td>
<td>'.$row1['NAAMBAND'].'</td>
<td>'.$row1['CONTACTBAND'].'</td>
<td>'.$row1['BOEKERID'].'</td>';
$sql2 = "SELECT ID, NAAM FROM `adres` WHERE ID = $row1['BOEKERID']";
$result2 = mysql_query($sql2) or die(mysql_error());
while($row2 = mysql_fetch_array($result2)){//change this while if $sql2 all time return only 1 result
echo '<td>'.$row2['NAAM'].'</td>';
}
echo'</tr>';
}
With join ( http://sql.sh/cours/jointures/inner-join ): 使用join( http://sql.sh/cours/jointures/inner-join ):
$sql1 = "SELECT NAAM,BANDID, NAAMBAND, CONTACTBAND, BOEKERID FROM `BANDS` INNER JOIN `adres` WHERE `BANDS`.BOEKERID = `adres`.id ORDER BY $field $sort";
$result1 = mysql_query($sql1) or die(mysql_error());
while($row1 = mysql_fetch_array($result1)){
echo'<tr>
<td>'.$row1['BANDID'].'</td>
<td>'.$row1['NAAMBAND'].'</td>
<td>'.$row1['CONTACTBAND'].'</td>
<td>'.$row1['BOEKERID'].'</td>
<td>'.$row1['NAAM'].'</td>
</tr>';
}
i guess you need to do something like this: 我想你需要做这样的事情:
$sql1 = "SELECT a.BANDID, a.NAAMBAND, a.CONTACTBAND, a.BOEKERID, b.NAAM FROM BANDS a, adres b WHERE a.BANDID = b.ID AND ID = $BOEKERID ORDER BY $field $sort";
$result1 = mysql_query($sql1) or die(mysql_error());
while($row1 = mysql_fetch_array($result1))
echo'<tr>
<td>'.$row1['BANDID'].'</td>
<td>'.$row1['NAAMBAND'].'</td>
<td>'.$row1['CONTACTBAND'].'</td>
<td>'.$row1['BOEKERID'].'</td>
<td>'.$row1['NAAM'].'</td>';
Please correct me if im wrong 如果我错了请指正我
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.