duanguan3863 2014-05-07 12:23
浏览 5
已采纳

笨。 哪个sql有多个值

I have content that i want to filter by a brandname and a permission id and then show it.

my problem is: When i send one value as $permission it works fine. But when a user has multiple permission_id's it doesnt work.

WHERE permission_id = 1, 2, 3;

I have a problem with my select query:

Codeigniter:

$this->db->select('*');
$this->db->from('content');
$this->db->where('brand_name', $brand);
$this->db->where('permission_id', $permission);

SQL:

SELECT * FROM `content` WHERE `brand_name` = $brand_name AND `permission_id` = $permission;

The print_r of $brand & $permission look like this:

Hyundai
1,10,11,2,3,4,5,6,7,8,9

I hope i gave enough information for someone to help me like this.

thank you.

  • 写回答

1条回答 默认 最新

  • douweibiao8471 2014-05-07 12:25
    关注

    To test a field against multiple values, your SQL should not look like :

    WHERE permission_id = 1, 2, 3;
    

    I don't know what your permission_id field looks like, but I guess your condition should rather be:

    WHERE permission_id IN (1, 2, 3);
    

    Try with where_in:

    $this->db->where_in('permission_id', $permission);
    

    where $permission is an array containing the permissions (not a comma separated string).

    With $permission being a string, use explode:

    $this->db->where_in('permission_id', explode(',', $permission));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程