Site icon Hip-Hop Website Design and Development

How to get specific column data conditionally in database in WordPress?

I have unique request_id and similar item_id columns in database.
Therefore, the same item_id can be stored more than once.

I’m trying to get the request_id of the last item_id I specified among the records. I tried various queries but none of them work. I would be glad if you show me the way.

global $wpdb;
$result = $wpdb->get_results("SELECT request_id FROM {$wpdb->prefix}my_table WHERE id=(SELECT max(id) and item_id=$last_item_id FROM {$wpdb->prefix}my_table) LIMIT 1");
         
$result2 = $wpdb->get_results("SELECT TOP 1 request_id FROM {$wpdb->prefix}my_table WHERE item_id=$last_item_id ORDER BY request_id DESC");