My SQL AUTO_INCREMENT on a secondary column - mysql

Is it possible to auto increment a value on a secondary column depending on a another refrence column in MySQL.
Say I have a table as follows, which I need to auto increment the ID value depending on the GRP_ID
+--------+----+---------+
| grp_id | id | name |
+--------+----+---------+
| 1001 | 1 | abc |
| 1002 | 1 | xyz |
| 1002 | 2 | ijl |
| 1002 | 3 | efg |
| 1003 | 1 | hij |
| 1003 | 2 | mno |
+--------+----+---------+

you can use LAST_INSERT_ID() it's defending last the insert auto increment id any table in your mysql db, but you need to insert values first reference table or column in MySQL .bec
there"s the place for id
INSERT INTO table (id, grap_id,name)
VALUES (LAST_INSERT_ID() ,' ', 'Value4name');

Related

Mysql Query for comparing two tables' and inserting the matched in another

I have two tables, say XYZ and ABC
XYZ
| id|
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
ABC
| id| name |
| 3 | rocky|
| 4 | Alex |
Perform ---->
ALTER TABLE XYZ ADD COLUMN name VARCHAR(8);
Now I want to set the values of ABC table in XYZ,like:-
| id| name |
| 1 | null |
| 2 | null |
| 3 | rocky|
| 4 | Alex |
| 5 | null |
I want a single line query??
Assuming id is a primary key:
REPLACE INTO XYZ SELECT * FROM ABC
If id isn't a primary key or a unique key, a multi-table update like:
update XYZ JOIN ABC USING (id) SET XYZ.name=ABC.name
ref: fiddle

MySQL insert skipped IDs

I have a mysql table which has two columns, id and value.
id(auto_increment primary key)
value(varchar 255)
insert into table columns(`id`,`value`)VALUES(,'something1');
insert into table columns(`id`,`value`)VALUES(,'something2');
output
+----+---------------+
| id | value |
+----+---------------+
| 1 | something1 |
| 2 | something2 |
+----+---------------+
Now Inserting one value again
insert into table columns(`id`,`value`)VALUES(8,'something8');
Updated Table
+----+---------------+
| id | value |
+----+---------------+
| 1 | something1 |
| 2 | something2 |
| 8 | something8 |
+----+---------------+
Now I am inserting one value again
insert into table columns(`id`,`value`)VALUES(,'something');
Final Output
+----+---------------+
| id | value |
+----+---------------+
| 1 | something1 |
| 2 | something2 |
| 8 | something8 |
| 9 | something |
+----+---------------+
But I want the final output like this
+----+---------------+
| id | value |
+----+---------------+
| 1 | something1 |
| 2 | something2 |
| 8 | something8 |
| 3 | something |
+----+---------------+
Now id is 3 and further insertion will create id 4,5,6,7,9 and so on.
Is there any way to get achieve this ?
I know it is old post but mybie it will help someone :)
I am afraid it cannot be done automatically, however, I solved it to my client, maybe some of you could find it useful:
DECLARE FirstEmptyId int;
SELECT l.id +1 AS
START
FROM TableName AS l
LEFT OUTER JOIN TableName AS r ON l.id +1 = r.id
WHERE r.id IS NULL
LIMIT 1 INTO FirstEmptyId;
Insert Into TableName (Id..) Values (FirstEmptyId...)
STILL, YOU HAVE TO MIND THAT IT IS NOT ADVISABLE TO INSERT ID VALUE OTHER WAY THAN BY AUTOINCREMENT IT AND THERE IS A REASON FOR THAT :)
Consider creating the second column just for performance purposes.
Good luck fellow coders!

Mysql->autoincrementing related to an other field

