如何 select 所有行中的相同列?
How to select the same columns from all rows?
我有一个 table,每行 50 行,大约 10 列。其中一列称为 "phpclass" 并包含字符串。
实际的字符串条目可以是唯一的,但它也可以在不同的行中相同,即查询过程。
我想查询 table 并收到与 "phpclass" 列中具有相似值的匹配行相关的内容。
即
$resultA = ["Orion", "Nova", "Alpha"];
$resultB = [1, 4, 3];
有人可以帮我创建一个查询吗?
我想你想要
SELECT phpclass, COUNT(*) count
FROM table
GROUP BY phpclass
这会给你返回行
Orion 1
Nova 4
Alpha 5
显示有多少行具有值 Orion
等(不一定按此顺序)。
您应该使用 GROUP BY
进行查询,如下所示:
SELECT phpclass, COUNT(*) occurences
FROM mytable
GROUP BY phpclass
然后您可以使用类似 mysqli_fetch_all
的方法来检索所有行,这将为您提供以下结构:
$result = mysqli_query($con, $sql);
$result = mysqli_fetch_all($result, MYSQLI_ASSOC);
// $result will be like:
// Array(
// Array("phpclass" => "Orion", "occurrences" => 1),
// Array("phpclass" => "Nova", "occurrences" => 4),
// Array("phpclass" => "Alpha", "occurrences" => 5)
// );
$resultA = array_column($result, "phpclass");
$resultB = array_column($result, "occurrences");
print_r ($resultA);
print_r ($resultB);
输出将是:
Array
(
[0] => Orion
[1] => Nova
[2] => Alpha
)
Array
(
[0] => 1
[1] => 4
[2] => 5
)
我有一个 table,每行 50 行,大约 10 列。其中一列称为 "phpclass" 并包含字符串。 实际的字符串条目可以是唯一的,但它也可以在不同的行中相同,即查询过程。
我想查询 table 并收到与 "phpclass" 列中具有相似值的匹配行相关的内容。
即
$resultA = ["Orion", "Nova", "Alpha"];
$resultB = [1, 4, 3];
有人可以帮我创建一个查询吗?
我想你想要
SELECT phpclass, COUNT(*) count
FROM table
GROUP BY phpclass
这会给你返回行
Orion 1
Nova 4
Alpha 5
显示有多少行具有值 Orion
等(不一定按此顺序)。
您应该使用 GROUP BY
进行查询,如下所示:
SELECT phpclass, COUNT(*) occurences
FROM mytable
GROUP BY phpclass
然后您可以使用类似 mysqli_fetch_all
的方法来检索所有行,这将为您提供以下结构:
$result = mysqli_query($con, $sql);
$result = mysqli_fetch_all($result, MYSQLI_ASSOC);
// $result will be like:
// Array(
// Array("phpclass" => "Orion", "occurrences" => 1),
// Array("phpclass" => "Nova", "occurrences" => 4),
// Array("phpclass" => "Alpha", "occurrences" => 5)
// );
$resultA = array_column($result, "phpclass");
$resultB = array_column($result, "occurrences");
print_r ($resultA);
print_r ($resultB);
输出将是:
Array
(
[0] => Orion
[1] => Nova
[2] => Alpha
)
Array
(
[0] => 1
[1] => 4
[2] => 5
)