Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you are looking for displaying the count for each of the property values the <code>partments</code>, <code>houses</code>, <code>freeholds</code>, you should add a <code>GROUP BY property_type</code></p> <pre><code>SELECT property_type, COUNT(*) FROM $wpdb-&gt;postmeta WHERE meta_value BETWEEN 50000 AND 74999 GROUP BY property_type </code></pre> <p><strong>Update:</strong> You have to <em>pivot</em> your rows into columns in order to get the data the way you described like so:</p> <pre><code>SELECT t.property_type, COUNT(t.post_id) AS 'type count' FROM ( SELECT t.post_id, MAX(CASE WHEN t.meta_key = 'property_type' THEN t.meta_value END) AS property_type, MAX(CASE WHEN t.meta_key = 'price' THEN CAST(t.meta_value AS DECIMAL) END) AS Price FROM postmeta t GROUP BY t.post_id ) t WHERE t.property_type = 'apartment' AND t.Price BETWEEN 600000 AND 700000 GROUP BY t.property_type </code></pre> <p>Note that the <code>meta_value</code> field is supposed to store both a string values for property types as well as numeric values for <code>price</code>s so I <code>CAST</code> it to <code>DECIMAL</code> in order to select it as numeric data type so that you can perform calculation on it later on. I also used <code>MAX</code> as an aggregate function as a work around to pivot the rows.</p> <p>Here is a <a href="http://sqlfiddle.com/#!2/59a1d/5" rel="nofollow noreferrer"><strong>demo in SQL fiddle</strong></a>.</p> <p>This what you usually encounter in such a design model which is what they called <a href="http://en.wikipedia.org/wiki/Entity%E2%80%93attribute%E2%80%93value_model" rel="nofollow noreferrer"><strong>Entity–attribute–value model</strong></a>. You can find a lot of useful thread about this data model under the tag <a href="/questions/tagged/eav" class="post-tag" title="show questions tagged &#39;eav&#39;" rel="tag">eav</a>. You can also find more useful pages about this design model in <a href="https://stackoverflow.com/a/8609588/722783">the following answer</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload