gpt4 book ai didi

php - MySQL 连接数组

转载 作者:行者123 更新时间:2023-11-29 21:11:33 27 4
gpt4 key购买 nike

如何连接一些表,然后生成数组。

表“fruits”是主表。

表“ordered”中的“fruit_id”字段取自表“fruits”id。

表:水果

------------------------------------------------------+
id fruits date_created
------------------------------------------------------+
1 Apple 2016-03-31 14:29:29
2 Blueberry 2016-03-30 14:22:54
3 Coconut 2016-03-30 14:19:12
------------------------------------------------------+

表:已排序

------------------------------------------------------------------------------------------------+
id fruit_id package_id price_with_ship price_without_ship
------------------------------------------------------------------------------------------------+
1 3 10 150 0
2 3 11 0 110
3 2 10 0 87
4 2 11 0 95
5 2 12 100 0
6 1 12 75 0
------------------------------------------------------------------------------------------------+

这是我建议的结果。

Array
(
[0] => Array
(
[id] => 1
[name] => Apple
[date_created] => 2016-03-31 14:29:29
[package_id] => Array
(
[0] => 10
[1] => 11
)
[price_with_ship] => Array
(
[0] => 150
[1] => 0
)
[price_with_ship] => Array
(
[0] => 0
[1] => 110
)
)

[1] => Array
(
[id] => 1
[name] => Blueberry
[date_created] => 2016-03-30 14:22:54
[package_id] => Array
(
[0] => 10
[1] => 11
[2] => 12
)
[price_with_ship] => Array
(
[0] => 0
[1] => 0
[2] => 100
)
[price_with_ship] => Array
(
[0] => 87
[1] => 95
[2] => 0
)
)

[1] => Array
(
[id] => 1
[name] => Coconut
[date_created] => 2016-03-30 14:19:12
[package_id] => 12
[price_with_ship] => 75
[price_with_ship] => 0
)

提前感谢您的帮助。

最佳答案

您可以按照以下方式加入-

select f.*, o.* 
from fruit as f
join order as o on o.fruit_id=f.id
where f.id=1;

关于php - MySQL 连接数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36329833/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com