I am trying to write a query to insert values in table2 with reference to table1 using a foreign key.table2 columns are (quesid(primary key/auto-increment), ques,ques_desc). There is another table table1 its primary key is foreign key which is also AUTO INCREMENT. So using that i was trying to insert values.
I have written below query:
insert into users_ques values("What is JAVA","Please SHare the details")
from users WHERE quesid = 1;
mysql is giving me error at WHERE (where is not valid at this position).
please help me so that i can sucessfully write this query.
The correct way to use select for insert
insert into users_ques (column1,column2)
select column1,column2
from users
where quesid = 1;
That is not a valid MySQL INSERT syntax. You either do:
INSERT INTO users_ques
VALUES ("What is JAVA","Please SHare the details");
OR
INSERT INTO users_ques
SELECT "What is JAVA","Please SHare the details" FROM users WHERE quesid = 1;
The way you're doing it now is just combining the two method together.
Demo fiddle
Related
I am beginner. So I don't know something.
I am trying insert value. But i can't. So i need your help.
My code:
INSERT INTO company_user (stamp_img) values ('test.png') SELECT * FROM company_user WHERE company_register = '123456789';
But Select,
syntax error: select is not valid input at this position.
How to check condition.
I want to check register is true then insert value.
So when i write
`INSERT INTO company_user (stamp_img) values ('test.png')`
Error Code: 1062. Duplicate entry '' for key 'company_register_UNIQUE'
What should I do?
Table Structure
1:
You are probably looking for updating the table. So you should use the UPDATE statement:
UPDATE company_user SET stamp_img = 'test.png' WHERE company_register = '123456789';
This will modifiy the row in your table where company_register is 123456789 setting the stamp_img to test.png.
Is this what you are looking for?
In addition to #Lelio
Are you trying to INSERT data first and then wanted to check if data is inserted or not?
Run following queries directly on query window and see if that you are looking for.
INSERT INTO `company_user` ('stamp_img') values ('test.png');
If its a UNIQUE key constraint check INSERT by changing stamp_img value to something else like test123.png etc.. if by changing the content your query works then you have to update the constraint that suits to your requirements
SELECT * FROM company_user WHERE company_register = '123456789';
This Insert is successful only if there is a record in company_user with company_register as '123456789'
INSERT INTO company_user (stamp_img) (SELECT 'test.png' FROM company_user WHERE company_register = '123456789');
I am trying to insert my values into table if Admin_User_Role_Id value against Admin_Id is not present in the table. Is it possible to insert!
My Table Structure:
Admin_User_Id (FK)
Admin_User_Role_Id (FK)
Is_Enabled (boolean flag)
Query which I tried, but not success
INSERT INTO role_association
SET Admin_User_Id=61, Admin_User_Role_Id=2, Is_Enabled=0
WHERE Admin_User_Role_Id
NOT IN
(SELECT Admin_User_Id, Admin_User_Role_Id FROM role_association)
I think it is possible but my logic is wrong. How should I manage this query to work successfully!
INSERT syntax cannot have WHERE clause. The only time you will find INSERT has WHERE clause is when you are using INSERT INTO...SELECT statement.
Probably you take care about where condition in your programming logic and write a simple insert statement and the depending on your logic use this statement to insert the records. Hope you got my point.
You want to insert your values in your table using this query for your reference
INSERT INTO Yourtablename(column1,column2,column3,...)
VALUES ('value1','value2','value3',.....);
You want to ADD one new column in your table means using this query
** ALTER TABLE table_name ADD column_name datatype**
If I'm inserting data into a table with the following fields:
serialNumber active country
I need to only insert duplicate serialNumbers if active is no.
So for example: I want to insert a record with serialNumber 1234.
If the serial number doesn't already exist in the table go ahead and add it. If it does already exist, check the value of 'active' active is yes then don't add the new record, if it's no then do add the record.
Any ideas how to achieve this in MYSQL?
If the table lacks the necessary unique keys and you do not have permission, or don't want to set the keys you would need, you can use this alternative:
INSERT INTO `table1`
(`field1`,
`field2`)
SELECT value1,
value2
FROM (SELECT 1) t_1
WHERE NOT EXISTS (SELECT 1
FROM `table1`
WHERE `field1` = value1
AND `field2` = value2);
For yor question it could be written as
INSERT INTO `activity`
(`serialNumbers`,
`active`)
SELECT 1234,
'yes'
FROM (SELECT 1) t_1
WHERE EXISTS (SELECT 1
FROM `activity`
WHERE `serialNumbers` = 1234
AND `active` = 'no');
You can use the ON DUPLICATE KEY statement after an INSERT INTO query to update the row if it already exists. Documentation : https://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
INSERT INTO table (serialNumber , active, country) VALUES (1010, 'no', 'FR')
ON DUPLICATE KEY UPDATE active='yes';
You can use the insert ... on duplicate key update in MySQL. It is similar to the MERGE used in other SQL databases, but MySQL does not provide the MERGE statement so this is the next best.
INSERT INTO TABLE (serialNumber, active, country)
VALUES (1234, 'active', 'GB') ON DUPLICATE KEY UPDATE country = 'ND';
Also, use INSERT IGNORE if you don't want to generate errors.
i have a doubt. i need the syntax for SQL SERVER – how to Insert Data From One Table to Another Table?
use INSERT INTO..SELECT statement
INSERT INTO table2 (col1,...)
SELECT col1,...
FROM table1
SQLFiddle Demo (different records but have same thought)
Insert INTO Table1 (UserName) (Select UName From Table2 b Where Table1.EID = b.EID)
But Showing Message:
The multi-part identifier "Table1.EID" could not be bound.
i want to set query if row not exist
IF NOT EXISTS (SELECT id FROM table WHERE id=1)
INSERT INTO table (id, name) VALUES (1,'abx');
and that if id!=1 then values are inserted and Check if row already exists
if any solution ?
thanks in advance ...
finally i soved it
INSERT INTO table (id, name) VALUES (1,'abx') ON DUPLICATE KEY UPDATE id = 1;
thanks for your suport
You have problem with data types - in SELECT id is compared to number (id = 1 - without apostrophes) and in INSERT id is written as a string (in apostrophes: '1').
INSERT IGNORE INTO `table_name` (`id`, `name`)
VALUES (1, 'abx');
MySQL does not have "IF NOT EXISTS".
For more info: How to 'insert if not exists' in MySQL?
You'll see that there are workarounds, but using MsSQL syntax won't work.
Using IGNORE should help: INSERT IGNORE INTO
Alternatively, simply let it fail.
I think that:
INSERT IGNORE INTO table (id, name) VALUES (1,'abx');
does that you want. It'll fail silently if the INSERT was unsuccessful.
Note that the key doesn't need quotes, it's an integer, not a string