How to update multiple values for specific ID numbers - mysql

I have a website and I want to update some empty values for some IDs.
In phpMyAdmin, editing a single row gives me this result:
UPDATE `sample_dir`.`page` SET `votes` = '4', `rating` = '7.00'
WHERE `page`.`id` =12676170;
However, if I try to update multiple rows at once (I was thinking putting a comma between the IDs will do it but it doesn't). I used this sql command:
UPDATE "sample_dir`.`page` SET `votes` = '1', `rating` = '9.00'
WHERE `page`.`id` =2042085451,12676170,733543897;
What am I doing wrong?
Thanks

Use the IN() operator
http://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_in
UPDATE `sample_dir`.`page` SET `votes` = '1', `rating` = '9.00'
WHERE `page`.`id` IN (2042085451,12676170,733543897);

Related

Trigger with query data

I have table with the informations:
INSERT INTO Transferencias
('Origem_Apostador_ID', 'Destino_Apostador_ID', 'Valor_Transferido')
VALUES
('1', '6', '200.00');
And I need to get value '200.00' and sum with the data from another table after this value have been insert on table to this table below:
INSERT INTO Conta
('Apostador_ID', 'Saldo', 'Qtd_Saque', 'Qtd_Deposito', 'Qtd_Transferencia')
VALUES
('1', '700.00', '0', '1', '0');
I have no idea how can I use data from query to do the update on "Saldo" column.
I don`t know if I understood your question correctly, but i think you need something like this:
UPDATE Conta
SET Saldo = Saldo + (SELECT Valor_Transferido FROM Transferencias WHERE Origem_Apostador_ID = '1')
WHERE Apostador_ID = '1';
You can add this code to some trigger if you want to. You can find many examples out there for simple triggers to do the work depending on your database.
TRIGGER EXAMPLE ON MYSQL
CREATE TRIGGER TRG_UPDATE_SALDO BEFORE INSERT ON Transferencias
FOR EACH ROW
BEGIN
IF NEW.Valor_Transferido > 0 THEN
--Increments the destination account
UPDATE Conta
SET Saldo = Saldo + NEW.Valor_Transferido
WHERE Apostador_ID = NEW.Destino_Apostador_ID;
--Subtracts the origin account
UPDATE Conta
SET Saldo = Saldo - NEW.Valor_Transferido
WHERE Apostador_ID = NEW.Origem_Apostador_ID;
END IF;
END;

MySQL update or insert for tables without primary key

I know the simple way to perform update or insert is using 'REPLACE' but it needs a primary key, how about in the case of a table without primary key?
I have 5 columns in my table:
remark_id(the auto-increment primary key)
user_id
remark_user_id
remark
last_modified
I wish to check whether the pair of user_id and remark_user_id exists first before updating the remark, else a new row will be created to save the remark with the user_id and remark_user_id.
Here's my code
INSERT INTO `users_remark` (`user_id`, `remark_user_id`, `remark`)
SELECT 1,3 ,'testing123'
FROM dual
WHERE NOT EXISTS
(SELECT *
FROM `users_remark`
WHERE `user_id` = 1
AND `remark_user_id` = 3)
After running the SQL, nothing happens in my Database. No record was added or updated.
[Edited]
Code changes using IF...ELSE... but it comes with some syntax errors on first line
#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 'IF EXISTS (SELECT * FROM users_remark WHERE user_id' at line 1
IF EXISTS (
SELECT * FROM `users_remark`
WHERE `user_id`=1 AND `remark_user_id` = 3
)
THEN UPDATE `users_remark` SET `remark` = 'testing123'
WHERE `user_id`=1 AND `remark_user_id` = 3
ELSE
INSERT INTO `users_remark` SET `remark` = 'testing123', `user_id`=1, `remark_user_id` = 3
Here is the query with both INSERT and UPDATE clauses (T-SQL syntax):
IF [condition here] BEGIN
UPDATE `users_remark`
SET `remark` = 'testing123'
WHERE `user_id`=1
AND `remark_user_id` = 3
END
ELSE BEGIN
INSERT INTO `users_remark` (`user_id`, `remark_user_id`, `remark`)
VALUES (1, 3, 'testing123)
END
EDIT: Same query with MySQL syntax
DECLARE #numrecords INT
SELECT #numrecords = count(*)
FROM `users_remark`
WHERE `user_id` = 1
AND `remark_user_id` = 3
IF #numrecords > 0 THEN
UPDATE `users_remark`
SET `remark` = 'testing123'
WHERE `user_id`=1
AND `remark_user_id` = 3
ELSE
INSERT INTO `users_remark` (`user_id`, `remark_user_id`, `remark`)
VALUES (1, 3, 'testing123)
END IF
Hope this will help you.
I finally try on putting code to php, it works and much easier to understand.
$checkRemark = mysqli_query($GLOBALS['db_conn'], "SELECT * FROM `users_remark` WHERE `user_id`=".$data['uid']." AND `remark_user_id` = ".$data['ruid']);
if (mysqli_num_rows($checkRemark)>0){
$remarkSql = "UPDATE `users_remark`
SET `remark` = '".$data['remark']."'
WHERE `user_id`=".$data['uid']."
AND `remark_user_id` = ".$data['ruid'];
} else {
$remarkSql = "INSERT INTO `users_remark` (`user_id`, `remark_user_id`, `remark`)
VALUES (".$data['uid'].", ".$data['ruid'].", '".$data['remark']."')";
}

Insert from multiple rows and update some static values at the same time?

I am trying to insert some rows form a select statement. The select statement returns multiple rows but with each row insert I want to update a few other columns.
This is what I have so far
INSERT INTO {$this->db->dbprefix('term_response')}
SET `Crs Code` = (
SELECT `Crs Code`
FROM {$this->db->dbprefix('EnrolmentsList')}
WHERE `Person ID` = ?
),`term_id` = ?, `st_id` = ?
I'm getting 'Subquery returns more than 1 row'. Can I not do what I'm trying?
Thanks
If you want to insert static values for other fields you could do something like this:
INSERT INTO {$this->db->dbprefix('term_response')}
SELECT `Crs Code`, 'my term_id value', 'my st_id value'
FROM {$this->db->dbprefix('EnrolmentsList')}
WHERE `Person ID` = 12345;

MySQL Replace is not working

I try to use mysql replace with following table.
CREATE TABLE price (
id BIGINT UNSIGNED AUTO_INCREMENT,
ski_chalet_id BIGINT UNSIGNED NOT NULL,
month DATE NOT NULL,
d_1 DOUBLE(18, 2),
d_2 DOUBLE(18, 2),
d_3 DOUBLE(18, 2),
d_4 DOUBLE(18, 2),
d_5 DOUBLE(18, 2),
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL,
UNIQUE INDEX fk_ski_chalet_price_ski_chalet_idx (ski_chalet_id, month),
INDEX ski_chalet_id_idx (ski_chalet_id),
PRIMARY KEY(id)
) DEFAULT CHARACTER SET utf8 ENGINE = InnoDB;
I want add a row if row is not exist and update if row is exist. So i used mysql REPLACE to do that.
REPLACE INTO `price` SET `ski_chalet_id` = 43 and `month` = '2013-04-01' and `d_1` = 23
This query is successful. but add all value as 00 and 0000-00-00 or null.
But insert query is working.
INSERT INTO `price` (`ski_chalet_id`,`month`,`d_1`) VALUES ('34', '2013-04-01', '46');
I cant find whats the issue. Please help me.
You have to use "," instead on "and" in replace query ........
REPLACE INTO price SET ski_chalet_id = 43 , month = '2013-04-01' , d_1 = 23
NOTE : REPLACE function will work want you want you don't have to look for other functions.
Don't use SET with REPLACE. The syntax for REPLACE is the same as for INSERT:
REPLACE INTO `price` (`ski_chalet_id`,`month`,`d_1`) VALUES ('43', '2013-04-01', '23');
To specify multiple columns with SET in REPLACE (and UPDATE / INSERT), you must separate them with commas. Your query amounts to:
SET `ski_chalet_id` = (some boolean expression)
Rewrite as
SET `ski_chalet_id` = 43, `month` = '2013-04-01', `d_1` = 23

MySQL INSERT WHERE

I have a stupid little SQL query which isn't working and i can't figure out why
SELECT * FROM `tablename` WHERE `id` = '294' INSERT INTO 'auth' VALUES 'false'
I want to select the row which pertains to the id (294) and insert the value false into the 'auth' column, but i keep getting a #1064 error.
Can someone correct the above?
embarrassed,
Dan
You just want to update the '294' record and set its 'auth' value to 'false', correct? If so:
update tablename set auth = 'false' where id = 294;
you want to do an update not an insert.
Update 'tablename' set 'auth' = 'false' where 'id' = '294'