This question already has answers here:
Concatenate string with field value in MySQL [duplicate]
(4 answers)
Closed 6 years ago.
I know this can update a table column to a new value from the previous value UPDATE table SET column=column+2 but this only happen to an integer value, I want to update a text value like this
// if the table column value is = "james,john,peter"
$new = ",andrew";
mysqli_query($con, "UPDATE table SET column=column+'".$new."');
so the new table column value will now be james,john,peter,andrew. I have searched even stackoverflow but all answers I got is for integer value. Please anyone with an idea?
Use the concat() function:
UPDATE table set column=concat(column, ',andrew');
Related
This question already has answers here:
MYSQL: Update field with concat of multiple fields
(1 answer)
how to concat two columns into one with the existing column name in mysql?
(6 answers)
Closed 3 years ago.
I want to get data from 2 columns, from '1h' and 'rate', and combine these values with '|' and add it to another column named '24h'. Something like "1h|rate"
update `exchanges` set 24h = `1h`.'|'.`rate` where id=1
update `exchanges`
set `24h` = concat(`1h`, '|', `rate`)
where id = 1
But note that this seems to be a denormalized column if you put multiple values in it.
This question already has answers here:
Mysql Table Column Cannot Be Null
(1 answer)
mysql - "column cannot be null"
(2 answers)
Closed 4 years ago.
I have read a lot of different posts trying to figure out why my code isn't working but the goal here is to set the value of the column 'name' to null when column 'mark' is below 69. My code is:
CREATE PROCEDURE gradesReport()
BEGIN
SELECT name FROM students WHERE mark > 69;
UPDATE students SET name = NULL WHERE mark < 69;
END
The first statement works fine but then I get the error that column name cannot be null.
It could be that the column name is declared as not null
You should firstly changed it into a NOT NULL Column
ALTER TABLE students
CHANGE `name` varchar(255) NULL;
Here is another way of making a column nullable in MySQL:
ALTER TABLE students MODIFY name VARCHAR(255);
Columns are nullable by default in MySQL, so we don't actually need to specify NULL in the alter statement.
This question already has answers here:
Get Updated Value in MySQL instead of affected rows
(5 answers)
Closed 9 years ago.
$query = "UPDATE transaction SET c_status = :status WHERE c_name = :name AND c_id = :id";
$stmt = $this->handle->prepare($query);
$stmt->bindParam(':c_status',$status,PDO::PARAM_STR);
$stmt->bindParam(':c_name',$name,PDO::PARAM_STR);
$stmt->bindParam(':c_id',$id,PDO::PARAM_STR);
return $stmt->execute();
Using the above syntax, I am able to update a record in the transaction table. However, what I only get with the return is a boolean. I want to know if there is a way I can get the transaction_id (the AUTO_INCREMENT field in the transaction table, c_id and c_name where just a column of that) and the rest of its columns?
This question is not related to PDO but to mysql in general.
UPDATE queries are not intended to return anything. To get a row from database you have to use SELECT query.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
MySQL ignores the NOT NULL constraint
I set a column to be not null
but when inserting new row and put this field value with null
mysql inserts the row
how i prevent that?
did you insert the value on database field like
insert into table
values('');
or
insert into table
values(null);
well both will insert a row in database but the field will be with null value. NULL is a keyword which indicates a field value that is null. if you want the field value will be empty then thats not null actually. to do so you have to do
insert into table
values(' '); // a space bar in between ' ' so thats not null
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Is there a way in MySQL to reverse a Boolean field with one query?
To update a (Boolean) value, normally we would check if it's set to false or true, and update it. Is there a query that would toggle a Boolean value?
UPDATE mytbl
SET field = !field
WHERE id = 42
Where 42 is the id of the record, field is the name of the Boolean field and mytbl is the table name.
You can use a Boolean operator for this. Here delete is your Boolean field.
update tab set `delete`=NOT `delete`