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 6720623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:14:03+00:00 2026-05-26T09:14:03+00:00

I want a MySQL field to be able to reference the same field in

  • 0

I want a MySQL field to be able to reference the same field in another row and return that field’s value when loaded into PHP.

For instance, if row with id = 1 has value "bar" in column foo, the row with id = 3 should have value "[1]" (or something similar; the 1 is pointing to the row with id = 1) in column foo and MySQL should replace that value with "bar" when returning the array.

I’m not talking about an UPDATE query. I am trying to build a SELECT query that will make the appropriate replacement. I want [1] to be the permanent value of that row, and reflect whatever foo in the referenced row should happen to be.

  • 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-26T09:14:04+00:00Added an answer on May 26, 2026 at 9:14 am

    You could try something like this but without knowing more, your mileage may vary.

    Also, simply use 1, not [1] as the reference value

    SELECT a.id, b.foo
    FROM `foo_table` a
    INNER JOIN `foo_table` b
        ON CAST(a.foo AS UNSIGNED) = b.id
    WHERE a.id = 3 -- or whatever
    

    Update

    I’d be more inclined to make your table design more specific. For example, try this structure

    CREATE TABLE `foo_table` (
        id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
        foo VARCHAR(255),
        parent_id INT NULL,
        FOREIGN KEY (parent_id) REFERENCES `foo_table` (id)
            ON DELETE CASCADE
    ) ENGINE=INNODB;
    

    Then your query can be

    SELECT a.id, COALESCE(b.foo, a.foo) AS foo
    FROM foo_table a
    LEFT JOIN foo_table b
        ON a.parent_id IS NOT NULL
        AND a.parent_id = b.id;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add a new time-field to a an existing MySQL-table that is
In order to be able to simulate substr from PHP and mysql, I want
I have a PHP/MySQL e-commerce site that is placing order detail information into a
I want to display the TIME field from my mysql table on my website,
I have a table in MySQL that has 3 fields and I want to
I want to execute some mysql statements that are stored in a text file
I am building my first PHP/MySQL site and i would like to return search
I want to change the date field in my sql view so that the
Ok, I am storing a Date/Time in a MySQL TEXT field. I want to
I have a field in MySQL that contains following: $a[]=5; $a[]=3; foreach($a as $n)

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.