I have a table to store contacts.
I want to grab the max value of a column where adding user_id is {some number} and set that as the same column value for the current inserting record.
I'm using prepared statements:
pg_prepare($db, "add", 'INSERT INTO '.CONTACTS.' (c_user_serial,c_name,c_company,c_email) VALUES ($1, $2, $3, $4)');
$insert_co = pg_execute($db, "add", array({(MAX OF c_user_serial where c_user_id = 1234) + 1 increment },$name,$company,$email));
c_user_id
is the ID of the user who is adding this contact, there is another column as index (id
) that is a common serial
column that increments for every row, c_user_serial
is a serial number that increments per user. Let's say one user added one contact so it is 1
. After other users added many contacts, when this user adds his second contact I want this column to store 2
, so an auto increment kind of column but that should increment per user.
Not sure how to use inner queries here to get the max value of the column and use the incremented value for the current insertion.