簡體   English   中英

將數據從表中提取到另一個表

[英]Fetch data from a table to another

我在SQL中有2個表,第一個名為cars

`cars` (`car_id` int(100) NOT NULL,`car_first_registration` text NOT NULL, `car_brand` int(11) NOT NULL, `car_profile_image` text NOT NULL, `car_cat` int(11) NOT NULL, `car_price` decimal(10,0) NOT NULL, `car_vin` char(20) NOT NULL, `car_mileage` char(20) NOT NULL, `car_seats` int(11) NOT NULL, `car_gearbox` text NOT NULL, `car_ext_color` char(30) NOT NULL, `car_int_color` char(20) NOT NULL, `car_desc` text NOT NULL, `car_stock` varchar(255) NOT NULL, `car_keywords` text NOT NULL, `car_visibility` tinyint(1) NOT NULL, `car_ref` char(8) NOT NULL ) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=latin1
`car_brands` (`brand_id` int(100) NOT NULL,`brand_title` text NOT NULL) ENGINE=InnoDB AUTO_INCREMENT=40 DEFAULT CHARSET=latin1;

在汽車表中我可以插入具有適合列的許多信息的汽車,但我有一個問題,汽車表中的car_brand存儲為一個值,這個值取自car_brands:預裝品牌,所以當我插入一輛車我選擇來自car_brands中的品牌,而car_brands有2列id和title,但問題是當我想要獲取汽車時car_brand存儲為等於car_brands中ID的數字例如我有一輛車是誰的品牌是寶馬並在car_brands表中存儲為ID = 4,因此它將在car_brand的汽車表中存儲為值4。 我希望在GET方法上有這個值,將產品顯示為文本(寶馬)NOT 4這是我的汽車顯示代碼,請告訴我要更正的內容,這樣我就可以顯示名稱而不是ID

注意:不要在car_brands和car_brand之間混淆,car_brands是一個表,car_brand是汽車表中的一列

真的非常感謝:)):) :)!

<?php
function getCars(){

if(!isset($_GET['car_categories'])){
 if(!isset($_GET['car_brands'])){

   global $con;

   $car_visibility = isset($_POST['car_visibility']);       
   $get_pro = "select * from cars where car_visibility= true";    

   $run_pro = mysqli_query($con, $get_pro)
or die("Error: ".mysqli_error($con));

   $i = 0;
   while($row_pro=mysqli_fetch_array($run_pro)){
            $i++;
            $car_id = $row_pro['car_id'];
            $car_first_registration = $row_pro['car_first_registration'];
            $car_brand = $row_pro['car_brand'];
            $car_profile_image = $row_pro['car_profile_image'];
            $car_cat = $row_pro ['car_cat'] ;
            $car_price = $row_pro['car_price'];
            $car_vin = $row_pro['car_vin'];
            $car_mileage = $row_pro['car_mileage'];
            $car_seats = $row_pro['car_seats'];
            $car_gearbox = $row_pro['car_gearbox'];
            $car_ext_color = $row_pro['car_ext_color'];
            $car_int_color = $row_pro['car_int_color'];
            $car_desc = $row_pro['car_desc'];
            $car_stock = $row_pro['car_stock'];
            $car_keywords = $row_pro['car_keywords'];
            $car_visibility = $row_pro['car_visibility'];
            $car_ref = $row_pro['car_ref'];    

$get_brands ="SELECT * FROM car_brands";    

$fetch_brands = "SELECT cars.car_id , cars.car_first_registration , cars.car_brand, cars.car_profile_image, cars.car_cat , cars.car_price , cars.car_vin,  cars.car_mileage , cars.car_seats , cars.car_gearbox , cars.car_ext_color, cars.car_int_color, cars.car_desc , cars.car_desc , cars.car_stock , cars.car_keywords , cars.car_visibility , cars.car_ref , car_brands.brand_id
FROM cars         
INNER JOIN  car_brands    
ON cars.car_brand=car_brands.brand_id";    

echo "<div class='single_product'>";
    echo    "<h1><a href='details.php?car_id=$car_id' id='product_title'>$car_brand . $car_cat . $car_first_registration</a></h1>";

    echo    "<img src='admin_area/Car Profiles/$car_profile_image'   />    
             <h2>$ $car_price</h2>                      
            </div>";        
     }
     }
     }
  }
 ?>
<?php getCars();?>

你已經知道如何加入。 所以,簡單一點,用一個查詢做你想做的一切。

將第一個查詢更改為:

SELECT * FROM cars         
INNER JOIN car_brands    
ON cars.car_brand=car_brands.brand_id AND cars.car_visibility = 1

您不需要任何查詢。

無論您需要顯示汽車的品牌名稱,都可以使用$ row_pro ['brand_title']。 所以,你可以改變這個:

$car_brand = $row_pro['car_brand'];

對此:

$car_brand_id = $row_pro['car_brand'];
$car_brand_name = $row_pro['brand_title'];

順便說一下,我建議你閱讀關系和索引,以建立更好的DataBase結構。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM