SELECT 
  cscart_products_categories.product_id, 
  GROUP_CONCAT(
    IF(
      cscart_products_categories.link_type = "M", 
      CONCAT(
        cscart_products_categories.category_id, 
        "M"
      ), 
      cscart_products_categories.category_id
    )
  ) AS category_ids 
FROM 
  cscart_products_categories 
  INNER JOIN cscart_categories ON cscart_categories.category_id = cscart_products_categories.category_id 
  AND cscart_categories.storefront_id IN (0, 1) 
  AND (
    cscart_categories.usergroup_ids = '' 
    OR FIND_IN_SET(
      0, cscart_categories.usergroup_ids
    ) 
    OR FIND_IN_SET(
      1, cscart_categories.usergroup_ids
    )
  ) 
  AND cscart_categories.status IN ('A', 'H') 
WHERE 
  cscart_products_categories.product_id IN (
    1128, 1127, 1126, 1125, 1124, 1123, 1139, 
    1138, 1137, 1136, 1135, 1134, 1133, 
    1132, 1131, 1130, 1129, 827, 1141, 851, 
    1254, 1253, 247, 1284, 1475, 1476, 1477, 
    2109, 2108, 2107, 2112, 2111, 2110, 
    2114, 2113, 2247, 2248, 3066, 3067, 
    3069, 3070, 3071, 3072, 3073, 3075, 
    3076, 3602, 3078, 3079, 3080, 3081, 
    3082, 3083, 3084, 3085, 4812, 4810, 
    4809, 4808, 7741, 7740, 9646, 9647, 
    9648, 9649, 9653, 9654, 9655, 9656, 
    9657, 9658, 9659, 9660, 9869, 9868, 
    9870, 9871, 10276, 10277, 10278, 10348, 
    10347
  ) 
GROUP BY 
  cscart_products_categories.product_id

Query time 0.00289

JSON explain

{
  "query_block": {
    "select_id": 1,
    "cost_info": {
      "query_cost": "156.51"
    },
    "grouping_operation": {
      "using_filesort": false,
      "nested_loop": [
        {
          "table": {
            "table_name": "cscart_products_categories",
            "access_type": "range",
            "possible_keys": [
              "PRIMARY",
              "pt"
            ],
            "key": "pt",
            "used_key_parts": [
              "product_id"
            ],
            "key_length": "3",
            "rows_examined_per_scan": 170,
            "rows_produced_per_join": 170,
            "filtered": "100.00",
            "index_condition": "(`softwarepirmam_hewadelivard_cscart_4`.`cscart_products_categories`.`product_id` in (1128,1127,1126,1125,1124,1123,1139,1138,1137,1136,1135,1134,1133,1132,1131,1130,1129,827,1141,851,1254,1253,247,1284,1475,1476,1477,2109,2108,2107,2112,2111,2110,2114,2113,2247,2248,3066,3067,3069,3070,3071,3072,3073,3075,3076,3602,3078,3079,3080,3081,3082,3083,3084,3085,4812,4810,4809,4808,7741,7740,9646,9647,9648,9649,9653,9654,9655,9656,9657,9658,9659,9660,9869,9868,9870,9871,10276,10277,10278,10348,10347))",
            "cost_info": {
              "read_cost": "80.01",
              "eval_cost": "17.00",
              "prefix_cost": "97.01",
              "data_read_per_join": "2K"
            },
            "used_columns": [
              "product_id",
              "category_id",
              "link_type"
            ]
          }
        },
        {
          "table": {
            "table_name": "cscart_categories",
            "access_type": "eq_ref",
            "possible_keys": [
              "PRIMARY",
              "c_status",
              "p_category_id"
            ],
            "key": "PRIMARY",
            "used_key_parts": [
              "category_id"
            ],
            "key_length": "3",
            "ref": [
              "softwarepirmam_hewadelivard_cscart_4.cscart_products_categories.category_id"
            ],
            "rows_examined_per_scan": 1,
            "rows_produced_per_join": 8,
            "filtered": "5.00",
            "cost_info": {
              "read_cost": "42.50",
              "eval_cost": "0.85",
              "prefix_cost": "156.51",
              "data_read_per_join": "26K"
            },
            "used_columns": [
              "category_id",
              "storefront_id",
              "usergroup_ids",
              "status"
            ],
            "attached_condition": "((`softwarepirmam_hewadelivard_cscart_4`.`cscart_categories`.`storefront_id` in (0,1)) and ((`softwarepirmam_hewadelivard_cscart_4`.`cscart_categories`.`usergroup_ids` = '') or (0 <> find_in_set(0,`softwarepirmam_hewadelivard_cscart_4`.`cscart_categories`.`usergroup_ids`)) or (0 <> find_in_set(1,`softwarepirmam_hewadelivard_cscart_4`.`cscart_categories`.`usergroup_ids`))) and (`softwarepirmam_hewadelivard_cscart_4`.`cscart_categories`.`status` in ('A','H')))"
          }
        }
      ]
    }
  }
}

Result

product_id category_ids
247 322,166,340M
827 166,322,323M
851 322,166,323M
1123 166,322,323M
1124 166,322,323M
1125 166,322,323M
1126 322,166,323M
1127 166,322,323M
1128 166,322,323M
1129 166,322,323M
1130 166,322,323M
1131 166,322,323M
1132 166,322,323M
1133 166,322,323M
1134 322,166,323M
1135 166,322,323M
1136 166,322,323M
1137 166,322,323M
1138 166,322,323M
1139 166,322,323M
1141 166,322,323M
1253 166,322,340M
1254 166,322,340M
1284 322,166,324M
1475 166,322,324M
1476 166,322,324M
1477 166,322,324M
2107 322,166,340M
2108 322,166,340M
2109 322,166,340M
2110 322,166,340M
2111 322,166,340M
2112 322,166,340M
2113 322,166,340M
2114 166,322,340M
2247 322,166,340M
2248 166,322,340M
3066 322,166,471M
3067 322,166,471M
3069 166,322,471M
3070 322,166,471M
3071 322,166,471M
3072 322,166,471M
3073 166,322,471M
3075 322,166,471M
3076 322,166,471M
3078 166,322,471M
3079 166,322,471M
3080 322,166,471M
3081 166,322,471M
3082 322,166,471M
3083 322,166,471M
3084 166,322,471M
3085 166,322,471M
3602 322,166,471M
4808 322,166,324M
4809 166,322,324M
4810 166,322,324M
4812 322,166,324M
7740 340M
7741 340M
9646 323M
9647 323M
9648 323M
9649 323M
9653 323M
9654 323M
9655 323M
9656 323M
9657 323M
9658 323M
9659 323M
9660 323M
9868 323M
9869 323M
9870 323M
9871 323M
10276 340M
10277 340M
10278 340M
10347 322M
10348 322M