Confusing title…my apologies.
What I’ve got is a table with two related rows. I need to get the value of a column in one row based on the value of a column in another row.
Given the following postmeta table:
+----------+------------+---------------------------------------------------+--------------------+
| meta_id | post_id | meta_key | meta_value |
+----------+------------+---------------------------------------------------+--------------------+
| 6917 | 661 | member_categories_0_member_categories_name | 11 |
+----------+------------+---------------------------------------------------+--------------------+
| 6918 | 661 | member_categories_0_member_categories_description | First description |
+----------+------------+---------------------------------------------------+--------------------+
| 6919 | 661 | member_categories_1_member_categories_name | 12 |
+----------+------------+---------------------------------------------------+--------------------+
| 6920 | 661 | member_categories_1_member_categories_description | Second description |
+----------+------------+---------------------------------------------------+--------------------+
I need to get the meta_value category description based on the meta_value category ID and the post_id. For instance, if my category ID is 11 and my post_id is 661, I should get “First description”.
I thought about using a subquery to get the meta_key corresponding with a meta_value of ’11’, but I don’t know how to find the description based on the counter inside ‘member_categories_x_member_categories_name’.
Unfortunately, I don’t have control over the name of the meta_key. I got as far as this simple query, which returns ‘member_categories_0_member_categories_name’. How do I use that value to find ‘First description’?
SELECT pm.meta_key
FROM postmeta pm
WHERE pm.meta_value = "11"
AND pm.post_id = 661
Here’s the SQL for the table:
CREATE TABLE `postmeta` (
`meta_id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`post_id` BIGINT(20) UNSIGNED NOT NULL DEFAULT '0',
`meta_key` VARCHAR(255) NULL DEFAULT NULL,
`meta_value` LONGTEXT NULL,
PRIMARY KEY (`meta_id`),
INDEX `post_id` (`post_id`),
INDEX `meta_key` (`meta_key`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=30814;
INSERT INTO `postmeta` (`meta_id`, `post_id`, `meta_key`, `meta_value`) VALUES (6917, 661, 'member_categories_0_member_categories_name', '11');
INSERT INTO `postmeta` (`meta_id`, `post_id`, `meta_key`, `meta_value`) VALUES (6918, 661, 'member_categories_0_member_categories_description', 'First description');
INSERT INTO `postmeta` (`meta_id`, `post_id`, `meta_key`, `meta_value`) VALUES (6919, 661, 'member_categories_1_member_categories_name', '12');
INSERT INTO `postmeta` (`meta_id`, `post_id`, `meta_key`, `meta_value`) VALUES (6920, 661, 'member_categories_1_member_categories_description', 'Second description');
This is ugly, but then, that table is so far from normalized that any answer short of scanning every row is going to be something like this:
The idea is to join the table to itself, linking rows that have the same post_id, and whose meta_key is ‘similar’ — it needs to be exactly the same, except that one ends with
_nameand one ends with_description.