我的查询有什么问题?我确定它的工作几天前,但今天我不知道它为什么会发生。我怎么能解决这个问题? 抱歉,我的英文不好
查询:
function getUserGroupList() {
$rulesQuery = "SELECT * FROM t_rules";
$rulesResult = pg_query($this->DB, $rulesQuery);
$nextQuery = '';
while ($data = pg_fetch_array($rulesResult)) {
$nextQuery .= ",(SELECT COUNT(*) FROM t_user_group_rules ugp WHERE ugp.ref_user_group = ug.id AND ugp.ref_rules = ".$data['id'].") as ".str_replace(' ', '_', $data['c_name'])."_count,(SELECT c_status FROM t_user_group_rules ugp WHERE ugp.ref_user_group = ug.id AND ugp.ref_rules = ".$data['id'].") as ".str_replace(' ', '_', $data['c_name']);
}
$query = "SELECT ug.id, ug.c_name $nextQuery
FROM t_user_group ug
WHERE ug.c_active = 1";
$result = pg_query($this->DB, $query);
// echo $query;
// exit();
$all = array();
while ($data = pg_fetch_assoc($result)) {
array_push($all, $data);
}
echo json_encode($all);
}
如果我回复$query
,它就会变成这样
SELECT
ug.id,
ug.c_name,
(SELECT count(*)
FROM t_user_group_rules ugp
WHERE ugp.ref_user_group = ug.id
AND ugp.ref_rules = 1
) AS command_count,
(SELECT c_status
FROM t_user_group_rules ugp
WHERE ugp.ref_user_group = ug.id
AND ugp.ref_rules = 1
) AS command,
(SELECT count(*)
FROM t_user_group_rules ugp
WHERE ugp.ref_user_group = ug.id
AND ugp.ref_rules = 2
) AS user_count,
(SELECT c_status
FROM t_user_group_rules ugp
WHERE ugp.ref_user_group = ug.id
AND ugp.ref_rules = 2
) AS USER,
(SELECT count(*)
FROM t_user_group_rules ugp
WHERE ugp.ref_user_group = ug.id
AND ugp.ref_rules = 3
) AS executive_count,
(SELECT c_status
FROM t_user_group_rules ugp
WHERE ugp.ref_user_group = ug.id
AND ugp.ref_rules = 3
) AS executive,
(SELECT count(*)
FROM t_user_group_rules ugp
WHERE ugp.ref_user_group = ug.id
AND ugp.ref_rules = 4
) AS rekon_count,
(SELECT c_status
FROM t_user_group_rules ugp
WHERE ugp.ref_user_group = ug.id
AND ugp.ref_rules = 4
) AS rekon,
(SELECT count(*)
FROM t_user_group_rules ugp
WHERE ugp.ref_user_group = ug.id
AND ugp.ref_rules = 5
) AS ej_count,
(SELECT c_status
FROM t_user_group_rules ugp
WHERE ugp.ref_user_group = ug.id
AND ugp.ref_rules = 5
) AS ej,
(SELECT count(*)
FROM t_user_group_rules ugp
WHERE ugp.ref_user_group = ug.id
AND ugp.ref_rules = 12
) AS tes_count,
(SELECT c_status
FROM t_user_group_rules ugp
WHERE ugp.ref_user_group = ug.id
AND ugp.ref_rules = 12
) AS tes
FROM t_user_group ug
WHERE ug.c_active = 1;
答案 0 :(得分:0)
如果查询中的任何count(*)
返回的值大于1,您将收到此错误。
这是因为SELECT
列表中的子选择可能不会返回多个值,因为它将导致单个值(如果没有结果,则会得到SQL NULL)。