title: SQL: Filtering on a computed column
body: <p>I have a problem that can't be that hard to solve, but I'm having problems with it.</p>
  
  <p>A small table</p>
  
  <pre><code>create table tbl1
    ( a INT AUTO_INCREMENT NOT NULL UNIQUE PRIMARY KEY,
      b VARCHAR(100)
    );
  </code></pre>
  
  <p>and some data</p>
  
  <pre><code>insert into tbl1 
    (b) values
      ("/some/path/1"), 
      ("/some/path/2"), 
      ("/some/longer/path/1"),  
      ("/some/longer/path/2"),
      ("/some/way/1"),
      ("/some/longer/way/2");
  </code></pre>
  
  <p>The I want to query like this:</p>
  
  <pre><code>select length(b) - length(replace(b, '/', '')) no_dirs, b
    from tbl1
    where b like "%path%"
       and no_dirs = min(no_dirs);
  </code></pre>
  
  <p>which should return all rows where no_dirs has its miminum value. The last condition does not work. </p>
  
  <p>I also fiddled with a subquery, but could not get that to work. </p>
  
  <p>Suggestions?</p>