Mysql insert into select max
The WAY to get the id is by using mysql_insert_id() or the mysql SQL function LAST_INSERT_ID(). 처음에는 저도 방법이 정확히 떠오르지 않아 구글링을 했지만 간단명료하게 나와있는 글이 없어 시간을 좀 뺐겼던 기억이 납니다. mysql은 oracle과는 달리 서브쿼리의 from절과 업데이트 target 모두를 같은 테이블로 사용할 수 없다는 룰이 존재합니다. insert into entry (cn, id) values ((SELECT max(a.cn)+1 FROM entry a), 'test'); 이런식으로 entry 테이블의 뒷에 alias를 설정하면 되실껍니다 INTO @v1, @v2, @v3,... FROM table_name WHERE condition; In this syntax: c1, c2, and c3 are columns or expressions that you want to select and store into the variables. MySQL SELECT INTO Variable syntax. Second, here is the simplest of workarounds: instead of using: insert into foo(lfd) values((select max(lfd) from foo)+1) just do insert into foo(lfd) select (max(lfd)+1) from foo; 1. select 한 내용의 전체 컬럼 Insert [crayon-5eebe314d3c93134235421/] 당연한 이야기지만 selec… UPDATE jobs SET worker_id=(SELECT MAX(worker_id)+1 FROM jobs) WHERE jobname='Abc' The idea behind this is: if the current highest worker_id in my jobs table is 14, this query would set the worker_id for my job 'Abc' to 15. Take care, if using mysql_insert_id() you should provide the resource returned by the mysql_connect, not the resultset returned by mysql_query. For example: INSERT INTO suppliers (supplier_id, supplier_name) SELECT account_no, name FROM customers WHERE customer_id < 5000; By placing a SELECT statement within the INSERT statement, you can perform multiples inserts quickly. We have this limitation now, because such query was illegal in SQL-92; however as further SQL standards (SQL-99) now allow it we have it on our TODO.

Select 한 내용을 그대로 Insert 시키는 방법과 약간 응용하는 법을 소개합니다. You can also create more complicated MySQL INSERT statements using sub-selects. See Section 5.1.11, “Server SQL Modes ” ... INSERT INTO tbl_name (a,b,c) VALUES(1,2,3), (4,5,6), (7,8,9); Each values list must contain exactly as many values as are to be inserted per row. SELECT '1005', '포도', ISNULL((SELECT MAX(display_order) + 1 FROM TBL_FRUITS), 1) INSERT + VALUES 조합을 사용하지 않고, INSERT + SELECT 를 사용했습니다. mysqlで商品番号などに使う連番を作成するにはauto_incrementが最も簡単ですが、自前で連番を作成したい場合はどうすれば良いでしょうか。たとえば、次のような商品データがあります。次に追加する商品のidはどのように生成すればよいで SELECT 를 통해서 나온 결과 레코드를 바로 INSERT 하는 방식입니다.