I wanted to overwrite all the values in the Project1 column with 0. Is there a way to do this ? Like all the students in studentid will all have 0 values in Project1.
You have to use an update query.
For your example:
UPDATE `TABLE_NAME` SET `Project1` = 0
DO :
UPDATE table_name SET Project1=0
Just omit where clause in Mysql, run this
update `mytable` set
`Project1` = 0
;
Related
i amn trying to run an insert query to insert the NOW() time and date only if certian enum values in my table are set to yes.
my table has 4 columns 'form1_completed', 'form2_completed', 'form3_completed', 'form4_completed'
the columns will be yes or no.
i am using this query to try and insert the current time and date into the database, its inserting but it doesnt pay attention to the where clause and just inserts anyway, can someone please show me what im doing wrong
$query2 = "IF (SELECT * FROM `supplier_session` WHERE `form1_completed` = 'Yes' AND `form2_completed` = 'Yes' AND `form3_completed` = 'Yes' AND `form4_completed` = 'Yes') INSERT INTO `supplier_session` (`completed_date`) VALUES (NOW());
WHERE `user_IP` = '$ipaddress '";
Instead of
WHERE `form1_completed` = Yes
use
WHERE `form1_completed` = 'Yes'
I think it should be:
IF EXISTS (SELECT * FROM ...) INSERT INTO ...
EXISTS (subquery) is true when the subquery returns any rows.
Or you could do:
IF (SELECT COUNT(*) FROM ...) INSERT INTO ...
Ok is there a possibility to update a column instead of a row?
f.e something like that:
$laninpstmt = $db->prepare ("UPDATE table SET column_name WHERE id = :allids");
$laninpstmt->bindParam(':allids', $_POST['input0']);
$laninpstmt->bindParam(':allids', $_POST['input1']);
$laninpstmt->bindParam(':allids', $_POST['input2']);
$laninpstmt->bindParam(':allids', $_POST['input3']);
$laninpstmt->bindParam(':allids', $_POST['input3']);
If i explain the code it's like:
Update all the rows(allids) from one column in a table
Running your query without a where clause will update all rows, and if you update a single field it will be the same as updating a column
UPDATE `test` SET `field_5` = 7
Will update table test and set all values in the column field_5 to 7
You could use IN:
Apparently, you need to do your own query, see https://stackoverflow.com/a/920523/2575355.
$inputs = array($_POST['input0'], $_POST['input1'], $_POST['input2']);
$allids = implode(', ', $inputs)
$laninpstmt = $db->prepare ("UPDATE table SET column_name WHERE id IN ($allids)");
You forgot to specify the value for you column_name like that
UPDATE table SET column_name = 'Some_value here' WHERE id = :allids
i guess you want do this
$laninpstmt = $db->prepare ("UPDATE table SET column_name = Concat(:allids1 , :allids2, :allids3, :allids4) WHERE id = :allids");
$laninpstmt->bindParam(':allids', $_POST['input0']);
$laninpstmt->bindParam(':allids1', $_POST['input1']);
$laninpstmt->bindParam(':allids2', $_POST['input2']);
$laninpstmt->bindParam(':allids3', $_POST['input3']);
$laninpstmt->bindParam(':allids4', $_POST['input4']);
I want to store the data from several curl calls to an initial table, but last insert id is inserting wrong info
Query 1 inserts data into table
table1
id name email valuereturn
1 val val#email.com 0
I then post data 3 times to my system and log it
table2
id name system valuereturn
1 val 5 0
2 val 0 0
3 val 0 0
the max value returned from my system i want to update table 1
update table1
set valuereturn = '5'
where id = LAST_INSERT_ID()
does not work because last insert id is 3 from table2, how can I use something like last_insert_id(Table1)?
i want to update my
Well, you can't. You have to retrieve and remember it in a PHP variable.
Or go for Saharsh's solution and remember it in a MySQL variable.
Store that LAST_INSERT_ID() of table1 in a variable and than use that variable in update query.
INSERT INTO table1(name) values ('Saharsh');
SELECT LAST_INSERT_ID() INTO #table1Id;
INSERT INTO table2(name, table1id) values ('Saharsh', #table1Id);
UPDATE table1 SET valuereturn = '5' WHERE id = #table1Id;
In PHP I assume you use the following code:
<?php
connect_db();
insert_first_query_to_table1();
insert_second_query_to_table2();
update_query_setting(last_insert_id());
?>
If that's the case, I suggest you to use a temp variable to store the last_insert_id.
<?php
connect_db();
insert_first_query_to_table1();
$setVal = last_insert_id();
insert_second_query_to_table2();
update_query_setting($setVal);
?>
Hope this helps.
PS: This is a pseudo code!
I need to update column values (say column2) with concatenation of two string variables (say str1 & str2) with another column (say column1). Final value which I need in column2 is str1 as prefix and str2 as suffix to column1 value.
One way which can be done is with a dummy third column and a total of 4 update queries.
update table set column3 = str1;
update table set column2 = concat(column3, column1);
update table set column3 = str2;
update table set column2 = concat(column2, column3);
But I want to reduce that to a single update without using a dummy column, like below -
update table set column2 = concat($str1, column1, $str2);
I need help in the concat part of the above query.
There's no reason this not to work:
$prefix = mysql_realescape_string($str1);
$suffix = mysql_realescape_string($str2);
$sql = "UPDATE table SET cloumn2 = CONCAT('$prefix', column1, '$suffix')";
Assuming you are using PHP, there is your solution:
$query="update table set column2 = concat('$str1', column1, '$str2');";
You were actually very close to solution.
Try something like:
$sql = "UPDATE table SET column2 = CONCAT_WS('-', '$str1', column1, '$str2');";
The first argument for CONCAT_WS is the string character you'd like to concatenate with.
Is there an efficient way to update a selection of rows' field to 0, but set One of these rows to 1 based on an ID.
Basically, I have multiple objects in a database, and I want to toggle between which one is "inuse", so the query will set one of the rows (by id) to inuse=1 and the others to inuse=0.
Thanks :)
UPDATE `table`
SET `inuse` = (`id` = 23)
Sure
UPDATE table SET inuse=IF(id=ABCD, 1, 0)
would set the inuse field to 1 if id is ABCD and 0 otherwise.
UPDATE table
SET inuse = (id = #id)
WHERE id = #id
OR inuse
This will update only relevant rows.
UPDATE myTable
SET Field = 0
WHERE FieldID <> [WhateverID]
UPDATE myTable
SET Field = 1
WHERE FieldId = [WhateverID]
Try
update tbl set inuse = if(test, 1, 0);
or shorter
update tbl set inuse = test;
for example
update tbl set inuse = name = 'foo';
If you are after to set a flag, so no other part of the code uses the same object at the same time, it's better if the calling code sets inuse=1 and resets it when done. Otherwise you will end up one thread to mark an object (row) as inuse, and then if another thread needs another object, it will reset the first one, while still in use.
If this is not the case, and you just want be able to set inuse for one, and reset all others, you can use:
UPDATE myTable
SET InUse = CASE
WHEN myTable.id = #id THEN 1
ELSE 0
END
if your database uses transactions, this is the best way to do it:
update myTable set inuse = 0;
update myTable set inuse = 1 where id = ?;
if you are not using transactions, then the other answer using CASE is the best option since it's portable. but it will require more CPU time than the two UPDATE statements.