gpt4 book ai didi

php - Yii 条件条件不工作 SQL 错误

转载 作者:行者123 更新时间:2023-11-30 23:01:35 26 4
gpt4 key购买 nike

$criteria->condition .= 'UserID = '.$userId.' UNION
(
SELECT * FROM display WHERE
UserID IN
(
SELECT DISTINCT UserID FROM lkusergroup WHERE
GroupID IN
(
SELECT GroupID FROM lkusergroup WHERE UserID = '.$userId.'
)
)
)';

我正在尝试执行上面的代码但出现以下错误

    CDbCommand failed to execute the SQL statement: SQLSTATE[21000]: 
Cardinality violation: 1222 The used SELECT statements have a different number of columns. The SQL statement executed was:
SELECT COUNT(*) FROM `display` `t` WHERE UserID = 2 UNION
(
SELECT * FROM display WHERE
UserID IN
(
SELECT DISTINCT UserID FROM lkusergroup WHERE
GroupID IN
(
SELECT GroupID FROM lkusergroup WHERE UserID = 2
)
)
)

为什么查询生成 count(*) 这是实际问题。我试过可能的方法来解决这个错误 bt 没有任何建议?

最佳答案

您还需要在下面的行中添加计数函数:

SELECT * FROM display WHERE 
UserID IN

你的代码必须是这样的:

$criteria->condition .= 'UserID = '.$userId.' UNION
(
SELECT count(*) FROM display WHERE
UserID IN
(
SELECT DISTINCT UserID FROM lkusergroup WHERE
GroupID IN
(
SELECT GroupID FROM lkusergroup WHERE UserID = '.$userId.'
)
)
)';

在 UNION 时,必须从查询中返回相同数量的列。

已编辑:

你的查询必须是这样的:

SELECT COUNT(*), * FROM display WHERE
UserID IN
(
SELECT DISTINCT UserID FROM lkusergroup WHERE
GroupID IN
(
SELECT GroupID FROM lkusergroup WHERE UserID = 2
)
)

关于php - Yii 条件条件不工作 SQL 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23737474/

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