Insert sql if all rows are different - mysql

I have a SQL script that contains hundreds of queries.
I need to do an INSERT INTO query, but only if a row with all same fields doesn't exist.
Is there a short way to do it ? I don't want to add a WHERE NOT EXISTS on each query, it will takes too much time...

MERGE INTO Table_Name Tar
USING Table_Name src
ON Tar.Field1=src.Field1 AND Tar.Field2=src.Field2 AND Tar.Field3=src.Field3
WHEN NOT MATCHED
THEN
INSERT (...) VALUES(src..)
GO
This is rough code .Try this.

Related

Update and select using single query in mysql

I want to update the columns and select the values using single query in MySQL
For example
update table set address='bangalore',updated_count =updated_count+1 where id=1
select * from table where id=1
This is not possible. As #D-Shih mentioned, you cannot do both update and select in the same query. The SQL update convention doesn't allow for return data and the select statement can't write to a table.
Each has its own purpose and can't be written in one statement. They must be executed separately.

Updating mysql code using select command

So I have an assignment that is asking for me to add data into a table using the SELECT command. I've always known to use the insert into command or the Update command so I'm a little confused.
There are 2 tables, one named 'Book' and one named 'Fiction'. Book already has data in the table, I need to insert values from book into fiction using the select tool.
If someone could help explain it would be great,
Thank you!
What the assignment means, is to still use a INSERT query, but with an SELECT query attached to insert rows quickly from one table to another.
With INSERT ... SELECT, you can quickly insert many rows into a table
from the result of a SELECT statement, which can select from one or
many tables. For example:
INSERT INTO tbl_temp2 (fld_id)
SELECT tbl_temp1.fld_order_id
FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;
Source: https://dev.mysql.com/doc/refman/5.7/en/insert-select.html

mysqli - how do I count the number of rows before an insert without PHP?

This is mostly an academic problem as I try to learn SQL better. Is there a way to do this in a single SQL statement, without first doing a row count, then another statement to insert?
Traditionally I'll do this:
SELECT COUNT(*) FROM mytable;
Then in PHP's result object I'll know the number of rows. let's say I call it $totalrows. Then I'll do
INSERT INTO mytable (`rows`) VALUES ($totalrows);
This of course requires separate queries and PHP.
I wonder if there is a way to accomplish the same using a single SQL statement? I'm using mysqli.
Of course:
INSERT INTO mytable (`rows`)
SELECT COUNT(*)
FROM mytable;

Multiple MySQL queries (no PHP)

I am wondering if it is possible to perform a SQL query then update another table with the generated ID and continue through all of the rows?
I have this SQL query that works but what I need to do is after each row is added to cards to then update merged.cars_id with the last generated ID so they are linked. normally I would do this with PHP but ideally I would like to just do it with MySQL if possible.
MAIN QUERY
INSERT INTO cards (first_contact_date, card_type, property_id, user_id)
SELECT first_contact_date, 'P', property_id, user_id FROM merged
THEN I NEED WITH MATCHING ROWS (Roughly)
UPDATE merged SET merged.card_id = LAST_INSERT_ID (FROM ABOVE) into the matching record..
Is something like this possible and how do I do it?
I would recommend using MySQL triggers to do this
http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html
A trigger is a function that will be executed AFTER or BEFORE the INSERT or DELETE or UPDATE is done over any record of your table.
In your case you need to do a AFTER INSERT on cards that just updates the merged table. Make sure its AFTER insert as you wont be able to access the new row's ID otherwise.
The code would look something like this, assuming the id field from the cards table its named "id"
delimiter |
CREATE TRIGGER updating_merged AFTER INSERT ON cards
FOR EACH ROW BEGIN
UPDATE merged SET card_id = NEW.id;
END;
|
delimiter ;
May I suggest Stored Procedures?
http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html
--EDIT--
Ah yes, triggers. For this particular situation, Jimmy has the answer. I will leave this post for the sake of the link.
I would set up a trigger to do this. For mysql, read http://dev.mysql.com/doc/refman/5.0/en/triggers.html. This is what triggers are designed to handle.

how to insert records in 2 mysql tables

I'm trying to insert records on multiple mysql tables with similar entities(a normalized table)
I tried to do this but I get an error. I've also seen joins but it seems to work only when retrieving data.
insert into t1(pnum, hospnum) values('117', '656')
insert into t2(TELNUM, HOSPNUM) values('9981235', '676')
If you are executing these statements in a batch, you may need a semicolon to separate/terminate them:
insert into t1(pnum, hospnum) values(117, 656);
insert into t2(TELNUM, HOSPNUM) values(9981235, 676);
I suspect your fields are numbers not strings, try this:
insert into t1(pnum, hospnum) values(117, 656)
insert into t2(TELNUM, HOSPNUM) values(9981235, 676)
No need to use quotes for numbers otherwise you will get an error.
MySQL does not support a statement that inserts into two different tables.
The only option is to use a trigger on t1 that inserts to t2, but of course you don't have access to the telnum value in a trigger; you only have the columns of t1. So this won't work in your situation.
You must execute the two inserts as separate SQL statements.