Updating column for more than one row - mysql

I'm trying to run a single SQL command which will update table 'name', where the column 'id' matches 'eg1', 'eg2' and 'eg3'. The column to be updated is 'status' and should be changed to 'new_status' for the previously specified ids only.
Unfortunately I'm new with SQL, therefore I only got as far as this which doesn't seem to be working:
SELECT * FROM `tblhosting` WHERE 'id' IN (eg1,eg2,eg3) UPDATE 'status' SET new_status

Update tblhosting set status = 'new_status' where id in ('eg1','eg2','eg3')
This assuems you want to update tblhosting column status set to 'new_status' where the ID is either eg1, eg2 or eg3.

String literals are enclosed in single quotes.
Identifiers can be optionally enclosed in backtick characters.
The syntax for an UPDATE statement is like this:
UPDATE `tblhosting`
SET `status` = 'new_status'
WHERE `id` IN ('eg1','eg2','eg3')
The specification is a little ambiguous. The example above searches the table named tblhosting for rows to be updated, and assigns a value to the status column. This assumes that the value to be assigned is a string literal "new_status", and that "eg1", "eg2" and "eg3" are string values found in the column named id.)

update tblhosting set new_status = 'status' WHERE 'id' IN (eg1,eg2,eg3)

Related

Update column with value from another column

In a SQL table I have two columns: the first contain a path and the second contains a value.
colunm1
/path/
colunm2
12345
I need to update the first column with the value that exists in the second column. to get this result :
colunm1
/path/12456/
I tried this, but not working
update tablename p
set p.colunm1 = "/path/'colunm2'/"
You have the right idea, but the SQL you shared uses column2 as a string literal. You could use the concat to concatenate the two columns:
UPDATE tablename
SET column1 = CONCAT(column1, column2)
You have to use CONCAT
update tablename p
set p.colunm1 = CONCAT("/path/",`colunm2`,"/");

MySQL Query: UPDATE and/or APPEND

I have a temporary table that I use to insert into the master db.
The temp table is named "temp_table"
The master table is "master"
I currently use the following command to update "master"
SELECT COUNT(*) FROM master;
SHOW COLUMNS FROM master;
INSERT INTO master
SELECT * FROM temp_table
ON DUPLICATE KEY UPDATE email = VALUES(email), phone = VALUES(phone)
Now, I want to be able to append field (counter) from the "temp table" into "master." The field already exists in both tables and I just want to be able to update or append it.
"counter" field in master may be empty or it may contain a number value already.
In cases where the value exists, it should append separated by a comma. Format (88,89,90)
In cases where the it's empty, it should update (88)
Thank you in advance.
I think you want:
on duplicate key update
email = values(email),
phone = values(phone),
counter = case when counter is null
then values(counter)
else concat(counter, ',', values(counter))
end
You can also phrase this with coalesce(), although the expression might be a bit more complicated to understand:
on duplicate key update
email = values(email),
phone = values(phone),
counter = concat(
coalesce(concat(counter, ','), ''),
values(counter)
)

SQL, Edit a field in a table row, entering a character at the beginning and at the end.

I need, through an SQL query, to wrap the field in a table row as follows:
field: my-long-text
field after update: [:en]my-long-text[:]
how can I move?
An UPDATE query and a CONCAT should be enough:
update tablename
set
field = concat('[:en]', field, '[:]')
If field is null concat will return null, but you can exclude with where field is not null and of course if the field is already wrapped you have to exclude it somehow (but this depends on your logic).

Trying to use update query to only update fields that are blank in Microsoft Access

I am trying to use an update query to update fields from one table to another for fields but only if the fields in the table that i am updating into is blank. If they contain information, I do not want to overwrite the existing data
e.g
Field: Name
Table: Table 1
Update to: [Table2.][Name]
Criteria:
I am unsure of what to put in the criteria. I tried, 'Is Null', Like "".
Here is an example:
UPDATE MyTable SET MyTable.FieldB = "MyNewValue" WHERE (((MyTable.FieldB) Is Null));
Looking at the Query from within Access, you can switch to SQL view. You just need to put Is Null in the criteria column: UPDATE MyTable SET MyTable.FieldB = "MyNewValue" WHERE (((MyTable.FieldB) Is Null));
Furthermore, you can just write Is Null on alternate lines and it will count as OR.

Inserting into table with skipping the auto incremented column without the need to write the columns' names

So I have this big table with over 30 columns, and what I wanted to do was insert its values but ofc skipping the first column which is the 'id' that's auto incremented, so I had two choices that I can think of, one is:
INSERT INTO 'table name' col2,col3,col4... VALUES val2,val3,val4
But that would've taken a long time to copy the names of the columns, and a possibility of missing a letter thus having an error. So another option would be fetching the last id in the table and editing the next id myself:
$last_row_query = mysql_query("SELECT id FROM `order` ORDER BY id DESC LIMIT 1");
if(!$last_row_query) {
die('invalid query: '.mysql_error());
}
$last_row = mysql_fetch_assoc($last_row_query);
$last_id = $last_row['id'];
$current_id = $last_id+1;
and then the query would be
INSERT INTO 'table name' VALUES $current_id,val2,val3,val4
Is there's any difference in the efficiency of the those two ways? And is there a way that's recommended more than the other?
If you fetch the current value and try to insert the next higher value, you create a race condition. Two concurrent app requests would try to insert the same value.
A better solution is to use either NULL or DEFAULT in place of a value. Zero also may work, depending on the SQL mode.
INSERT INTO `table name` VALUES (DEFAULT, ?, ?, ?);
And remember to put the table name in back-ticks, not single-quotes. Single-quotes are for string literals or date literals.