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 (
    37513, 
    37514, 
    37515, 
    37516, 
    37520, 
    37521, 
    37522, 
    37523, 
    37524, 
    37525, 
    37526, 
    37527, 
    37528, 
    37529, 
    37530, 
    37531, 
    37532, 
    37533, 
    37534, 
    37535, 
    37536, 
    37537, 
    37538, 
    37539, 
    37540, 
    37541, 
    37542, 
    37543, 
    37544, 
    37545, 
    37546, 
    37547, 
    37548, 
    37549, 
    37550, 
    37551, 
    37552, 
    37553, 
    37554, 
    37555, 
    37557, 
    37558, 
    37559, 
    37560, 
    37561, 
    37562, 
    37563, 
    37820, 
    37821, 
    37822, 
    37823, 
    37824, 
    37825, 
    37826, 
    37827, 
    37828, 
    37829, 
    37830, 
    37831, 
    37832, 
    37833, 
    37834, 
    37835, 
    37836, 
    37837, 
    37838, 
    37839, 
    37840, 
    37841, 
    37842, 
    37843, 
    37844, 
    37845, 
    37846, 
    37847, 
    37848, 
    37849, 
    37850, 
    37851, 
    37852, 
    37853, 
    37854, 
    37855, 
    37856, 
    37858, 
    37859, 
    37860, 
    37861, 
    37862, 
    37863, 
    37864, 
    37865, 
    37866, 
    37867, 
    37868, 
    37869, 
    37870, 
    37871, 
    37872, 
    37873, 
    37874, 
    37875, 
    37877, 
    37878, 
    37879, 
    37880, 
    37881, 
    37882, 
    37883, 
    37884, 
    37885, 
    37886, 
    37887, 
    37888, 
    37889, 
    37890, 
    37891, 
    37892, 
    37893, 
    37894, 
    37895, 
    37896, 
    37897, 
    38150, 
    38151, 
    38152, 
    38155, 
    38156
  ) 
GROUP BY 
  q_products_categories.product_id

Query time 0.00231

JSON explain

{
  "query_block": {
    "select_id": 1,
    "cost_info": {
      "query_cost": "661.01"
    },
    "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": 205,
            "rows_produced_per_join": 205,
            "filtered": "100.00",
            "index_condition": "(`portal`.`q_products_categories`.`product_id` in (37513,37514,37515,37516,37520,37521,37522,37523,37524,37525,37526,37527,37528,37529,37530,37531,37532,37533,37534,37535,37536,37537,37538,37539,37540,37541,37542,37543,37544,37545,37546,37547,37548,37549,37550,37551,37552,37553,37554,37555,37557,37558,37559,37560,37561,37562,37563,37820,37821,37822,37823,37824,37825,37826,37827,37828,37829,37830,37831,37832,37833,37834,37835,37836,37837,37838,37839,37840,37841,37842,37843,37844,37845,37846,37847,37848,37849,37850,37851,37852,37853,37854,37855,37856,37858,37859,37860,37861,37862,37863,37864,37865,37866,37867,37868,37869,37870,37871,37872,37873,37874,37875,37877,37878,37879,37880,37881,37882,37883,37884,37885,37886,37887,37888,37889,37890,37891,37892,37893,37894,37895,37896,37897,38150,38151,38152,38155,38156))",
            "cost_info": {
              "read_cost": "374.01",
              "eval_cost": "41.00",
              "prefix_cost": "415.01",
              "data_read_per_join": "3K"
            },
            "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": 41,
            "filtered": "20.00",
            "cost_info": {
              "read_cost": "205.00",
              "eval_cost": "8.20",
              "prefix_cost": "661.01",
              "data_read_per_join": "108K"
            },
            "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
37513 5M
37514 4M
37515 4M
37516 39M
37520 39,301M
37521 4M
37522 5M
37523 4M
37524 5M
37525 4M
37526 5M
37527 39M
37528 39M
37529 5M
37530 39M
37531 4M
37532 39M
37533 4M
37534 39M
37535 39M
37536 39M
37537 39M
37538 4M
37539 4M
37540 4M
37541 4M
37542 4M
37543 4M
37544 39M
37545 5M
37546 4M
37547 4M
37548 39M
37549 5M
37550 39M
37551 4M
37552 39M
37553 39M
37554 39M
37555 39M
37557 39M
37558 5M
37559 39M
37560 4M
37561 4M
37562 4M
37563 4M
37820 5M
37821 5M
37822 4M
37823 4M
37824 4M
37825 5M
37826 4M
37827 5M
37828 400M
37829 400M
37830 586M
37831 145M
37832 39M
37833 4M
37834 400M
37835 400M
37836 135M
37837 135M
37838 135M
37839 135M
37840 135M
37841 135M
37842 400M
37843 135M
37844 400M
37845 135M
37846 135M
37847 400M
37848 400M
37849 135M
37850 4M
37851 135M
37852 135M
37853 135M
37854 135M
37855 135M
37856 134M
37858 3M
37859 135M
37860 135M
37861 586M
37862 5M
37863 145M
37864 5M
37865 145M
37866 145M
37867 39M
37868 39M
37869 39M
37870 145M
37871 135M
37872 4M
37873 4M
37874 39M
37875 400M
37877 400M
37878 153M
37879 5M
37880 4M
37881 5M
37882 5M
37883 4M
37884 39M
37885 39M
37886 400M
37887 5M
37888 5M
37889 39M
37890 39M
37891 135M
37892 145M
37893 39M
37894 135M
37895 4M
37896 5M
37897 400M
38150 39M
38151 5M
38152 4M
38155 4M
38156 4M