title: For each row of a table run function with every row of another table
body: <p>I have a table of points of type <code>GEOGRAPHY</code>, call it <code>T_points</code>:</p>
  <pre><code>id   point
  1    POINT(-73.964736938476562,  40.7670783996582)
  2    POINT(-73.962371826171875,  40.773128509521484)
  3    POINT(-73.989463806152344,  40.767578125)
  4    POINT(-73.978683471679688,  40.765167236328125)
  ...
  </code></pre>
  <p>and a table of polygons, also of type <code>GEOGRAPHY</code>, call it <code>T_zones</code>:</p>
  <pre><code>id  zone
  1   POLYGON((-74.1856319999999 40.6916479999999, -74.185911 40.6914499999999, ... ))
  3   POLYGON((-73.848596761 40.8716707849999, -73.849876452 40.8721018809999, ... ))
  18  POLYGON((-73.8844286139999 40.8668003789999, -73.883625181 40.8672575879999, ... ))
  20  POLYGON((-73.8839239579998 40.8644177609999, -73.883960024 40.8645497189999, ... ))
  ...
  </code></pre>
  <p>With the function <code>ST_COVERS</code> I can find out if point X is in zone Y by:</p>
  <pre><code>ST_COVERS(POLYGON((-73.8839239579998 40.8644177609999, -73.883960024 40.8645497189999, ... )),
            POINT(-73.964736938476562,  40.7670783996582),
           )
  </code></pre>
  <p>This function returns <code>TRUE</code> if it's in the zone and <code>FALSE</code> if not.</p>
  <p><strong>Objective</strong>: Find which zone each point is in.</p>
  <p><strong>How to do that</strong>: For each point in <code>T_points</code> run this <code>ST_COVERS</code> with each zone in <code>T_zones</code> and return the <code>T_zones.id</code> of the zone that the point is in.</p>
  <p>Can someone recommend an efficient way of implementing this?</p>
  <p>Something like</p>
  <pre><code>SELECT get_zone_id(point, T_zones.zones)
  FROM T_points
  </code></pre>
  <p>would be ideal but no idea how to do that.</p>
  <p>Ideal output:</p>
  <pre><code>id   point                                            zone_id
  1    POINT(-73.964736938476562,  40.7670783996582)    1
  2    POINT(-73.962371826171875,  40.773128509521484)  3
  3    POINT(-73.989463806152344,  40.767578125)        24
  4    POINT(-73.978683471679688,  40.765167236328125)  77
  </code></pre>
  <p>(the <code>zone_id</code> values there are made up)</p>