Php 从mysql表中获取多个关联行
我有一个名为Php 从mysql表中获取多个关联行,php,mysql,sql,Php,Mysql,Sql,我有一个名为goods的表,看起来像这样 id | name | type | 1 | honda | car | 2 | bianci | bike | 3 | ferari | car | 4 | hurley | motor bike | 4 | bar | motor bike | array("car"=>"honda", "bike"=>
goods
的表,看起来像这样
id | name | type |
1 | honda | car |
2 | bianci | bike |
3 | ferari | car |
4 | hurley | motor bike |
4 | bar | motor bike |
array("car"=>"honda", "bike"=>"bianci", "car"=>"ferrari", "motor bike"=>"hurley");
我试图从这个表中得到一个关联数组,数组的索引应该是类型
,值应该是名称
。最终结果应该是这样的
id | name | type |
1 | honda | car |
2 | bianci | bike |
3 | ferari | car |
4 | hurley | motor bike |
4 | bar | motor bike |
array("car"=>"honda", "bike"=>"bianci", "car"=>"ferrari", "motor bike"=>"hurley");
我尝试了从商品中选择名称作为类型,其中键入('car'、'bike'、'motorbike')
但是仍然给出数组的结果索引
type
您的查询应该如下所示:
SELECT GROUP_CONCAT(`name`) AS `brand`,
`type`
FROM goods
WHERE `type` IN ('car', 'bike', 'motor bike')
GROUP BY `type`
$result = array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$result[$row['type']] = $row['brand'];
}
print_r($result);
$result = array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$result[$row['type']][] = $row['name'];
}
print_r($result);
其中,上述查询的结果类似于:
name | type
-------------------------------
honda, ferari | car
bianci | bike
hurley, bar | motor bike
在您的PHP上,应该是这样的:
SELECT GROUP_CONCAT(`name`) AS `brand`,
`type`
FROM goods
WHERE `type` IN ('car', 'bike', 'motor bike')
GROUP BY `type`
$result = array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$result[$row['type']] = $row['brand'];
}
print_r($result);
$result = array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$result[$row['type']][] = $row['name'];
}
print_r($result);
由于数组上不能有重复键,因此通过使用groupby
对类型进行分组,并使用GROUP\u CONCAT
将名称分组为单个字符串,我们可以得到与您想要的结果相近的结果:
array("car" => "honda, ferrari",
"bike" => "bianci",
"motor bike" => "hurley, bar"
);
另一种方法是:
SELECT `name`,
`type`
FROM goods
WHERE `type` IN ('car', 'bike', 'motor bike')
在您的PHP上,应该是这样的:
SELECT GROUP_CONCAT(`name`) AS `brand`,
`type`
FROM goods
WHERE `type` IN ('car', 'bike', 'motor bike')
GROUP BY `type`
$result = array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$result[$row['type']] = $row['brand'];
}
print_r($result);
$result = array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$result[$row['type']][] = $row['name'];
}
print_r($result);
使用此方法,您可以将type作为键,将数组作为值,并使用foreach
或任何其他循环轻松读取所有名称:
array("car" => array("honda", "ferrari"),
"bike" => array("bianci"),
"motor bike" => array("hurley", "bar")
);
根据你的提问,如果你想通过SQL实现,那么这就是你能做的,也许有更好的方法。因此,在PHP代码中,您必须以某种方式处理这些空/空值。对PHP一无所知
select
isnull(car,'') as car,
isnull(bike,'') as bike,
isnull([motor bike],'') as 'motor_bike'
from
(
SELECT
case when name in ('honda','ferari') then name end as car,
case when name = 'bianci' then name end as bike,
case when name in ('bar','hurley') then name end as 'motor bike'
FROM goods
) tab
(或)根据评论的直接方式
SELECT
case when type = 'car' then name end as car,
case when type = 'bike' then name end as bike,
case when type = 'motor bike' then name end as 'motor bike'
FROM goods
这将导致
本例中的
类型是一个列标题。虽然可以更改列标题标签,但据我所知,动态列标题标签是不可能的。在php代码中,您可以在SQL之外实现这一点。您的SQL查询很好,您可以发布您的php吗?@Kami我实现的是在php中使用foreach循环,但我想在mysql中可能有这样做的方法。@mituw16补充了一点说明。这不是一个与错误相关的问题。不可能使用简单的sql(可能类似于数据透视表)吗?但这可能会使sql变得更加复杂,不能直接执行吗<代码>当类型为'car'时,则将结尾命名为car,
?是的,当然可以。我走了另一条路。正如所说的,解决问题的方法不止一种。这种方法是好的,但并不方便。它需要在语句中显式写入名称。在我上面给出的示例中,特别是:In('car','bike','motorbike')
实际上是一个变量。也就是说:IN(“$inpulodegoods”)
。例如,我在这里放松了一下。所以,现在如果我用你的答案,会有点复杂,因为这些名字分散成一行,而不是Prix的答案,它们是连在一起的。因此,将它们替换为一个变量,即$introdegoods
,在imho中会方便得多。谢谢大家的努力。到目前为止,这是最好的方法。唯一的缺点是我必须使用explode()
来获取汽车名称。很可能没有任何汽车/自行车的名称中有逗号name@iOi您可以将分隔符更改为非逗号,但取决于您将如何使用MySQL查询的结果,可能有更好的方法来读取它,除非您能够提供有关如何使用结果的更多信息,我想这是最接近您的方法“想要”。@iOi我在底部添加了一种你可能更喜欢的方式,因为你不必对结果进行分解。我认为你的第二个答案更好。我真不敢相信我离那个lol这么近。无论如何,谢谢。