Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6846691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:39:16+00:00 2026-05-27T00:39:16+00:00

Confusing title…my apologies. What I’ve got is a table with two related rows. I

  • 0

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');
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:39:17+00:00Added an answer on May 27, 2026 at 12:39 am

    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:

    SELECT pm2.meta_value 
      FROM postmeta pm1
      JOIN postmeta pm2
        ON pm1.post_id = pm2.post_id
       AND SUBSTRING(pm1.meta_key,1,LENGTH(pm1.meta_key)-5) = SUBSTRING(pm2.meta_key,1,LENGTH(pm2.meta_key)-12)
       AND pm1.meta_key like '%_name'
       AND pm2.meta_key like '%_description'
     WHERE pm1.meta_value = 11
       AND pm1.post_id = 661
    

    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 _name and one ends with _description.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Apologies for the rather confusing title, I'm finding it rather difficult to phrase this
The title sounds confusing.. but im sure this is possible! I have one button,
My apologies if the title of this thread is a little confusing. What I'm
I'm confusing. All I want to do is setting title one to the viewcontroller
Apologies for the confusing title, didn't know how to express it better. This is
the title is confusing sorry, if you can think of a better one, please
Assuming two columns in a MySQL (MyISAM) table: title VARCHAR(1500) url VARCHAR(155) I want
sorry for the confusing title. I have two tables, individu and orgcont . individu
Confusing title, hopefully some code will clarify: struct MyNestedType { void func(); }; struct
sorry for the confusing title, its really hard for me to explain what i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.