简体   繁体   中英

Inner SQL statement while inserting values in a table

I have a table called addresses_users which maps address to the users. Table structure is.

------------------------------------------
id  | projectid   | crmconfigid  | order     |
------------------------------------------
1   |    1        |    1         |   1       |
------------------------------------------
2   |    2        |    1         |   3       |
------------------------------------------
3   |    3        |    1         |   2       |
------------------------------------------

This is the table structure with some existing data, what I want to do is, I want to update priority of the address to the MAX(order)+1 of the user while inserting new record.

I am trying a sql

INSERT INTO projectcrm VALUES (1,1, (SELECT MAX(order)+1 FROM projectcrm WHERE projectid = 1));

For example :

INSERT INTO projectcrm SELECT 1, 1, MAX(order) FROM projectcrm WHERE projectid = 1 is not working for me.`

Giving error : Error Code: 1136 Column count doesn't match value count at row 1

Maybe it does not count MAX(order) as a column.

Please try the following query:

INSERT INTO addresses_users (projectid, crmconfigid, order)
SELECT 1, 1, MAX(order) + 1
FROM addresses_users
WHERE projectid = 1

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