I am doing a query on a wordpress table (postmeta). The table has keys and values and I need a query that will get all rows that match "key1" equal to "value1" and "key2" equal to "value2" ordered by value2
The table basically has an id, postid, key and value columns.
I am not sure even where to start. I can find one value fine ie ... where key='featured' & value=true. But I need the top 25 ordered by the value of the rows where key='hits' meaning I need the value of the corresponding hits key for those featured rows
I am not sure how to do this.
TIA
It is difficult to say exactly how to do this with the limited details that you provided. But when you want to return key/value pairs you can use the following.
You can join on your table multiple times:
See SQL Fiddle with Demo
Or you can use an aggregate function with a
CASE
expression (usingsum()
assumes a numeric value, you can usemax()
/min()
for string values:See SQL Fiddle with Demo