1
我必須根據單個鍵值從多個表中選擇數據。我有一個名爲maintable
的表,我將從中獲取所有ID,並且我在另一個數據庫中有另外10個表,其中maintable.id
作爲外鍵。現在我必須從一張表中的maintable.id
匹配的10個表中檢索數據。基於單個id獲取多個表中的所有數據
我已經嘗試的代碼是:
$sql = select id from maintable;
$runsql = mysql_query($sql);
while($sqlRow = mysql_fetch_array($runsql ,MYSQL_ASSOC)) {
for($i=1;$i<=10(This is another table count);$i++) {
$servSql = "select * from table.$i where ref_id = ".$sqlRow['id'];
$runServerSql = mysql_query($servSql);
while($serverRow = mysql_fetch_array($runServSql,MYSQL_ASSOC)) {
}
}
}
絕對是你想要的不是像這樣的嵌套循環。請參考mysql連接語法 – e4c5
使用[Joins](http://www.tutorialspoint.com/mysql/mysql-using-joins.htm)我的朋友。 –
嗨@ e4c5請你可以建議我舉例,因爲我是新來的 – shaan