简体   繁体   中英

Can I add a variable value in mySQL query to table?

I have a table in my database in which I have three fields: Field_1 , Field_2 , Field3 .

Field_1 is auto-incremented integer. Field_2 will be a string value provided by the user. Field_3 should be a concatenation of Field_2 and Field_1 .

Example: (1, Dog, Dog1) should be added to my table without me providing the "Dog1" input.

How to do this? I want to make Field_3 my primary key. I have tried to look for solutions online, but to no use. Sorry, if this is a very easy question.

How is your values being provided? If you are having an insert command you could do something like:

INSERT INTO table (Field_2, Field_3) VALUES (value2, CONCAT(value2, (SELECT MAX(Field_1) FROM table) +1 ))

I am not 100% for the +1 increase you need to check if that works or if you need to do a set before. (dont have an SQL server available right now to check it for you))

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