1 回答

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超7個(gè)贊
我不是 SQL 專家,但我知道一點(diǎn) PHP。也許這會(huì)有所幫助。得到兩個(gè)表。從第一個(gè)表中獲取第一行,作為默認(rèn)值,并存儲(chǔ)它。然后循環(huán)第二個(gè)表并將第一個(gè)表中的每一行與第二個(gè)表中的行進(jìn)行比較。當(dāng)cat_id兩行中的a相同時(shí),將存儲(chǔ)第二行。
將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' );
?>
現(xiàn)在您應(yīng)該擁有填充字段所需的數(shù)據(jù)。該$selected_row_2變量現(xiàn)在包含一個(gè)數(shù)組,其名稱是您的輸入字段所需的名稱。
免責(zé)聲明
這一切都是基于您提供的小信息,我希望它會(huì)幫助您。如果沒有,我會(huì)盡我所能帶你去你需要去的地方。
- 1 回答
- 0 關(guān)注
- 199 瀏覽
添加回答
舉報(bào)