This question already has answers here:
How can I write SQL for a table that shares the same name as a protected keyword in MySql? [duplicate]
(3 answers)
Closed 9 years ago.
Not sure what's wrong, just running this in PHP MyAdmin right now. Does anything pop out to you? Thanks.
INSERT INTO order (CustomerID, BillAddr, ShipAddr, Date, Total)
VALUES ('test', 'test', 'test', '2012-07-02', 22)
Error:
MySQL said: #1064
Structure of order table:
FIELD TYPE
OrderID int(11) auto increment
CustomerID varchar(50)
BillAddr varchar(200)
ShipAddr varchar(200)
Date date
Total double
(I leave the OrderID out of the INSERT as it is an auto increment)
EDIT
Same error with this syntax:
SQL query:
INSERT INTO 'order'( CustomerID, BillAddr, ShipAddr, 'Date', Total )
VALUES (
'test', 'test', 'test', '2012-07-02', 22
)
MySQL said: Documentation
#1064 -
The word Date is a keyword. Try it like this:
INSERT INTO `order` (CustomerID, BillAddr, ShipAddr, `Date`, Total)
VALUES ('test', 'test', 'test', '2012-07-02', 22)
ETA: And order is also a keyword :)
Related
This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 2 years ago.
I have a table as shown and I am trying to insert the data to it but I am constantly getting error . Can anyone suggest what should be the query ??
Here's my table structure:
My insert query:
INSERT INTO country (id, country_name, iso_code, country_pic_url, rank, created_time, updated_time, deleted_time) VALUES(1, 'Afghanistan', 'AF', null, 0, '2013-09-04 13:30:00', '2013-09-04 13:30:00', '2013-09-04 13:30:00')
Don't use null instead Put NULL in your syntax or either leave it blank.
Check if the rank you are trying to put is a string, so instead of just using 0, put '0' in your query.
I've got a question about SQL (php), I want to INSERT data in my table. But I want to use the IF NOT EXIST value.
What I've tried:
INSERT INTO vrienden (id, userid, vriendmetid, accepted) VALUES (null, '1', '20', '0') WHERE NOT EXISTS (SELECT * FROM vrienden WHERE userid='1' AND vriendmetid='20')
I'm not sure what's wrong, because I get the following error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE NOT EXISTS (SELECT * FROM vrienden WHERE userid='1' AND vriendmetid='20')' at line 1
Thanks.
You want insert . . . select, not insert . . . values:
INSERT INTO vrienden (id, userid, vriendmetid, accepted)
SELECT x.*
FROM (select null as id, '1' as userid, '20' as vriendmetid, '0' as accepted) x
WHERE NOT EXISTS (SELECT 1 FROM vrienden v WHERE v.userid = x.userid AND v.vriendmetid = x.vriendmetid);
However, you probably shouldn't be doing this in the INSERT. Instead, create a unique index/constraint:
create unique index unq_vrienden_userid_vriendmetid on vrienden(userid, vriendmetid);
This way, the database will ensure uniqueness of the columns, so your application does not have to.
I have 2 tables: users with columns (id,username, password), and user_failed with columns (user_id, failed, time). is there any possible way i can insert into table user_failed by only using username? i try this code but it failed:
INSERT INTO `login_attempts`(`user_id`, `time`, `failed`)
VALUES (SELECT user_id FROM users WHERE username = 'pokemon','',3)
Your SQL query is incorrect for several reasons.
The following should work if I have interpreted your query correctly.
INSERT INTO `login_attempts`(`user_id`, `time`, `failed`)
SELECT id, '', 3 FROM users WHERE username = 'pokemon'
INSERTing into a table from a SELECT does not require VALUES ( ... ). Here is an example of how you would use VALUES ( ... ):
INSERT INTO `login_attempts`(`user_id`, `time`, `failed`)
VALUES (1, '', 3)
Also, your sub query SELECT user_id FROM users WHERE username = 'pokemon','',3 the WHERE clause is invalid. Specifically the '',3 part which I assume is the values you wanted to insert for time and failed.
This will work....you have to add plain parentheses before and after statements.
INSERT INTO `login_attempts`(`user_id`, `time`, `failed`) VALUES ((SELECT user_id FROM users WHERE username = 'pokemon'),'',3)
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
This question already has answers here:
Check if a row exists, otherwise insert
(12 answers)
MySQL Conditional Insert
(13 answers)
Closed 9 years ago.
I want to run a set of queries to insert some data into an SQL table but only if the record satisfying certain criteria are met. The table has 4 fields: id (primary), fund_id, date and price
I have 3 fields in the query: fund_id, date and price.
So my query would go something like this:
INSERT INTO funds (fund_id, date, price)
VALUES (23, '2013-02-12', 22.43)
WHERE NOT EXISTS (
SELECT *
FROM funds
WHERE fund_id = 23
AND date = '2013-02-12'
);
So I only want to insert the data if a record matching the fund_id and date does not already exist. If the above is correct it strikes me as quite an inefficient way of achieving this as an additional select statement must be run each time.
Is there a better way of achieving the above?
Edit: For clarification neither fund_id nor date are unique fields; records sharing the same fund_id or date will exist but no record should have both the same fund_id and date as another.
This might be a simple solution to achieve this:
INSERT INTO funds (ID, date, price)
SELECT 23, DATE('2013-02-12'), 22.5
FROM dual
WHERE NOT EXISTS (SELECT 1
FROM funds
WHERE ID = 23
AND date = DATE('2013-02-12'));
p.s. alternatively (if ID a primary key):
INSERT INTO funds (ID, date, price)
VALUES (23, DATE('2013-02-12'), 22.5)
ON DUPLICATE KEY UPDATE ID = 23; -- or whatever you need
see this Fiddle.
Although the answer I originally marked as chosen is correct and achieves what I asked there is a better way of doing this (which others acknowledged but didn't go into). A composite unique index should be created on the table consisting of fund_id and date.
ALTER TABLE funds ADD UNIQUE KEY `fund_date` (`fund_id`, `date`);
Then when inserting a record add the condition when a conflict is encountered:
INSERT INTO funds (`fund_id`, `date`, `price`)
VALUES (23, DATE('2013-02-12'), 22.5)
ON DUPLICATE KEY UPDATE `price` = `price`; --this keeps the price what it was (no change to the table) or:
INSERT INTO funds (`fund_id`, `date`, `price`)
VALUES (23, DATE('2013-02-12'), 22.5)
ON DUPLICATE KEY UPDATE `price` = 22.5; --this updates the price to the new value
This will provide much better performance to a sub-query and the structure of the table is superior. It comes with the caveat that you can't have NULL values in your unique key columns as they are still treated as values by MySQL.
Assuming you cannot modify DDL (to create a unique constraint) or are limited to only being able to write DML then check for a null on filtered result of your values against the whole table
FIDDLE
insert into funds (ID, date, price)
select
T.*
from
(select 23 ID, '2013-02-12' date, 22.43 price) T
left join
funds on funds.ID = T.ID and funds.date = T.date
where
funds.ID is null