How to update two tables with a single MySql query? - mysql

I Have 2 Table "users" and "querys". Normally i use
INSERT INTO querys (row1,row2,row3) values(.....)
UPDATE users SET credit=credit-1 ......
I want to use one query.

Try using Stored Procedures for a single query instead of trying to write very complex queries.

You can write multiple statements using the semi-colon delimiter. Additionally, you can use a transaction to ensure that both statements execute in your batch.
START TRANSACTION;
INSERT INTO querys (row1,row2,row3) values('A', 'B', 'C');
UPDATE users SET credit=credit-1;
COMMIT;

Related

how to run insert after update statement in sql

I am trying to run these statements, an insert statement after an update statement:
UPDATE mytable SET mycol='-' WHERE id IN (1,2,3);
INSERT mytable(mycol) VALUES ('hi');
but it did not work, only each one separately works, but I need to update some rows and then insert some values and all that sql code in a single sql file, how can achieve that?
thanks

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.

SQL check if existing row, if not insert and return it

I'm having a problem with my sql query. I need to insert a data that needs to be checked first if it is existing or not. If the data is existing the sql query must return it, if not insert and return it. I already google it but the result is not quite suitable to my problem. I already read this.
Check if a row exists, otherwise insert
How to 'insert if not exists' in MySQL?
Here is a query that' I'm thinking.
INSERT INTO #tablename(#field, #conditional_field, #field, #conditional_field)
VALUES(
"value of field"
(SQL QUERY THAT CHECK IF THERE IS AN EXISTING DATA, IF NOT INSERT THE DATA and RETURN IT, IF YES return it),
"value of feild",
(SQL QUERY THAT CHECK IF THERE IS AN EXISTING DATA, IF NOT INSERT THE DATA and RETURN IT, IF YES return it)
);
Please take note that the conditional field is a required field so it can't be NULL.
Your tag set is quite weird, I'm unsure you require all the technologies listed but as long as Firebird is concerned there's UPDATE OR INSERT (link) construction.
The code could be like
UPDATE OR INSERT INTO aTable
VALUES (...)
MATCHING (ID, SomeColumn)
RETURNING ID, SomeColumn
Note that this will only work for PK match, no complex logic available. If that's not an option, you could use EXECUTE BLOCK which has all the power of stored procedures but is executed as usual query. And you'll get into concurrent update error if two clients execute updates at one time.
You could split it out into 2 steps
1. run a select statement to retrieve the rows that match your valus. select count (*) will give you the number of rows
2. If zero rows found, then run the insert to add the new values.
Alternatively, you could create a unique index form all your columns. If you try to insert a row where all the values exist, an error will be returned. You could then run a select statement to get the ID for this existing row. Otherwise, the insert will work.
You can check with if exists(select count(*) from #tablename) to see if there is data, but with insert into you need to insert data for all columns, so if there is only #field missing, you cant insert values with insert into, you will need to update the table and go with a little different method. And im not sure, why do you check every row? You know for every row what is missing? Are you comparing with some other table?
You can achieve it using MySQL stored procedure
Sample MySQL stored procedure
CREATE TABLE MyTable
(`ID` int, `ConditionField` varchar(10))
;
INSERT INTO MyTable
(`ID`, `ConditionField`)
VALUES
(1, 'Condition1'),
(1, 'Condition2')
;
CREATE PROCEDURE simpleproc (IN identifier INT,ConditionData varchar(10))
BEGIN
IF (SELECT ID FROM MyTable WHERE `ConditionField`=ConditionData) THEN
BEGIN
SELECT * FROM MyTable WHERE `ConditionField`=ConditionData;
END;
ELSE
BEGIN
INSERT INTO MyTable VALUES (identifier,ConditionData);
SELECT * FROM MyTable WHERE `ConditionField`=ConditionData;
END;
END IF;
END//
To Call stored procedure
CALL simpleproc(3,'Condition3');
DEMO

Combine Insert, Update and Select in MySQL

Is there a way to combine an insert, update, and select query into one?
Kind of like in a condition,
if select query, when the condition is true it will go to update and if not it will go to insert.
How can i do this?
No, it's not possible, in the context of a single SQL statement.
It is possible to combine and INSERT and UPDATE action into a single statement. The INSERT ... ON DUPLICATE KEY UPDATE statement will basically attempt to INSERT a row, and if that insert results in a "duplicate key" exception, the UPDATE action will be performed instead.
But in terms of a testing some general condition with a SELECT statement, and then running an INSERT or UPDATE depending on the result of the query, the answer is no, MySQL does not have any single statement syntax to support that.
STORED PROCEDURE is the ANSWER and IF/ELSE

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.