doufei1852 2014-04-18 05:11
浏览 1300
已采纳

PHP / MYSQL-如何使用IN查询

When i am add new post that time select multiple category and store in database like 48,52 into single field sub_cat_id

I am try to get value using IN but not get properly

SELECT *
FROM tbl_listing 
WHERE status='1' and sub_cat_id IN (52)
ORDER BY tbl_listing.company_name DESC LIMIT 0, 15

enter image description here

I am store value into db like below screenshot

Please help me

  • 写回答

3条回答 默认 最新

  • doublel83422 2014-04-18 05:45
    关注

    If you are passing 48,52 as comma separated numbers as a string to IN then search is not performed correctly. You have to use FIND_IN_SET with column and the CSV value.

    SELECT * FROM tbl_listing 
    WHERE status='1' 
      and ( FIND_INSET( 48, sub_cat_id ) > 0
            OR FIND_INSET( 52, sub_cat_id ) > 0 )
    ORDER BY 
      tbl_listing.company_name DESC 
    LIMIT 0, 15
    

    Refer to:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?