简体   繁体   English

从一个表中选择匹配值到另一个表中的多个值

[英]selecting matching value from one table to multiple values in another

    cars                 models
car_id  car       car_id   model_id  model
   1    ford         1         1      mustang
   2    fiat         1         2      focus
   3    toyota       1         3      escort
                     2         4      500
                     2         5      spider
                     3         6      tacoma

The two tables I have are much more complicated so I took this code from another users question, It is almost what I want, but I don't know how to get the output to format with PHP correctly 我拥有的两个表要复杂得多,所以我从另一个用户的问题中获取了此代码,这几乎是我想要的,但是我不知道如何正确使用PHP格式化输出

SELECT c.Car, m.Model_id, m.Model
FROM models m
INNER JOIN car c ON c.Car_id = m.Car_id
WHERE m.Car_id = (SELECT Car_id FROM models WHERE Model = 'Escort');

$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {

$model = $row['Model'];
$vehicle = $row['Car'];

    ford mustang
    ford focus
    ford escort

What I am trying to get is 我想要得到的是

    ford
        mustang
        focus
        escort

I have posted my updated attempt below, which works, but I bet you fine people could make it prettier. 我已经在下面发布了我的最新尝试,该方法有效,但是我敢打赌,好的人可以使其更漂亮。

$sql = "
SELECT c.Car, m.Model_id, m.Model 
FROM models m 
INNER JOIN car c   
ON     c.Car_id = m.Car_id
WHERE m.Car_id = '1' ";

$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)) {
$vehicle[$row['Car']][] = $row['Model'];
}

echo "<table>";
foreach( $vehicle as $value => $key )
{echo '<tr><td>'.$value.'</td></tr>'; 
foreach( $key as $mod) 
{echo '<tr><td>'.$mod.'</td></tr>';}}

}

Results in: 结果是:
ford
mustang 野马
focus 焦点
escort 护送

You probably want an array with the key as the car type. 您可能需要一个以键作为汽车类型的数组。 Loop you're results and append to an array as necessary. 循环得到结果,并根据需要追加到数组。

Ex: 例如:

vehicle=[];
while($row = mysqli_fetch_assoc($result)) {
    if (!array_key_exists($row['Car'], $vehicle)){
        $vehicle[$row['Car']] = [];
    }
    $vehicle[$row['Car']][] = $row['Model'];
}

Output will look like this: 输出将如下所示:

array(1) {
  ["Ford"]=>
  array(2) {
    [0]=>
    string(4) "focus",
    [1]=>
    string(6) "escort"
  }
}
<?php

$mo1 = "";

$sql = "SELECT c.Car, m.Model_id, m.Model FROM models m INNER JOIN car c ON c.Car_id = m.Car_id
WHERE m.Car_id = (SELECT Car_id FROM models WHERE Model = 'Escort')";

$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
    $model = $row['Model'];
    $vehicle = $row['Car'];

 if($mo1 != $mo){$data.="<tr>
                            <td>$model</td>
                        </tr>";
                }

$data.="<tr>
            <td>$vehicle</td>
           </tr>";

$mo1=$mo;

}

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM