1

我想在 MeekroDB 中编写这样的查询:

SELECT * FROM `tablename` WHERE `id` IN (3,1,2) ORDER BY FIELD (`id`,3,1,2)

我希望它在 meekro 中像这样工作:

$possible_ids = array(1,2,3);
DB::query('SELECT * FROM `tablename` WHERE `id` IN %ld0 ORDER BY FIELD (`id`,%ld0)', $possible_ids);

但它得到了这个:

SELECT * FROM `tablename` WHERE `id` IN (3,1,2) ORDER BY FIELD (`id`,(3,1,2))

有什么办法可以避免这个括号吗?

4

1 回答 1

1

试试这个:

$possible_ids = implode(',', array(1, 2, 3));
//echo "SELECT * FROM `tablename` WHERE `id` IN %ld0 ORDER BY FIELD (`id`,%ld0)', $possible_ids";

DB::query('SELECT * FROM `tablename` WHERE `id` IN %ld0 ORDER BY FIELD (`id`,%ld0)', $possible_ids);

您也可以使用回声查询进行检查。尝试它工作正常。

于 2019-12-26T13:41:46.377 回答