简体   繁体   中英

How to refference a value from the same row in mysql?

I am retrieving a long text blurb for a row. Example: a description for a book. Is there a way that I can reference other values of the same row in the longer text as a form of variable. The idea here being that I don't have to comb through the entire long text output and change every instance of the value where referenced.

Using the same as before: a description which is a column retrieved from the same row as say the title and author of the same book: "$TITLE is a book written by $AUTHOR etc. etc." If for example the same row had a title column and author column with the value of "Huckleberry Fin" and "Mark Twain" and a description column with the value of "$TITLE is a book written by $AUTHOR etc. etc."" .. When the description was retrieved, the description would read "Huckleberry Fin is a book written by Mark Twain etc. etc." Re-Using the same example: If the above row's title were updated to "The Adventures of Huckleberry Finn" without updating the description, which would be a laborious and tedious process that I would like to avoid, when the description is retrieved it would read: "The Adventures of Huckleberry Finn is a book written by Mark Twain"

Maybe this doesn't exist and I would have to make my own engine for it web server side, but it seems like the best option for efficiency would be to have this done in the database and not the web server. Maybe I don't know enough about MySQL to know that this could be achieved using something like a view for instance. Anyway, it seems like this would be a very common thing to have done on the server and I can't imagine that this cannot be done... but then again, maybe I am missing something really simple.

You could use REPLACE :

SELECT title, REPLACE(description, '$TITLE', title) as description FROM books

But it's going to be ureadable if you need to replace multiple placeholders using nested REPLACE expressions:

SELECT title,
    REPLACE(
        REPLACE(description, '$TITLE', title),
        '$MAINCHARACTER',
        main_character
    ) as description
FROM books

I would rather do it in application language. For example with PHP you could use:

$description = str_replace(
    ['$TITLE', '$MAINCHARACTER'],
    [$title, $mainCharacter],
    $description
);

Adding more placeholders wouldn't make it unreadable.

[..] but it seems like the best option for efficiency would be to have this done in the database and not the web server.

I don't see any reason, why any language should do that less efficient than MySQL.

Anyway, it seems like this would be a very common thing to have done on the server

I doubt that. You will find a lot of comments like "Why don't you do that in application language?". At least MySQL has a very limited support for string manipulation.

Are you trying to call different data within the same row? You can use mysql_fetch_row .

If yes, you can add a column of index, like

=============================

index |title |character

1 | Huckleberry Fin | Tom Sawer

2 | ABC | John

==============================

And say you have your table name called "books", Then you do

$handle = mysql_query('SELECT * FROM books WHERE index=1');
$row = mysql_fetch_row($handle);

so $row[0]=1, $row[1]=Huckleberry Fin, and $row[2]=Tom Sawer

You can take a look at this manual .

And you may want to learn how to use escape functions like mysql_escape_string() here .

PS Sorry for the ugly table drawn LOL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM