title: List of tables that a user has SELECT privilege for in MySQL
body: <p><strong>Short version</strong>: How can I write an SQL procedure to list which of several tables in a MySQL database a particular user has access to?</p>
  
  <p><strong>Longer version</strong>:</p>
  
  <p>I'm writing a multi-user app that accesses a database with data for several branches of a company. The database has a number of lookup tables that any user can access, and a table for each branch that only authorized users can access. My strategy is:</p>
  
  <ul>
  <li>Write a stored procecure that returns a list of the relevant tables for which the user has SELECT privilege.</li>
  <li>From the app, call the procedure. If there's only one table returned, use it, otherwise let the user select which branch they want to access (e.g. for managers).</li>
  </ul>
  
  <p>I'm having trouble figuring out how to write such a stored procedure. <code>SHOW GRANTS FOR CURRENT_USER</code> is an obvious possibility, but parsing something like:</p>
  
  <pre><code>GRANT SELECT ON Company.BranchABC TO 'auser'@'%clientdomain.com'
  </code></pre>
  
  <p>in SQL to figure out what the tables are seems <em>way</em> too messy. Doing a <code>SELECT</code> from the actual tables that hold the permissions also seems problematic, because I'd have to duplicate MySQL's logic for combining the permissions from the various tables (user, db, host, etc.)</p>
  
  <p>Any words of wisdom?</p>