This question already has answers here:
MySQL: Insert record if not exists in table [duplicate]
(16 answers)
Closed 8 years ago.
I have the following sql query running and it is giving me error 1064, syntax error.
IF NOT EXISTS (select * from locations where STREET_ADDRESS = 'test')
BEGIN
insert into locations (STREET_ADDRESS) values ('test')
end;
Can someone please help me out? It seems so simple yet it will not run. Thanks.
Also, I'm running MySQL version 5.6.11
Also you can try this
INSERT INTO locations (STREET_ADDRESS)
SELECT 'test' FROM DUAL
WHERE NOT EXISTS (
SELECT * from locations where STREET_ADDRESS = 'test'
) LIMIT 1;
Try this
IF ((select * from locations where STREET_ADDRESS = 'test')=0 )
BEGIN
insert into locations (STREET_ADDRESS) values ('test')
end;
you should use this type of sample code as:
INSERT INTO table_listnames (name, address, tele)
SELECT * FROM (SELECT 'Rupert', 'Somewhere', '022') AS tmp
WHERE NOT EXISTS (
SELECT name FROM table_listnames WHERE name = 'Rupert'
) LIMIT 1;
click here
Related
This question already has answers here:
SELECT INTO in MySQL
(2 answers)
Closed 5 years ago.
I'm trying to move a select statement's result into a new non-existent table but not able to figure how.
In MS SQL, I would be following the below,
SELECT * INTO <NON_EXISTING_TABLE> FROM
(
SELECT * FROM TABLE1 A
JOIN TABLE2 B
ON A.DescriptionNo = B.DescriptionNo
WHERE A.DescriptionNo =1) A
When i quickly looked it up , I can see only answers to insert data into an existing table but not dynamically create a new table with the result of the statement.
Please advice !
If you want to create a new table from a select, you can use this syntax:
create table new_table as
select *
from existing_table
where ...
This solution showed above works perfect also for selected rows. For example I am creating demonstration rows for my nice2work project, and this works perfect.
CREATE TEMPORARY TABLE tmptable SELECT * FROM myTable WHERE id=500;
UPDATE tmptable SET id = 0;
UPDATE some fields I need to change
INSERT INTO myTable SELECT * FROM tmptable;
DROP TABLE tmptable;
// You can use this same also directly into your code like (PHP Style)
$sql = "CREATE TEMPORARY TABLE tmptable SELECT * FROM myTable WHERE id=500;
UPDATE tmptable SET id = 0;
UPDATE some fields I need to change
INSERT INTO myTable SELECT * FROM tmptable;DROP TABLE tmptable;";
This question already has answers here:
MySQL: Insert record if not exists in table [duplicate]
(16 answers)
Closed 7 years ago.
I have two tables event and guest and an eventGuest table that joins them together and has some information about the guest (like if they attended etc) and am trying to insert into the eventGuest table without creating a duplicate sort of like:
insert into eventGuest(eventID, GuestID, attended)
values(iEventID, iGuestID, bAttended)
where (select count(*) from eventGuest where eventID = iEventID and guestID = iGuestID) = 0
Copy one table data to another :-
INSERT INTO TARGET_TABLE (`col1`,`col2`) SELECT `col1`,`col2` FROM SOURCE_TABLE;
You should use INSERT INTO ... SELECT if you want to insert values from a table into another:
INSERT INTO eventGuest(eventID, GuestID, attended)
SELECT iEventID, iGuestID, bAttended
FROM Anothertable t
where NOT EXIST(select 1
from eventGuest e
where e.eventID = t.iEventID
and e.guestID = t.iGuestID);
Or, if you want to insert into the same table if the values of eventid and iGuestid doesn't exist, you can do this:
INSERT INTO eventGuest(eventID, GuestID, attended)
SELECT *
FROM ( SELECT 'eventid', 'guestid', 'somevalue' ) AS t
WHERE NOT EXISTS (
SELECT 1 FROM eventGuest e
WHERE e.eventID ='eventid'
and e.guestID = 'guestid'
) LIMIT 1;
Please do add a unique constraint in the eventGuest table for both eventid and guestid and use INSERT IGNORE or REPLACE command to insert the new data.
I have a SQL syntax error with my IF NOT EXISTS on line 1 when I tried to do this request on MySQL, and I can't figure why.
IF NOT EXISTS (SELECT * FROM `my_table` WHERE first_name = 'Testfn')
BEGIN
INSERT INTO `my_table` (first_name)
VALUES ('Testfn')
END;
I'm trying to insert first_name only if there is no other same first name in my_table.
I also tried this syntax, but I still have the error 1064 "You have an error in your SQL syntax" :
IF NOT EXISTS (SELECT * FROM `my_table` WHERE first_name = 'Testfn') THEN
INSERT INTO `my_table` (first_name)
VALUES ('Testfn')
END IF;
I tried SELECT * FROMmy_tableWHERE first_name = 'Testfn' separately, and it works.
And like this doesn't work too :
INSERT INTO `my_table` (first_name)
VALUES ('Testfn')
WHERE NOT EXISTS (SELECT * FROM `my_table` WHERE first_name = 'Testfn');
EDIT : first_name is UNIQUE in the database.
You have not need to write a column name, without specifying you can try to insert because we already checked condition on above. So basically you can do it using Merge Statement like.
MERGE INTO my_table
USING (
SELECT first_name
) t
ON t.first_name = my_table.first_name
WHEN NOT MATCHED THEN
INSERT (first_name) VALUES (t.first_name);
Hope this help you!
This question already has an answer here:
how to fetch data from two tables in mysql?
(1 answer)
Closed 8 years ago.
I have to find out the output as name of student with highest marks? My both tables are-
CREATE TABLE student(Fields_ID INT, Name VARCHAR(20));
INSERT INTO student(Fields_ID,Name) VALUES(30,'JYOTI');
INSERT INTO student(Fields_ID,Name) VALUES(31,'KIRTI');
INSERT INTO student(Fields_ID,Name) VALUES(32,'YOGITA');
INSERT INTO student(Fields_ID,Name) VALUES(33,'RASHMI');
INSERT INTO student(Fields_ID,Name) VALUES(34,'NUPUR');
SELECT * FROM student;
CREATE TABLE Marks(Fields_ID INT, Student_ID INT NOT NULL,marks INT NOT NULL);
INSERT INTO Marks(Fields_ID,Student_ID,Marks) VALUES (30,40,100);
INSERT INTO Marks(Fields_ID,Student_ID,Marks) VALUES (31,41,88);
INSERT INTO Marks(Fields_ID,Student_ID,Marks) VALUES (32,42,72);
INSERT INTO Marks(Fields_ID,Student_ID,Marks) VALUES (33,43,33);
INSERT INTO Marks(Fields_ID,Student_ID,Marks) VALUES (34,44,15);
SELECT * FROM Marks;
I was trying to return the required out from the following code but it does not return same. Any idea why it is not returning the right values.I am a beginner in MySql so I am unable to find out the problem.
SELECT student.name,Marks.marks
FROM student INNER JOIN Marks
ON student.Fields_ID=Marks.Fields_ID;
SELECT student.name,Marks.marks
FROM student INNER JOIN Marks
ON student.Fields_ID=Marks.Fields_ID
where Marks.marks = (select max(marks) from Marks);
SELECT student.name,Marks.marks
FROM student
INNER JOIN Marks
ON student.Fields_ID=Marks.Student_ID
ORDER BY Marks.marks DESC
LIMIT 1
SELECT student.name,Marks.marks
FROM student INNER JOIN Marks
ON student.Fields_ID=Marks.Fields_ID
ORDER by marks DESC;
http://sqlfiddle.com/#!2/a5324/11
This is desired output?
EDIT: add LIMIT 1 to query if you want JUST ONE result.
This question already has an answer here:
how to fetch data from two tables in mysql?
(1 answer)
Closed 8 years ago.
I have to find out the output as name of students who have secured more than 70? My both tables are-
CREATE TABLE student(Fields_ID INT, Name VARCHAR(20));
INSERT INTO student(Fields_ID,Name) VALUES(30,'JYOTI');
INSERT INTO student(Fields_ID,Name) VALUES(31,'KIRTI');
INSERT INTO student(Fields_ID,Name) VALUES(32,'YOGITA');
INSERT INTO student(Fields_ID,Name) VALUES(33,'RASHMI');
INSERT INTO student(Fields_ID,Name) VALUES(34,'NUPUR');
SELECT * FROM student;
CREATE TABLE Marks(Fields_ID INT, Student_ID INT NOT NULL,marks INT NOT NULL);
INSERT INTO Marks(Fields_ID,Student_ID,Marks) VALUES (30,40,100);
INSERT INTO Marks(Fields_ID,Student_ID,Marks) VALUES (31,41,88);
INSERT INTO Marks(Fields_ID,Student_ID,Marks) VALUES (32,42,72);
INSERT INTO Marks(Fields_ID,Student_ID,Marks) VALUES (33,43,33);
INSERT INTO Marks(Fields_ID,Student_ID,Marks) VALUES (34,44,15);
SELECT * FROM Marks;
I was trying to return the required out from the following code but i was unable to form the logic. Any idea how to do it.I am a beginner in MySql so I am unable to find out the problem.
SELECT student.name,(select (marks>70) from marks)
From Student INNER JOIN marks
ON student.Fields_ID = marks.Fields_ID
GROUP BY student.name;
You can check conditions for groups with the having clause.. Use having to check if the sum of the marks reached your limit for every student.
SELECT student.name
From Student
INNER JOIN marks ON student.Fields_ID = marks.Fields_ID
GROUP BY student.name
HAVING sum(marks) > 70