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 (
    20775, 
    20824, 
    20773, 
    20809, 
    20789, 
    20840, 
    20778, 
    20730, 
    20735, 
    20720, 
    20731, 
    20793, 
    20748, 
    20970, 
    20760, 
    20790, 
    20712, 
    20713, 
    20954, 
    20743, 
    20765, 
    20758, 
    21008, 
    20801, 
    20953, 
    20882, 
    20745, 
    20929, 
    20979, 
    21068, 
    21012, 
    20932, 
    20812, 
    20852, 
    20725, 
    21010, 
    21071, 
    20823, 
    20923, 
    20826, 
    20797, 
    20853, 
    20802, 
    20884, 
    20815, 
    20774, 
    20723, 
    20832, 
    20900, 
    20858, 
    20871, 
    21067, 
    20718, 
    20766, 
    20780, 
    20752, 
    20784, 
    20709, 
    21066, 
    21076, 
    20901, 
    20930, 
    20955, 
    20678, 
    20796, 
    20948, 
    20989, 
    20862, 
    20800, 
    21002, 
    20828, 
    20861, 
    20781, 
    20835, 
    20744, 
    20977, 
    20902, 
    20927, 
    20833, 
    20995, 
    21007, 
    20679, 
    20818, 
    20831, 
    20837, 
    20866, 
    20740, 
    20750, 
    21018, 
    20728, 
    20761, 
    20938, 
    21029, 
    20808, 
    20847, 
    20844, 
    20918, 
    20947, 
    20963, 
    20985, 
    20806, 
    20859, 
    20719, 
    20950, 
    20867, 
    21016, 
    20946, 
    20868, 
    20978, 
    20881, 
    20822, 
    20855, 
    20960, 
    20742, 
    20907, 
    20937, 
    21077, 
    20749, 
    20753, 
    20928, 
    21033, 
    20958, 
    20959, 
    20924, 
    20869, 
    21025, 
    20785, 
    20872, 
    20777, 
    20983, 
    20726, 
    20994, 
    20878, 
    20945, 
    20908, 
    20899, 
    20830, 
    20920, 
    21041, 
    20717, 
    21034, 
    21027, 
    21039, 
    20879, 
    20910, 
    20734, 
    21011, 
    20851, 
    21040, 
    20998, 
    21030, 
    21055, 
    21054
  ) 
GROUP BY 
  cscart_products_categories.product_id

Query time 0.00371

JSON explain

{
  "query_block": {
    "select_id": 1,
    "cost_info": {
      "query_cost": "266.26"
    },
    "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": 285,
            "rows_produced_per_join": 285,
            "filtered": "100.00",
            "index_condition": "(`cscartdb`.`cscart_products_categories`.`product_id` in (20775,20824,20773,20809,20789,20840,20778,20730,20735,20720,20731,20793,20748,20970,20760,20790,20712,20713,20954,20743,20765,20758,21008,20801,20953,20882,20745,20929,20979,21068,21012,20932,20812,20852,20725,21010,21071,20823,20923,20826,20797,20853,20802,20884,20815,20774,20723,20832,20900,20858,20871,21067,20718,20766,20780,20752,20784,20709,21066,21076,20901,20930,20955,20678,20796,20948,20989,20862,20800,21002,20828,20861,20781,20835,20744,20977,20902,20927,20833,20995,21007,20679,20818,20831,20837,20866,20740,20750,21018,20728,20761,20938,21029,20808,20847,20844,20918,20947,20963,20985,20806,20859,20719,20950,20867,21016,20946,20868,20978,20881,20822,20855,20960,20742,20907,20937,21077,20749,20753,20928,21033,20958,20959,20924,20869,21025,20785,20872,20777,20983,20726,20994,20878,20945,20908,20899,20830,20920,21041,20717,21034,21027,21039,20879,20910,20734,21011,20851,21040,20998,21030,21055,21054))",
            "cost_info": {
              "read_cost": "138.01",
              "eval_cost": "28.50",
              "prefix_cost": "166.51",
              "data_read_per_join": "4K"
            },
            "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": [
              "cscartdb.cscart_products_categories.category_id"
            ],
            "rows_examined_per_scan": 1,
            "rows_produced_per_join": 14,
            "filtered": "5.00",
            "cost_info": {
              "read_cost": "71.25",
              "eval_cost": "1.43",
              "prefix_cost": "266.26",
              "data_read_per_join": "47K"
            },
            "used_columns": [
              "category_id",
              "storefront_id",
              "usergroup_ids",
              "status"
            ],
            "attached_condition": "((`cscartdb`.`cscart_categories`.`storefront_id` in (0,1)) and ((`cscartdb`.`cscart_categories`.`usergroup_ids` = '') or (0 <> find_in_set(0,`cscartdb`.`cscart_categories`.`usergroup_ids`)) or (0 <> find_in_set(1,`cscartdb`.`cscart_categories`.`usergroup_ids`))) and (`cscartdb`.`cscart_categories`.`status` in ('A','H')))"
          }
        }
      ]
    }
  }
}

