简体   繁体   中英

Create new column in mySQL using values from other columns

I have two columns in my database I'd like to combine to make a new third column. The first column is a company name and the second is the URL to the company website.

I'd like to make a new column that is the company name hyperlinked to the website:

<a href="http://companywebsite.com>Company Name</a>

Is there a simple way to go about doing this? I'm VERY new to mySQL and can't figure out how I'd even go about doing this.

Bonus points for coming up with a way where when I add a new entry with company name and URL it automatically generates a value for this new hyperlink column.

Thanks.

As Polvonjon write, use CONCAT:

SELECT brief_description, CONCAT('<a href="', link, '">', innovation_name, '</a>') FROM inno_db

By the way - inno_db is a very odd name for a table in the database; it's a particular type of storage engine for MySQL. Do you not think "companies" is a better name?

Creating a new column is a bad idea - you have to keep it updated, and you're duplicating data, which leads to bugs in the long term. Ideally, you use the query to populate your WP screen.

If you can't do that, as the comments recommend, you could create a view, from which you can just do a straight select:

create view WPPlugin
as

select brief_description, 
CONCAT('<a href="', link, '">', innovation_name, '</a>') 
FROM inno_db

in your plug in code, you then do select * from WPPlugin .

Try use CONCAT function:

SELECT company, url, CONCAT('<a href="', url, '">', company, '</a>') FROM companies

But I will not advice to using this sample of getting anchor tag. Try to use VIEW element in your MVC app.

Assuming you have a table with three columns CREATE TABLE mytable ( company_name text , url text , hyperlink text )

Then you would need to UPDATE the value of the third column (hyperlink) based on what you need, something like:

UPDATE mytable
SET    hyperlink = '<a href="http://' || url || '>' || company_name || '</a>';

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