Create MySQL column with string variable from anot

2019-09-15 19:35发布

问题:

What is a way that I could add a column to my table that would include the docID, plus a bunch of static information around it?

For example

docID    |    topic   | docURL
________________________________________________________
1        |   Floods   | http://site.com/downloaddoc.php?docID=1

2        |    Etc..   | http://site.com/downloaddoc.php?docID=2

I know it seems convoluted to do it this way, but we have to have this particular table downloadable into a CSV to import the data into other programs that use Excel as an input.

回答1:

You could try this:

CREATE TRIGGER upd BEFORE INSERT ON table
FOR EACH ROW
BEGIN
 SET NEW.docURL = CONCAT('http://site.com/downloaddoc.php?docID=',NEW.docID);
END;


回答2:

After every INSERT you could make an

UPDATE table SET docURL = CONCAT('http://site.com/downloaddoc.php?docID=',docID)


回答3:

I'm not sure I have understood but I think you could use concat function in order to concatenate strings and then use a select into outfile to export into csv without changing your table structure.