MySQL "IF NOT EXISTS" [duplicate] - php

This question already has answers here:
MySQL: Insert record if not exists in table [duplicate]
(16 answers)
Closed 5 years ago.
I am having error in SQL syntax but I don't see it.
$query = "IF NOT EXISTS ( SELECT id FROM Provider
WHERE name=('$filename') )
INSERT INTO Provider (Name) VALUES ('$filename')";
$query_result = mysqli_query($connect, $query);
The code
INSERT INTO Provider (Name) VALUES ('$filename')
worked normally if I use just that.
Also the code
SELECT id FROM Provider WHERE name=('$filename')
worked fine when I tested its value
When I added IF NOT EXISTS I have SQL syntax error but I can't see it!
Any ideas?

You cannot use NOT EXISTS in this context. Try the following query instead:
INSERT INTO Provider (Name)
SELECT filename
FROM (SELECT ('$filename') AS filename) AS t
WHERE NOT EXISTS (SELECT id
FROM Provider
WHERE name=('$filename')

Related

MYSQL select * plus one column again [duplicate]

This question already has answers here:
How do I select all the columns from a table, plus additional columns like ROWNUM?
(4 answers)
Closed 5 years ago.
Some SQL toolsets like PDO can do special things based on the first column selected, such as take it as class name to instantiate or to use it as key into a hashtable. Unfortunately PDO removes that column from the result. What if I still want it to be part of the result?
I've tried queries such as
SELECT `class` as `myclass`, * FROM `mytable`
but I'm getting errors:
#1064 - 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 '* FROM mytable at line 1
I understand that there can't be conflicts in column names, hence the
as `myclass`
And the following works just fine:
SELECT `class` as `myclass`, `class` FROM `mytable`
Is this possible at all without doing a self-join or putting the full list of columns?
You can do like below
SELECT `mytable`.`class` as `myclass`, `mytable`.* FROM `mytable`
or like this
SELECT t.class AS myclass, t.* FROM mytable t;

MysQL SELECT statement if empty [duplicate]

This question already has answers here:
How can I do 'insert if not exists' in MySQL?
(11 answers)
Closed 5 years ago.
I am struggling a bit with something that I think should be simple to solve.
I first check the database to see if it exists. I would like it to insert the date if it does not exist but I am not sure how to structure the IF statement part. Many thanks
$date='2017-05-13';
$select_date = mysqli_query($link, "SELECT * from `marker` WHERE `date`='$date' ");
$insert_date = mysqli_query($link, "INSERT INTO `marker` (`date`,`value`) VALUES ('$date','1') ");
In general, for this type of operation, you want to use on duplicate key update. This starts with a unique index:
CREATE UNIQUE INDEX unq_marker_date ON marker(date);
Then the database guarantees only one row per date. You can then do the insert as:
INSERT INTO `marker` (`date`, `value`)
VALUES ('$date', '1')
ON DUPLICATE KEY UPDATE `date` = VALUES(`date`);
The ON DUPLICATE KEY part does nothing except prevent an error for a duplicate date.

MySQL CREATE TRIGGER using php [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 6 years ago.
I have read some other posts like mine, but none of them solve my problem,
I have two tables, user and purchase, When a user signup, I want to add the same user_id to the purchase table as well. I try to right a php code for that like this:
$query = "CREATE TRIGGER `purchase_insert` AFTER INSERT ON `user` FOR EACH ROW BEGIN INSERT INTO purchase (user_id) VALUES (NEW.user_id)";
$result = mysqli_query($connection, $query);
if($result){echo "<br>TRIGGER Success!";} else {die("<br>Database query failed. " . mysqli_error($connection));}
this is the error I get:
Database query failed. 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 '' at line 1
I'm sorry if the question is repetitious.
It should be back tick not single quotes
CREATE TRIGGER `purchase_insert` AFTER INSERT ON `user` FOR EACH ROW BEGIN INSERT INTO purchase (user_id) VALUES (NEW.user_id)
UPDATE
Try this
DELIMITER$$
CREATE TRIGGER `purchase_insert`
AFTER INSERT ON `user`
FOR EACH ROW
BEGIN
INSERT INTO purchase (user_id) VALUES (NEW.user_id);
END$$
DELIMITER ;

What faster "Insert ignore" or "Select and Insert" [duplicate]

This question already has answers here:
Which DB design is faster: a unique index and INSERT IGNORE, or using SELECT to find existing records?
(6 answers)
Closed 9 years ago.
mysql database.
Table have index on field "Code".
I need to insert to table new rows.
What works faster?
1)
simple index on field Code - for fast select
befor insert check rows : SELECT COUNT(*) FROM table WHERE Code = 'NewCode';
simple insert(if rows not found): Insert into table values ('NewCode')
2)
unique index on field Code - for insert
Insert IGNORE into table values ('NewCode')
For me more secure (and can be a backup of changes - better way) is the first, but I think that the action is similar.
Consult http://dev.mysql.com/doc/refman/5.5/en/insert.html for more detali
paladinux

how to select last autoincrement id using php and mysql [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
php/MySQL insert row then get 'id'
I have an inset command such as
insert (name) values($name);
I have id column as autoincrement. How can I get the id of the inserted record after inserting.
insert (name) values($name);
SET #lastid = LAST_INSERT_ID();
select blah_blah from table where id = #lastid;
To get that back into php, you do a normal mysql select on it like this:
select #lastid;
For mysql you can use mysql_insert_id to get the id of the last inserted row.

Categories