Increment field value on REPLACE - mysql

I have done this successfully with an UPDATE statement before but not a REPLACE.
I am saving favourite items in a mysql table when a user has checked out.
Table Favs is:
USER (int)
ITEM (int)
COUNT (int default 0)
The SQL I am trying is :
REPLACE INTO favs (user,item,count) VALUES ('1','3', count + 1)
although it does not throw any errors it does not seem to increment the value either.
Is this possible? Thank you.

Looks like it doesn't work like this on replace. From the manual:
You cannot refer to values from the
current row and use them in the new
row. If you use an assignment such as
SET col_name = col_name + 1, the
reference to the column name on the
right hand side is treated as
DEFAULT(col_name), so the assignment
is equivalent to SET col_name =
DEFAULT(col_name) + 1.
Edit:
However, INSERT ... ON DUPLICATE UPDATE might do what you're trying to accomplish:
INSERT INTO favs (user, item) VALUES (2, 3)
ON DUPLICATE KEY UPDATE count = count + 1;

Related

Using INSERT with condition

there is a table customers with three columns (Id, name, Phone).
For example there is charachter with id 1 and name is Tom, and there are a lot of differents id and names so i need to add a phone number solely for tom in this sutiation, how can i do it?
i tried this:
INSERT INTO Customers (Phone) values ('a Number') WHERE Id = 1;
I can get that i use condition "where" wrong, how should i use it right in my situation, please help, and thanks.
It seems that you actually want an update here:
UPDATE Customers
SET Phone = 'a Number'
WHERE Id = 1;
If you really do want to add a new record, then drop the WHERE clause:
INSERT INTO Customers (Phone) VALUES ('a Number');
If the Id column be auto increment, then MySQL will auto generate a value for the Id.
In the described case (update of already existing record) you should use UPDATE statement instead of INSERT:
UPDATE Customers SET Phone = "customer_phone_number_here" WHERE Id = 1;
Hope, this link would be useful as well.

Mysql trigger add number to new value on duplicate key

I'm trying to import data to a new table, but turns out some values from two different columns are duplicate, but it seems to not be working. This is what my trigger looks like:
DELIMITER //
CREATE TRIGGER insert_specificationattributeoption_child AFTER INSERT ON import_specificationattributeoption FOR EACH ROW
BEGIN
INSERT INTO t_virtuemart_customs (virtuemart_custom_id, custom_parent_id, custom_title, show_title, field_type, custom_params, created_on, created_by, ordering, modified_on, modified_by)
VALUES (NEW.option_id, NEW.specification_attribute_id, NEW.option_name, lower(NEW.option_name), 'S', 0, current_time(), 633, NEW.display_order, current_time(), 633)
ON DUPLICATE KEY UPDATE NEW.option_id = NEW.option_id + 5000;
END //
With the trigger I don't get the duplicate key error, but I don't see any key being over 5000. What am I doing wrong there? Would it be better to "find" the highest id and adding the new id to it instead?
Edit: Basically what I want to do is, if I'm inserting a primary key that already exists, change the value I want to insert.
I am not clear what you are trying to do. The trigger must be detecting the duplicate key because no duplicate error is being shown but UPDATE NEW.option_id = NEW.option_id + 5000 has no effect on the table. If you want to amend the table then the syntax would be UPDATE option_id = NEW.option_id + 5000 -note this also amends the next auto_increment value to NEW.option_id + 5000 + 1.

Get a number inserted or updated into a table

What's the best way to get a number I'm inserting or updating into my database? I'm trying to figure out what count ended up being after this insert:
$pdo = $db->prepare('INSERT INTO dailies (day, count) VALUES (:day, 1) ON DUPLICATE KEY UPDATE count = count+1');
$pdo->execute(array(':day'=>date('z')));
I'm trying to do a fetch, but its not working. I'm guessing because this is an insert or update and not a select.
$fetch = $pdo->fetch(PDO::FETCH_ASSOC);
You can do something like this with user variables:
INSERT INTO dailies (day, count) VALUES (:day, 1) ON DUPLICATE KEY UPDATE count = (#new_count = count+1)
Then
SELECT #new_count
If the record was inserted, #new_count will contain the value you set in the update. Otherwise it will come back as NULL.

Need something like alias in insert table

I want to insert a value into a table. However that value comes from that table too. And I want to check if there is a duplicate key on that table. Since this value come from that table too, the query says that a column name is ambiguous.
$result2 = "INSERT INTO estock_saldo
(items, customer_id, quantity , reference_no, size)
SELECT
items, '".$member_id."', '".$quantity[$i]."', reference_no, size
FROM
estock_saldo
WHERE id in ({$order_id[$i]})
ON DUPLICATE KEY
UPDATE estock_saldo.quantity = estock_saldo.quantity - '".$quantity[$i]."'";
$res2 = $mysqli->query($result2);
if(!$res2){ printf("Errormessage 2: %s\n", $mysqli->error); die(); }
The ambiguous come from estock_saldo.quantity. I have tried to alias the column name. However you can't do that in insert table.
Problem persist in the below shown code snippet. You can't use column alias in INSERT statement.
INSERT INTO estock_saldo
(items, customer_id, quantity AS asquan
<-- HERE
What you are trying will always have a duplicate entry, since you are inserting the same record again. Instead of INSERT statement, you actually meant to do a UPDATE like
UPDATE estock_saldo SET
quantity = quantity - '".$quantity[$i]."'"
WHERE id in ({$order_id[$i]});

MySQl: triggering an insertion before updating, if the not existing

I'm not really familiar with MySQL. But for performance reasons i want to avoid checking if a row already exists. Currently I've got a table with 2 columns (num, count) and I do something like this:
SELECT num FROM numbers WHERE num = 123
then if row exists...
UPDATE numbers SET count = count + 456 WHERE num = 123
else if row not exists...
INSERT INTO numbers (num, count) VALUES (123, 456)
Is there a possibility to avoid always querying the table. Something like a trigger... In the end, i just want to make an UPDATE, so that insertion is done automatically. thanks in advance
You don't need to use triggers, all you have to do is add UNIQUE constraint to your num column and then:
INSERT INTO numbers SET num = 123, count = 456 ON DUPLICATE KEY UPDATE count = count + 1;
Also, don't use reserved words for column names such as COUNT.