I have table with several cols and I'm doing a multiplier insert to it.
ie:
INSERT INTO accounting_positions
KEYS (`date`, `customer`, `type`, `product`, `volume`, `payout`, `count`)
VALUES ('2012-01-10','10','regular','60sec',65,83,13),
('2012-01-10','10','regular','binary',15,83,13)
ON DUPLICATE KEY UPDATE volume=volume+CURRENT_COL_VAL;
what should I write instead of the "CURRENT_COL_VAL"?
if I want to update the duplicate row with a value from my insert.
because I cant place a static number inside it because it is differs with every row insert in the multiple insert
have a look at the mysql docu on insert-on-duplicate From there you can see that you can use something like this:
INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6)
ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b);
in your case:
INSERT INTO accounting_positions
(`date`, `customer`, `type`, `product`, `volume`, `payout`, `count`)
VALUES
('2012-01-10','10','regular','60sec',65,83,13),
('2012-01-10','10','regular','binary',15,83,13)
ON DUPLICATE KEY UPDATE volume=volume+VALUES(volume);
which would add the new and the old value. If you only want the given insert value to overwirite the row then do: ON DUPLICATE KEY UPDATE volume=VALUES(volume)
Related
I am trying to insert 50 values into a DB, the table has two column names, one is an ID column set to auto increment and the other is for a code.
When I attempt to do the insert I get a error.
This is my SQL code:
INSERT INTO Names (Id, Code)
VALUES (NULL, 'CodeHere', NULL, 'CodeHere', NULL, 'CodeHERE' );
Don't include the ID column if it is auto increment and split the input to one one value per time
INSERT INTO Names (Code)
VALUES ('CodeHere'),('CodeHere'),('CodeHERE' );
INSERT INTO Names VALUES ('CodeHere'),('2CodeHere'),('3CodeHere'),('4CodeHere')
just ignore auto increment column.
Use this
INSERT INTO Names (Id, Code)
VALUES (NULL, 'CodeHere'), (NULL, 'CodeHere') ,( NULL, 'CodeHERE' );
I'm using MYSQL script and I want to insert some values in a table.
INSERT INTO `testType` (`name`) VALUES ('URL');
INSERT INTO `testType` (`name`) VALUES ('xD');
INSERT INTO `testType` (`name`) VALUES ('LOL');
But I only want to insert if the table is empty in the first place.
I'm not a SQL guy but basically
if(testType.length() == 0 {
INSERT INTO `testType` (`name`) VALUES ('URL');
INSERT INTO `testType` (`name`) VALUES ('xD');
INSERT INTO `testType` (`name`) VALUES ('LOL');
}
How can I do this the simplest and smallest way possible? Thank you.
EDIT: my question is different. I want to insert ALL THE DATA if the table is empty. not only one insert at the time
First, I would suggest doing this in one step:
INSERT INTO testType(name)
VALUES ('URL'), ('xD'), ('LOL');
Then, you can express this without IF:
INSERT INTO testType(name)
SELECT t.name
FROM (SELECT 'URL' as name UNION ALL
SELECT 'xD' as name UNION ALL
SELECT 'LOL' as name
) t
WHERE NOT EXISTS (SELECT 1 FROM testType);
Finally, if you want to insert these values if each doesn't exist, then you can let the database do the work. First, define a unique constraint/index on the name (if name is not already the primary key), and then use ON DUPLICATE KEY UPDATE:
CREATE UNIQUE INDEX unq_testtable_name ON testtable(name);
INSERT INTO testType(name)
VALUES ('URL'), ('xD'), ('LOL')
ON DUPLICATE KEY UPDATE name = VALUES(name);
You can use stored procedure
DELIMITER //
CREATE PROCEDURE `proc1` ()
BEGIN
SELECT COUNT(*) INTO variable1 FROM testType;
IF variable1 = 0 THEN
INSERT INTO `testType` (`name`) VALUES ('URL');
INSERT INTO `testType` (`name`) VALUES ('xD');
INSERT INTO `testType` (`name`) VALUES ('LOL');
END WHILE;
END //
Try this:
DECLARE #ExistCount INT;
SET #ExistCount = (SELECT COUNT(1) FROM `testType`);
IF #ExistCount<1
THEN
INSERT INTO `testType` (`name`) VALUES ('URL'),('xD'),('LOL');
END IF;
I am using insert on duplicate key update for updating the status of many orders.
However if I enter an id that does not exist, mysql creates a new order.
Is there any way to NOT create a new order?
Here is my query
INSERT INTO
`order`
( id, status )
VALUES (1, 'COMPLETE'), (2, 'COMPLETE'), (3, 'INVALID')
ON DUPLICATE KEY UPDATE
status = VALUES(status)
Add backtracks for table name
INSERT INTO `order`( id, status ) VALUES (1, '43534'), (2, '434'), (3,'345345') ON DUPLICATE KEY UPDATE status = VALUES(status)
I have a table where player character information is kept. I have another table where all possible status affects are kept. I am trying to figure out how to create a relationship where any/all/none of the character entries in the first table can be linked to any/all/none of the status entries in the second table. My research has shown me that I will probably need a third table, and may have to use composite Primary Keys and/or surrogate keys?
So here is an update based on responses. I seem to have working what I was hoping to achieve. Here is the code:
create table characters (
char_id int primary key auto_increment,
name varchar(25)
);
create table health_status (
status_id int primary key auto_increment,
stat_name varchar(15)
);
create table char_status (
char_id int,
status_id int,
primary key (char_id, status_id)
);
insert into characters (name) values ('Godzilla');
insert into characters (name) values ('King Kong');
insert into characters (name) values ('Mecha-Dragon');
insert into characters (name) values ('Chris Hanson');
insert into characters (name) values ('Journey');
insert into characters (name) values ('Lou Diamond Phillips');
insert into characters (name) values ('RedHot');
insert into health_status (stat_name) values ('Bleeding');
insert into health_status (stat_name) values ('Shaken');
insert into health_status (stat_name) values ('Frightened');
insert into health_status (stat_name) values ('Petrified');
insert into health_status (stat_name) values ('Poisoned');
insert into health_status (stat_name) values ('Slowed');
insert into health_status (stat_name) values ('Rushed');
insert into health_status (stat_name) values ('Endowed');
insert into char_status (char_id, status_id) values (1, 1);
insert into char_status (char_id, status_id) values (1, 3);
insert into char_status (char_id, status_id) values (1, 6);
insert into char_status (char_id, status_id) values (2, 2);
insert into char_status (char_id, status_id) values (2, 4);
insert into char_status (char_id, status_id) values (3, 7);
insert into char_status (char_id, status_id) values (6, 8);
insert into char_status (char_id, status_id) values (7, 2);
insert into char_status (char_id, status_id) values (7, 7);
select characters.name, health_status.stat_name
from characters
left join char_status on characters.char_id = char_status.char_id
left join health_status ON health_status.status_id =
char_status.status_id;
I have three questions. Does anyone see any way this could be cleaned up, or a better way to achieve my goal? Also, is having the compound PK in the table char_status really doing anything useful? And finally, Is there a way to organize the output with a query to list the character name only once, followed by all its associated status - or is this something for a different language to do? Thanks for everyone's help!
Based on your structure you say you need to further create new table and put data into it.**
In order for you to connect between tableA and TableB you need to have
a Primary key in one and also its reference in other
**. Many to many relationship is something like a mutual one where tableA depends on many elements of tableB and also vice-versa.
You may have to normalize your tables first based on your situation.
Have a look at Normalization_tuorial
Next also have a look at one-many and many-many etc..
As you found in your research
create a third table with 2 fields that will hold the "id" (main field) from those 2 tables you want to make relation many to many...
CREATE TABLE `many_to_many` ( `information_id` INT(11) NOT NULL , `status_id` INT(11) NOT NULL , PRIMARY KEY (`information_id`, `status_id`)) ENGINE = MyISAM;
Pay attension to the PRIMARY KEY that depend on thos 2 fields.
Now you can add for every information_id many status_id relations,
and for status_id many information_id relations.
For first try, run this query to add values to see it
INSERT INTO `many_to_many` (`information_id`, `status_id`) VALUES (1, 1), (1, 2), (2, 1), (2, 2);
Insert Into table if a duplicate exits select that row primary key else insert into that table and return last insert id ?
SELECT IF (EXISTS(SELECT * FROM users WHERE userName='adminchat')) THEN
BEGIN
SELECT userId FROM users WHERE userName='adminchat';
end;
ELSE
BEGIN
INSERT INTO `users`( `userRole`, `userName`, `createdOn`, `emailId`, `is_active`, `password`) VALUES (1,'user1_chat',NOW(),'sdmd1#sdmd1.com',1,'123456')
select LAST_INSERT_ID();
END;
If a table contains an AUTO_INCREMENT column and INSERT ... UPDATE inserts a row, the LAST_INSERT_ID() function returns the AUTO_INCREMENT value. If the statement updates a row instead, LAST_INSERT_ID() is not meaningful. However, you can work around this by using LAST_INSERT_ID(expr). Suppose that id is the AUTO_INCREMENT column. To make LAST_INSERT_ID() meaningful for updates, insert rows as follows:
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), c=3;
A way to make things work is to use a dummy column,
so if you have a table with auto_increment column ID and unique key a,b and a smallint dummy column for instance, the query might look like this:
INSERT INTO test (a,b) VALUES ('1','2') ON DUPLICATE KEY UPDATE ID=LAST_INSERT_ID(ID),Dummy = NOT dummy;
Now, SELECT LAST_INSERT_ID(); will return the correct ID.