Reputation: 31
I am trying to filter results where the relation is in multiple rows and so I need to first get the ID by matching a value and then use the query again to fetch value from another row for the retrieved value from the same table.
ID - MetaKey - MetaValue
142398 - _wc_checkout_add_on_id - 4
142398 - _wc_checkout_add_on_value - wife
142398 - _wc_checkout_add_on_label - Wife
So I get ID via matching key and value from row 1, now within the same query I want to get metavalue of 2nd and 3rd row to generate the final results, I am unable to do so at the moment.
Can anyone suggest please?
Upvotes: 1
Views: 72
Reputation: 30819
You can do it with sub query, e.g.:
SELECT *
FROM table
WHERE id IN (
SELECT id FROM table WHERE metaKey = ? AND metaValue = ?
);
This will give you all the records belonging to that id. Now, if you don't want the first row in the result then you ca exclude itm, e.g.:
SELECT *
FROM table
WHERE id IN (
SELECT id FROM table WHERE metaKey = ? AND metaValue = ?
)
AND metaKey <> ? AND metaValue <> ?;
Upvotes: 2
Reputation: 15057
something like this will work:
SELECT t2.*
FROM yourTAble t1
LEFT JOIN yourTable t2
ON t1.id = t2.id
AND NOT t2.MetaKey = t1.MetaKey
WHERE t1.id = 142398;
Upvotes: 1