SELECT 
  q_products_categories.product_id, 
  GROUP_CONCAT(
    IF(
      q_products_categories.link_type = "M", 
      CONCAT(
        q_products_categories.category_id, 
        "M"
      ), 
      q_products_categories.category_id
    )
  ) AS category_ids 
FROM 
  q_products_categories 
  INNER JOIN q_categories ON q_categories.category_id = q_products_categories.category_id 
  AND (
    q_categories.usergroup_ids = '' 
    OR FIND_IN_SET(0, q_categories.usergroup_ids) 
    OR FIND_IN_SET(1, q_categories.usergroup_ids)
  ) 
  AND q_categories.status IN ('A', 'H') 
WHERE 
  q_products_categories.product_id IN (
    5878, 5879, 5880, 5881, 5882, 5883, 5884, 
    5885, 5886, 5887, 5888, 5889, 5890, 
    5891, 5892, 5893, 5894, 5895, 5896
  ) 
GROUP BY 
  q_products_categories.product_id

Query time 0.00063

JSON explain

{
  "query_block": {
    "select_id": 1,
    "cost_info": {
      "query_cost": "68.41"
    },
    "grouping_operation": {
      "using_filesort": false,
      "nested_loop": [
        {
          "table": {
            "table_name": "q_products_categories",
            "access_type": "range",
            "possible_keys": [
              "PRIMARY",
              "pt"
            ],
            "key": "pt",
            "used_key_parts": [
              "product_id"
            ],
            "key_length": "3",
            "rows_examined_per_scan": 19,
            "rows_produced_per_join": 19,
            "filtered": "100.00",
            "index_condition": "(`portal`.`q_products_categories`.`product_id` in (5878,5879,5880,5881,5882,5883,5884,5885,5886,5887,5888,5889,5890,5891,5892,5893,5894,5895,5896))",
            "cost_info": {
              "read_cost": "41.81",
              "eval_cost": "3.80",
              "prefix_cost": "45.61",
              "data_read_per_join": "304"
            },
            "used_columns": [
              "product_id",
              "category_id",
              "link_type"
            ]
          }
        },
        {
          "table": {
            "table_name": "q_categories",
            "access_type": "eq_ref",
            "possible_keys": [
              "PRIMARY",
              "c_status",
              "p_category_id"
            ],
            "key": "PRIMARY",
            "used_key_parts": [
              "category_id"
            ],
            "key_length": "3",
            "ref": [
              "portal.q_products_categories.category_id"
            ],
            "rows_examined_per_scan": 1,
            "rows_produced_per_join": 3,
            "filtered": "20.00",
            "cost_info": {
              "read_cost": "19.00",
              "eval_cost": "0.76",
              "prefix_cost": "68.41",
              "data_read_per_join": "10K"
            },
            "used_columns": [
              "category_id",
              "usergroup_ids",
              "status"
            ],
            "attached_condition": "(((`portal`.`q_categories`.`usergroup_ids` = '') or find_in_set(0,`portal`.`q_categories`.`usergroup_ids`) or find_in_set(1,`portal`.`q_categories`.`usergroup_ids`)) and (`portal`.`q_categories`.`status` in ('A','H')))"
          }
        }
      ]
    }
  }
}

Result

product_id category_ids
5878 418M
5879 418M
5880 418M
5881 418M
5882 418M
5883 418M
5884 418M
5885 418M
5886 418M
5887 418M
5888 418M
5889 418M
5890 418M
5891 418M
5892 418M
5893 418M
5894 418M
5895 418M
5896 418M