gpt4 book ai didi

mysql - Yii1相当于Sql查询

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

我有这个sql查询

SELECT * FROM form_fields 
WHERE id NOT IN ("3", "1", "6")
ORDER BY FIELD (id, "3" ,"1" ,"6")

我想将其转换为 yii1 查询。所以我尝试了这样的

$SQL="SELECT * FROM form_fields WHERE id NOT IN {$sorted_array}  ORDER BY FIELD (id, $sorted_array)";
$connection=Yii::app()->db;
$command=$connection->createCommand($SQL);
$rowCount=$command->execute();
$dataReader=$command->query();

其中 $sorted_array 的值为 Array ( [0] => 3 [1] => 1 [2] => 6 )

它给了我一个错误

PHP notice

Array to string conversion

然后我将 $sorted_array 转换为这样的字符串

$string = implode(' ', $sorted_array);

并再次执行查询

$SQL="SELECT * FROM form_fields WHERE id NOT IN {$string}  ORDER BY FIELD (id, $string)";
$connection=Yii::app()->db;
$command=$connection->createCommand($SQL);
$rowCount=$command->execute();
$dataReader=$command->query();

现在我收到另一个错误

CDbException

CDbCommand failed to execute the SQL statement: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '3 1 6 ORDER BY FIELD (id, 3 1 6)' at line 1. The SQL statement executed was: SELECT * FROM form_fields WHERE id NOT IN 3 1 6 ORDER BY FIELD (id, 3 1 6)

而不是这个

SELECT * FROM form_fields 
WHERE id NOT IN ("3", "1", "6")
ORDER BY FIELD (id, "3" ,"1" ,"6")

我的yii1查询产生了这个错误的sql查询

SELECT * FROM form_fields
WHERE id NOT IN 3 1 6 ORDER BY FIELD (id, 3 1 6)

如有任何帮助,我们将不胜感激

最佳答案

implode的第一个参数应该是逗号:

$string = implode(',', $sorted_array);

关于mysql - Yii1相当于Sql查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34868655/

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