G
G
GM2mars2014-05-23 10:49:38
PHP
GM2mars, 2014-05-23 10:49:38

How to correctly compose a mysql query with a subquery to insert a new row?

Need to insert a new row into the table, but one of the row's values ​​must be +1 to the maximum value of the "id_category" column in the specific user's table.
For example, if the table were not locked, then the query would be:

INSERT INTO category (
  name, 
  id_user, 
  id_category, 
  created
) VALUES (
  'новая категория', 
  1, 
  (SELECT MAX(id_category )+1 
  FROM category 
  WHERE id_user=1), 
  NOW()
)

How to make a correct request?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilya Lesnykh, 2014-05-23
@Aliance

autoincrement? no, have not heard!

G
GM2mars, 2014-05-23
@GM2mars

Went the other way. The question is irrelevant.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question