Trouble with MAX(col)+1 INSERT into same MySQL table - mysql

I need two id columns in the same table that create unique values upon insert. Since MySQL allows only one column to auto-increment, I need to do something like max(id)+1 for the other column.
Here's the SQL I expected to work:
INSERT INTO invoices (invoiceid)
VALUES ((SELECT MAX(invoiceid)+1 FROM invoices))
The select statement works independently, but within my INSERT, it's not allowed. I get the error : You can't specify target table 'invoices' for update in FROM clause

You want to use INSERT INTO .... SELECT FROM instead of INSERT INTO...VALUES():
INSERT INTO invoices (invoiceid)
SELECT MAX(invoiceid)+1
FROM invoices
My question for you would be why are you not use an AUTO INCREMENT field to generate the invoiceid value? That is what it is for, then you will not have to create this when inserting data.

Related

get the last ID after insert in mysql

how can i get the ID of the last insert statement
im using trigger to create a ID for every record
INSERT INTO table1_seqdocument VALUES (NULL);
SET NEW.tracknum = CONCAT('DOC', LPAD(LAST_INSERT_ID(), 3, '0'));
and i need that ID for other table
this is this my insert statement
INSERT INTO tble_transaction
(
tracknum
,signatoryid
,signed
,status
,signatorylevel
)
VALUES
(?,?,?,?,? )
what i want is to retrieve the ID and use it for another insert statement but using other table. is it possible? thank you
You can use ##identity
SELECT ##IDENTITY AS [##IDENTITY];
LAST_INSERT_ID() can only tell you the ID of the most recently auto-generated ID for that entire database connection, not for each individual table, which is also why the query should only read SELECT LAST_INSERT_ID() - without specifying a table. As soon as you fire off another INSERT query on that connection, it gets overwritten. If you want the generated ID when you insert to some table, you must run SELECT LAST_INSERT_ID() immediately after doing that (or use some API function which does this for you).
If you want the newest ID currently in an arbitrary table, you have to do a SELECT MAX(id) on that table, where id is the name of your ID column. However, this is not necessarily the most recently generated ID, in case that row has been deleted, nor is it necessarily one generated from your connection, in case another connection manages to perform an INSERT between your own INSERT and your selection of the ID.
(For the record, your query actually returns N rows containing the most recently generated ID on that database connection, where N is the number of rows in table1.)

insert multiple ID in mysql row

Is it possible to insert multiple data in a single mysql row?
Example of multiple id:
41,32,31,293,877
Yes it is possible to insert the multiple values at a time in mysql database.
suppose you have created a table of name emp. and in this table you have 1 field which is named as id.
now you just want to insert the multiple id in a table named emp , so can do this by writing following command ->
INSERT INTO emp(id) VALUES(1),(2),(3),(4),(5),(6);
Above query will insert the following given values in a table.
If you declare column to be VARCHAR().
yes you can insert multiple IDs. like
INSERT INTO table (Id) value ('41,32,31,293,877');
If you are using varchar or text data type for Id field then it is possible to enter and store with comma separated.
INSERT INTO TABLENAME (Id) VALUES ('41,32,31,293,877');
If you are inserting into a single table, you can write your query like this :
insert into table (id) values (41),(32),(31),(293),(877);

How to insert a value from a select statement into a table along with a hardcoded value

I have a table called ROLES with ROLEID and ROLEDESC as the columns.
I am trying to get the max ROLEID from ROLES table and store it in another table along with a hardcoded value say 'NEWROLEDESC'
The new table(viz. 'DROLES') structure is: DID and DROLEDESC where DROLEDESC is to be populate with a hardcoded value and DID is to populated with a value from the select statement : SELECT MAX(ROLEID)+1 as maxroleid FROM ROLES
I have tried :
insert into DROLES(DID) SELECT MAX(ROLEID)+1 FROM ROLES
now,the above inserts the max ID in the DID, but when I try
insert into DROLES(DID,DROLEDESC)
values ((SELECT MAX(ROLEID)+1 FROM ROLES),'NEWROLEDESC')
It doesn't work.Ofcourse the SQL grammer is not correct. Is there any way to achieve it/The correct SQL Syntax?
NOTE: I am just writing this as an experiment. I know AUTO increment would do the trick.Anything apart from that?
Reinventing AUTO_INCREMENT(MySQL)/SEQUENCE/IDENTITY(SQL Server) is not the best way to go and you may end up with incorrect values with concurent queries!
The INSERT ... SELECT syntax is:
INSERT INTO DROLES(DID, DROLEDESC)
SELECT MAX(ROLEID)+1, 'NEWROLEDESC'
FROM ROLES;
Keep in mind that if you insert multiple times to DROLES and ROLES is not chaning you will get the same value in DID column.

Database INSERT INTO SET ... WHERE(SELECT ...)

I am trying to insert my values into table if Admin_User_Role_Id value against Admin_Id is not present in the table. Is it possible to insert!
My Table Structure:
Admin_User_Id (FK)
Admin_User_Role_Id (FK)
Is_Enabled (boolean flag)
Query which I tried, but not success
INSERT INTO role_association
SET Admin_User_Id=61, Admin_User_Role_Id=2, Is_Enabled=0
WHERE Admin_User_Role_Id
NOT IN
(SELECT Admin_User_Id, Admin_User_Role_Id FROM role_association)
I think it is possible but my logic is wrong. How should I manage this query to work successfully!
INSERT syntax cannot have WHERE clause. The only time you will find INSERT has WHERE clause is when you are using INSERT INTO...SELECT statement.
Probably you take care about where condition in your programming logic and write a simple insert statement and the depending on your logic use this statement to insert the records. Hope you got my point.
You want to insert your values in your table using this query for your reference
INSERT INTO Yourtablename(column1,column2,column3,...)
VALUES ('value1','value2','value3',.....);
You want to ADD one new column in your table means using this query
** ALTER TABLE table_name ADD column_name datatype**

INSERT and then SELECT the inserted row

My table Structure,Table Name- user_tb
User_Id Name Mob_No City
=================================
100 Sumith 34542 dfsc
101 Yadhu 35485 dfgd
102 Aby 34234 jhhg
Here column User_Id is auto_increment
and am inserting values to this table using this query,
insert into user_tb(Name,Mob_No,City) values('Yadhu',34542,'dfsc');
I need to get User_Id of the Person at the time of insertion
ie, when am inserting using this query,
insert into user_tb(Name,Mob_No,City) values('Yadhu',34542,'dfsc');
i need to get User_id-101
Is this possible....
Anyone please help me to complete this.
Obtaining the value of column that uses AUTO_INCREMENT after an INSERT statement can be achieved in a number of different ways. To obtain the value immediately after an INSERT, use a SELECT query with the LAST_INSERT_ID() function.
For example, using Connector/ODBC you would execute two separate statements, the INSERT statement and the SELECT query to obtain the auto-increment value.
insert into user_tb(Name,Mob_No,City) values('Yadhu',34542,'dfsc');
SELECT LAST_INSERT_ID();
If you do not require the value within your application, but do require the value as part of another INSERT, the entire process can be handled by executing the following statements:
insert into user_tb(Name,Mob_No,City) values ('Yadhu',34542,'dfsc');
insert into tbl2 (id,text) VALUES (LAST_INSERT_ID(),'text');
In Mysql: use LAST_INSERT_ID() function; http://dev.mysql.com/doc/refman/5.0/en/mysql-insert-id.html
If PHP: use mysql_insert_id() to get the last id inserted by your query.
http://php.net/manual/en/function.mysql-insert-id.php