Result

product_id category_ids
20678 747,50M
20679 747,50M
20709 753,729M
20712 760,676M
20713 759,676M
20717 756,50M
20718 754,676M
20719 761,50M
20720 754,676M
20723 760,50M
20725 761,50M
20726 757,50M
20728 756,50M
20730 756,676M
20731 756,82M
20734 758,50M
20735 752,50M
20740 754,50M
20742 755,676M
20743 755,676M
20744 757,50M
20745 752,50M
20748 757,676M
20749 761,50M
20750 759,50M
20752 752,50M
20753 761,535M
20758 757,50M
20760 759,676M
20761 752,676M
20765 760,676M
20766 756,676M
20773 754,50M
20774 755,50M
20775 754,50M
20777 759,50M
20778 754,50M
20780 757,50M
20781 752,676M
20784 754,50M
20785 762,50M
20789 756,50M
20790 754,50M
20793 755,50M
20796 758,50M
20797 758,50M
20800 747,50M
20801 755,50M
20802 760,50M
20806 760,50M
20808 755,50M
20809 756,50M
20812 758,50M
20815 756,50M
20818 760,50M
20822 756,50M
20823 755,50M
20824 756,50M
20826 756,676M
20828 758,50M
20830 755,50M
20831 755,50M
20832 758,676M
20833 755,50M
20835 760,50M
20837 754,50M
20840 756,676M
20844 755,50M
20847 757,50M
20851 755,50M
20852 754,50M
20853 755,50M
20855 755,50M
20858 755,50M
20859 762,50M
20861 760,50M
20862 761,676M
20866 755,50M
20867 754,50M
20868 760,50M
20869 757,50M
20871 754,50M
20872 754,50M
20878 755,50M
20879 758,50M
20881 759,50M
20882 754,50M
20884 760,50M
20899 759,50M
20900 747,50M
20901 754,50M
20902 754,50M
20907 755,50M
20908 755,50M
20910 755,50M
20918 747,50M
20920 755,50M
20923 753,50M
20924 753,50M
20927 760,50M
20928 760,50M
20929 752,50M
20930 752,50M
20932 755,50M
20937 752,50M
20938 752,50M
20945 755,50M
20946 752,65M
20947 752,50M
20948 759,50M
20950 752,676M
20953 755,52M
20954 756,676M
20955 752,65M
20958 756,50M
20959 754,50M
20960 758,67M
20963 747,48M
20970 754,676M
20977 755,676M
20978 754,50M
20979 752,67M
20983 755,52M
20985 761,347M
20989 755,52M
20994 755,50M
20995 752,50M
20998 755,52M
21002 754,50M
21007 754,52M
21008 754,50M
21010 754,52M
21011 754,52M
21012 756,67M
21016 754,52M
21018 754,52M
21025 753,65M
21027 760,67M
21029 755,52M
21030 755,52M
21033 756,67M
21034 753,65M
21039 759,52M
21040 754,52M
21041 756,48M
21054 754,52M
21055 752,50M
21066 753,676M
21067 755,52M
21068 755,50M
21071 759,67M
21076 759,65M
21077 747,50M