0

我试图根据它收到的规格表值在“规格”列表中显示表格的所有“汽车”。

表:

汽车:

dcar: 1 / width:30 / length:10 / weight:300
idcar: 2 / width:20 / length:12 / weight:210
idcar: 3 / width:20 / length:21 / weight:230
idcar: 4 / width:40 / length:11 / weight:210

规格表:

idspec:1 spec:width
idspec:2 spec:weight
idspec:3 spec:length

输出将是:

Width
car-1 width-30
car-2 width-20
car-3 width-20
car-4 width-40


Weight
car-1 weight-300
car-2 weight-210
car-3 weight-230
car-4 weight-210

Length
car-1 length-10
car-2 length-12
car-3 length-21
car-4 length-11

我的查询可以吗?我想在一段时间内使用它或 foreach (php)

$resultjoin = mysqli_query($connecDB,"SELECT specs.idspec, specs.spec, cars.idcar FROM (specs) LEFT JOIN cars ON cars.idcar = specs.idspec");

谢谢!

4

6 回答 6

4

尝试这个

你的桌子设计有错误。在规格和汽车之间还有另一张桌子。例如 car_apec_value 将有 carid, specid, value。

通过加入这三个表,您将获得您想要的结果,例如

$resultjoin = mysqli_query($connecDB,"SELECT 'cars'.'idcar', 'specs'.'spec' FROM cars LEFT JOIN car_apec_value ON cars'.idcar = 'car_apec_value'.'carid' where specs.specid = car_apec_value.specid AND specs.specid='1'");
于 2013-07-04T14:51:41.490 回答
2

我看起来你正试图取消你的表。如果是这样,您的查询就有点偏离了。

SELECT c.idcar, 'width' as which, width as value
FROM cars c
union all
SELECT c.idcar, 'length' as which, length as value
FROM cars c
union all
SELECT c.idcar, 'weight' as which, weight as value
FROM cars c
于 2013-07-04T14:48:58.847 回答
1

因此,据我了解,无论如何您都需要请求选择所有汽车。但选定的数据字段取决于您提供的“idspec”。我的意思是如果你给 idspec=2,你想得到:

car-1 weight-300
car-2 weight-210
car-3 weight-230
car-4 weight-210

在这种情况下,您只需执行 2 个请求:

SELECT spec FROM specs WHERE idspec = $idspec
this will give you needed field from "specs" table, lets call it $spec.
SELECT CONCAT('car-', idcar), CONCAT('$spec-', $spec) FROM cars

这将选择所需的信息

于 2013-07-08T10:34:43.800 回答
1

尝试这个:

SELECT
    CONCAT('car-',idcar),
    CONCAT('width-',width)
FROM
    cars
UNION
SELECT
    CONCAT('car-',idcar),
    CONCAT('weight-',weight)
FROM
    cars
UNION
SELECT
    CONCAT('car-',idcar),
    CONCAT('length-',length)
FROM
    cars
于 2013-07-04T14:48:09.827 回答
0

完毕!

$resultlast = mysqli_query($connecDB,"select * from cars order by (case id when $idcar then 0 else id end), name asc");

$rows = array();
    while($row = mysqli_fetch_array($resultlast)){
        $rows[] = $row;
    }

    while($rowspec = mysqli_fetch_array($resultspec)){
        echo '<div id="'.$rowspec["name"].'">';

            foreach($rows as $row){ 
                $car = stripslashes($row['name']);
                echo '<div id="'.$rowspec["name"].'">' . $car . '</div>';
            }

        echo '</div>';
    }
于 2013-07-04T17:37:58.050 回答
0

替换'为`可以工作!试试看 !

于 2013-07-04T14:48:50.943 回答