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.
Related
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.
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;
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
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;
In MS Access, how do I insert a row which has one column that takes an SQL statement (SELECT) as value? In other words, how to I tell the SQL interpreter not to try to parse that statement and give me errors?
Wrap it in quotes like any other character.
INSERT INTO myTable
('MySQLColumn')
VALUES
('SELECT * FROM MyOtherTable')