Yii2 WHERE IN 查询
Yii2 WHERE IN query
以下活动记录 WHERE IN 查询不起作用。根据文档,这应该有效:
$data = $dropDownData->find()
->select('country, country_text')
->distinct()
->WHERE(['in', 'server', $servers]);
$listData = ArrayHelper::map($data,'country', 'country_text');
sql 等价于:
$query = "SELECT DISTINCT country, country_text
FROM `dropDownData`
WHERE server IN ({$servers})";
$servers 只包含一个字符串 1,2,4
我做错了什么?
基于 Yii2 文档 $servers
应该是数组而不是字符串。
in: operand 1 should be a column or DB expression. Operand 2 can be
either an array or a Query object.
https://www.yiiframework.com/doc/guide/2.0/en/db-query-builder#operator-format
尝试使用合适的数组:
$servers = [1,2,4]
$data = $dropDownData->find()
->select('country, country_text')
->distinct()
->WHERE(['in', 'server',[1,2,4]]);
或
$data = $dropDownData->find()
->select('country, country_text')
->distinct()
->WHERE(['in', 'server', $servers]);
你应该使用数组变量
$servers = [1,2,4];
以下活动记录 WHERE IN 查询不起作用。根据文档,这应该有效:
$data = $dropDownData->find()
->select('country, country_text')
->distinct()
->WHERE(['in', 'server', $servers]);
$listData = ArrayHelper::map($data,'country', 'country_text');
sql 等价于:
$query = "SELECT DISTINCT country, country_text
FROM `dropDownData`
WHERE server IN ({$servers})";
$servers 只包含一个字符串 1,2,4
我做错了什么?
基于 Yii2 文档 $servers
应该是数组而不是字符串。
in: operand 1 should be a column or DB expression. Operand 2 can be either an array or a Query object.
https://www.yiiframework.com/doc/guide/2.0/en/db-query-builder#operator-format
尝试使用合适的数组:
$servers = [1,2,4]
$data = $dropDownData->find()
->select('country, country_text')
->distinct()
->WHERE(['in', 'server',[1,2,4]]);
或
$data = $dropDownData->find()
->select('country, country_text')
->distinct()
->WHERE(['in', 'server', $servers]);
你应该使用数组变量
$servers = [1,2,4];