Plugin MySQL SELECT custom data and filter on user meta

Without a proper SQL query in the question is not possible to write a proper query in the response, but following your pseudo-code approach:

SELECT custom-data
  FROM custom-table
  LEFT JOIN wp_users ON custom_data.user_id = wp_users.ID
  LEFT JOIN wp_usermeta ON wp_users.ID = wp_usermeta.user_id
 WHERE some-condition
   AND wp_usermeta.meta_key = 'flag'
   AND wp_usermeta.meta_value="flagvalue"

So basically, you need to join against wp_usermeta, which is where you’d be storing your users “flag”.

error code: 523