1 回答
TA貢獻1876條經驗 獲得超7個贊
我不是 SQL 專家,但我知道一點 PHP。也許這會有所幫助。得到兩個表。從第一個表中獲取第一行,作為默認值,并存儲它。然后循環第二個表并將第一個表中的每一行與第二個表中的行進行比較。當cat_id兩行中的a相同時,將存儲第二行。
將FIRSTTABLE和SECONDTABLE字符串更改為您自己的表名
<?php
// Query to collect data table 1.
// Change the FIRSTTABLE and SECONDTABLE to your table names.
$table_1_sql = "select * from FIRSTTABLE";
$table_2_sql = "select * from SECONDTABLE";
// Query both tables
$query_table_1 = $dbo->query( $table_1_sql );
$query_table_2 = $dbo->query( $table_2_sql );
// Set the initial values to false.
$selected_row_1 = false;
$selected_row_2 = false;
// Loop over the first table to get the first row.
foreach( $query_table_1 as $row_table_1 ) {
if ( $selected_row_1 === false ) {
$selected_row_1 = $row_table_1
}
}
// Loop over the second table to get the row that matches that cat_id from the first table row.
foreach( $query_table_2 as $row_table_2 ) {
if ( $selected_row_1 !== false ) {
if ( $selected_row_1[ 'cat_id' ] === $row_table_2[ 'cat_id' ] ) {
$selected_row_2 = $row_table_2;
}
}
}
// The values from the first and second row.
// Both rows with the same 'cat_id' value.
var_dump( $selected_row_1 ); // array( 'cat_id' => SOMENUMBER, 'catdesc' => 'SOMEDESCRIPTION' );
var_dump( $selected_row_2 ); // array( 'cat_id' => SOMENUMBER, 'sub_id' => SOMENUMBER, 'catdesc' => 'SOMEDESCRIPTION', 'name' => 'SOMENAME' );
?>
現在您應該擁有填充字段所需的數據。該$selected_row_2變量現在包含一個數組,其名稱是您的輸入字段所需的名稱。
免責聲明
這一切都是基于您提供的小信息,我希望它會幫助您。如果沒有,我會盡我所能帶你去你需要去的地方。
- 1 回答
- 0 關注
- 216 瀏覽
添加回答
舉報
