0

I need to update 3 fields in a mySQL database table programmaticaly. I need to update label_id, label & company_id

What I need is an SQL query that allows me to indicate what the company_id is and the "label" fields and then have it generate the label ids automatically.

For example, create label="test", company_id="17"......and have it automatically generate the label_id. Any ideas on an sql query to do this? Table structure example:

label_id label company_id 1 Cook 8 2 Chef 8 3 Driver 9 
3
  • UPDATE yourtable SET field=value, field=value WHERE field=value? Commented Nov 27, 2012 at 21:12
  • Thanks. But how will it know to create the label_id values? Commented Nov 27, 2012 at 21:13
  • 1
    Maybe use a trigger on insert. Once the first two values are inserted you could then generate and insert the company_id, possibly based off of the first two values: Cook 8 Cook_8 (or Cook8, C8, etc). Commented Nov 27, 2012 at 21:18

1 Answer 1

2

If you're needing the label_id to be generated automatically, it sounds like you're talking about an INSERT rather than an actual UPDATE

INSERT INTO table_name(label, company_id) VALUES ("Company", 5)

This would require your table to be created, such as:

CREATE table_name (label_id int PRIMARY KEY AUTO_INCREMENT, label VARCHAR(255), company_id INT) 
Sign up to request clarification or add additional context in comments.

1 Comment

Agree with Rawkode. Also, your company_id value should be a foreign key tied to the Primary key of the Company table.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.