Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php MSQL加入:我无法显示类别列表_Php_Mysql_Sql_Join - Fatal编程技术网

Php MSQL加入:我无法显示类别列表

Php MSQL加入:我无法显示类别列表,php,mysql,sql,join,Php,Mysql,Sql,Join,我试图在“规格”列表中显示表中的所有“汽车”,具体取决于它接收到的规格表值 表: 汽车: 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 idspe

我试图在“规格”列表中显示表中的所有“汽车”,具体取决于它接收到的规格表值

表:

汽车:

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
规格表:

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
输出为:

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)

谢谢大家!

试试这个:

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

替换为“可以工作!”!试试看

我看你是想把桌子打开。如果是这样,您的查询就有点不对劲了

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
试试这个

你的桌子设计有误。在spec和car之间还有一张桌子。例如car_apec_值,该值将包含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'");
完成了

$rows=array();
while($row=mysqli\u fetch\u数组($resultlast)){
$rows[]=$row;
}
while($rowspec=mysqli\u fetch\u数组($resultspec)){
回声';
foreach($行作为$行){
$car=stripslashes($row['name']);
回声“.$car.”;
}
回声';
}

据我所知,您在任何情况下都需要请求选择所有车辆。但选定的数据字段取决于您提供的“idspec”。我的意思是如果你给idspec=2,你想得到:

car-1 weight-300
car-2 weight-210
car-3 weight-230
car-4 weight-210
在这种情况下,您只需执行两个请求:

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

这将选择所需信息

删除我尝试过的单个报价,但这不是问题:/谢谢+1
UNION
就足够了,因为行肯定是两行的。谢谢,有可能在一段时间内或以后再放一行吗?@santyas。对但是php编码不是我的专长,所以我将把它留给更有知识的人。@Stephen。感谢您修复别名。但是,您应该养成使用
union all
而不是
union
的习惯<代码>联合进行重复消除,这在不需要时通常是额外的工作。好的,谢谢,但我也有同样的要求。有可能把它放在一段时间或一段时间内吗?@santyas你是什么意思?每次在循环中执行它?最终目的是什么?是的,在我的问题中,输出是最终目的。在一个“while”中显示所有规格,在里面有一个包含所有汽车的foreach和“while”的具体值在每辆车上。例如:
WIDTH
  • car-1 WIDTH:20
  • car-2 WIDTH:15HEIGHT
  • car-1 HEIGHT:20
  • car-2 HEIGHT:15
    您只需执行一次查询,然后在结果中循环并构建如上所示的列表,谢谢。我用while和foreach解决了它。我在上面写了我的例子。