gpt4 book ai didi

php - 按一个字段从表组中获取所有值

转载 作者:行者123 更新时间:2023-11-29 03:23:13 25 4
gpt4 key购买 nike

我有一个表“products”,它包含如下值。

column-a     column-b     column-c      column-d   ....
1023 57 value1 value2 ....
1024 58 value3 value4 ....
1025 58 value5 value6 ....
1026 57 value7 value8 ....
1027 59 value9 value10 ....
1028 60 value11 value12 ....
1029 59 value13 value14 ....

我想从这个表中按 b 列分组显示如下结果。

数组
(

 [0] => stdClass Object
(
[column-a] => 1023
[column-b] => 57
[column-c] => value1
[column-d] => value2
.
.
.
)
[1] => stdClass Object
(
[column-a] => 1024
[column-b] => 58
[column-c] => value3
[column-d] => value4
.
.
.
)
[2] => stdClass Object
(
[column-a] => 1027
[column-b] => 59
[column-c] => value9
[column-d] => value10
.
.
.
)

[3] => stdClass Object
(
[column-a] => 1028
[column-b] => 60
[column-c] => value11
[column-d] => value12
.
.
.
)

[4] => stdClass Object
(
[column-a] => 1026
[column-b] => 57
[column-c] => value7
[column-d] => value8
.
.
.
)
[5] => stdClass Object
(
[column-a] => 1025
[column-b] => 58
[column-c] => value5
[column-d] => value6
.
.
.
)
[6] => stdClass Object
(
[column-a] => 1029
[column-b] => 59
[column-c] => value13
[column-d] => value14
.
.
.
)

)

如何按列 b 从表组中选择所有表值。

我试过如下,但它只显示一次b列的值

SELECT * FROM products GROUP BY column-b;

有人用相同的方式帮助检索所有数据。提前致谢!

最佳答案

您可以按如下方式使用查询:

SELECT column-a,column-b,GROUP_CONCAT(column-c SEPARATOR ' , '),GROUP_CONCAT(column-d SEPARATOR ' , ') FROM products GROUP BY column-b

它将返回具有不同 column-b 值的记录数组,以及 column-c 和 column-d 的多个值

关于php - 按一个字段从表组中获取所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40602890/

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