gpt4 book ai didi

php - 如何将 MySQL 定义的名称导入 php?

转载 作者:太空宇宙 更新时间:2023-11-03 12:29:21 25 4
gpt4 key购买 nike

如果我有一个像这样的mysql查询

(SELECT COUNT(*) FROM data AS amount)
UNION
(SELECT COUNT(*) FROM data WHERE some < 50 AS something)

然后像这样用 php 创建一个数组 $row = mysqli_fetch_array($sql, MYSQLI_ASSOC);

我现在如何解决每个 AS 名称。这不起作用:echo $row["amount"];。我的第二个问题是为什么在有 WHERE 子句时我不能使用 AS something

最佳答案

试试这个:

(
SELECT
'amount1' as za_name,
COUNT(*) as za_count
FROM
data
)
UNION
(
SELECT
'amount2' as za_name,
COUNT(*) as za_count
FROM
data
WHERE some < 50
)

然后可以通过$row[za_name]进行区分,得到金额$row[za_count]

对于第二个问题:如果你做一个临时表就可以使用它:

SELECT
tmp.za_name,
tmp.za_count
FROM (
SELECT
'amount2' as za_name,
COUNT(*) as za_count
FROM
data
WHERE some < 50
) as tmp

关于php - 如何将 MySQL 定义的名称导入 php?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16195524/

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