我有两张桌子“BANDS”& “ADRES”。在表“BANDS”中有一个BOEKERID,其ID与“adres”中的相应ID相同。
乐队总是不同的,而且BOEKER可以是相同的。另外,由于地址变化,我有2个不同的表格。
现在,我试着这个来获取信息:
$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>';
有人可以帮助我吗?
答案 0 :(得分:0)
尝试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
也不要尝试使用mysql,因为它已被弃用,而是尝试切换到MySQLi或PDO
答案 1 :(得分:0)
如果我使用你的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>';
}
加入(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>';
}
答案 2 :(得分:0)
我想你需要做这样的事情:
$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>';
如果我错了,请纠正我