I am stuck on a project design. One of the table has 1-M relation with users table. So it has a foreign key. Same field is also primary key.
Table as follows
Itemid:
Primarykey
Autoincrement
Useriditem:
Primarykey
Foreign key of id in users table
Itemname:
Not null
Values:
-----------------------------------------
| **ITEMID** | **USERID** | ITEMNAME |
-----------------------------------------
| 1 | 1 | fooooooo |
-----------------------------------------
| 2 | 1 | tinytext |
-----------------------------------------
| 3 | 1 | MediumText |
-----------------------------------------
| 4 | 2 | LARGEtext |
-----------------------------------------
| 5 | 2 | HUGETEXT |
-----------------------------------------
| 6 | 1 | BLOOOOOB |
-----------------------------------------
| 7 | 3 | 001010101 |
-----------------------------------------
This is the result of the current design. What i am wondering is that a way to make auto increment for each user separately.
Something like "Autoincrement item id GROUP BY user id"
-----------------------------------------
| **ITEMID** | **USERID** | ITEMNAME |
-----------------------------------------
| 1 | 1 | fooooooo |
-----------------------------------------
| 2 | 1 | tinytext |
-----------------------------------------
| 3 | 1 | MediumText |
-----------------------------------------
| 1 | 2 | LARGEtext |
-----------------------------------------
| 2 | 2 | HUGETEXT |
-----------------------------------------
| 4 | 1 | BLOOOOOB |
-----------------------------------------
| 1 | 3 | 001010101 |
-----------------------------------------
Is there a way to do this using mysql?
You want something like this:
Demo
CREATE TRIGGER item_id_auto_inc
BEFORE INSERT ON items
FOR EACH ROW
BEGIN
SET NEW.item_id := (SELECT CASE WHEN ISNULL(MAX(item_id)) THEN 0 ELSE MAX(item_id) END +1 FROM items WHERE user_id=NEW.user_id);
END
//
I dont know what happens when multiple users execute queries but i think i managed to narrow down the algorithm.
how to "insert into table (col1, col2) values (select max(id) from table2, select id from table3); "?
INSERT INTO table VALUES (
(SELECT (MAX(itemid)+1)
FROM table
WHERE userid = 'theid') , 'theid' , 'foo1');
Can this solve the simultaneous execution by multiple user problem.
The safer way to do this is taking into account that your application can get hit by more than 1 user at any given time
START TRANSACTION;
Insert into table A
Select Last inserted id from table A using Last_Insert_ID()
Update table B
COMMIT;
At least you are guaranteed to get this last inserted id from table A into table B.

MySQL copy column from different tables, insert at top of table

I'm trying to merge a couple tables together to consolidate the data, but when I try to insert a column from one table to the other, the query I'm using inserts the records after the last currently existing record in the table. There are a ton of questions about duplicating columns, but they all seem to be starting with an empty table.
INSERT INTO newTable( newColumn ) SELECT oldColumn FROM oldTable
How do I modify this query to insert the rows at the beginning of the table instead of the end?
Visual representation of what is happening (left) vs. what I want to happen (right):
+--------+--------+------------+ +--------+--------+------------+
| ID | Column | newColumn | | ID | Column | newColumn |
+--------+--------+------------+ +--------+--------+------------+
| 1 | 12345 | | | 1 | 12345 | 12345 |
| 2 | 12345 | | | 2 | 12345 | 12345 |
| 3 | 12345 | | | 3 | 12345 | 12345 |
| 4 | | 12345 | +--------+--------+------------+
| 5 | | 12345 |
| 6 | | 12345 |
+--------+--------+------------+
As mentioned in the comments you need an UPDATE statement not an INSERT statement:
UPDATE newTable
JOIN oldTable
ON newTable.id = oldTable.id
SET newcolumn = oldcolumn;
A tested example may be seen here: http://sqlfiddle.com/#!2/77724/1

mysql insert on duplicate FIELD instead of KEY

-------------------------------------
| user_id | user_name | user_visits |
-------------------------------------
| 1 | foo | 5 |
-------------------------------------
| 2 | bar | 12 |
-------------------------------------
user_id: auto increament, user_visits: default 1
INSERT INTO table (user_name) VALUES ('baz'), ('bar'), ('qux');
the above statement will of course insert 3 new records, as the result:
-------------------------------------
| user_id | user_name | user_visits |
-------------------------------------
| 1 | foo | 5 |
-------------------------------------
| 2 | bar | 12 |
-------------------------------------
| 3 | baz | 1 |
-------------------------------------
| 4 | bar | 1 |
-------------------------------------
| 5 | qux | 1 |
-------------------------------------
but what I'm trying to achieve is:
-------------------------------------
| user_id | user_name | user_visits |
-------------------------------------
| 1 | foo | 5 |
-------------------------------------
| 2 | bar | 13 |
-------------------------------------
| 3 | baz | 1 |
-------------------------------------
| 4 | qux | 1 |
-------------------------------------
so literally,
if field user_name exists, update user_visits, else insert a new record.
is it possible to achieve this with a single insert statement?
Sure there is but it has nothing to do with your insert statement. You need to add a unique index on the user_name column:
create unique index user_name_idx on yourtable (user_name);
Then afterward in your code that tracks the count will have to decide whether to do an insert or an update.
You have to create a key for your username field and then use INSERT ON DUPLICATE query to update the columns values.
For example your query must be,
INSERT INTO table (user_name) VALUES ('baz'), ('bar'), ('qux')
ON DUPLICATE KEY UPDATE user_visits=user_visits+1;
For further reference visit http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
You could leave your INSERT statement as is and implement a trigger that handles your special actions.
See: An Introduction To Triggers