如何在单个查询中获取与提供的 ID 对应的所有“名称”列?
假设我有一个像这样的 php 数组 $ids = [ '1', '3', '7', '24', ... ];
那么,是否有可能得到下表中的“名称” WHERE $ids[i]='id'
在该表中,全部在单个查询中,或者我是否需要使用 for
循环?
CREATE TABLE `services` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL DEFAULT '',
`description` varchar(100) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`) USING BTREE
)
我正在使用 CodeIgniter。
最佳答案
您可以为此使用where_in
$ids = [ '1', '3', '7', '24', ... ];
$this->db->select('name');
$this->db->from('services');
$this->db->where_in('id', $ids);
关于php - MySql select field_1 where X = field_2 for all supplied Xs in a single query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32449566/