Multiple entries using SQL Update Query - mysql

Would this SQL query work for an SQL database?
UPDATE tableName SET colName1='value' WHERE colName2='Id_1' OR colName2='Id_2'
Basically, can I update the same column for multiple entries by checking against another column twice?
For example(as shown above), set colName1 to a value for 2 seperate entries which are identified by two different IDs

Yes that would work but you can make it more efficient like this:
UPDATE tableName SET colName1='value' WHERE colName2 in('Id_1','Id_2')

Related

SQL update Query with values coming from other Tables

I'm very bad at SQL so I'm struggling to UPDATE using values coming from separate tables:
I need use values inside 2 different tables as well as values from PHP script as part of an update query into a third table. My current SQL
UPDATE
order1
SET
Reorder_Time=supplier.Reorder_Time
Amount_Ordered="123456789"
Stock_Amount=product.Stock_Amount
FROM
supplier,product
WHERE
Order_ID="123456789"
:variables is just values from PHP and table and product are just other tables in my database
update order set
Reorder_Time=supplier.Reorder_Time
Amount_Ordered="123456789"
Stock_Amount=product.Stock_Amount
from
order
join supplier
on supplier.orderId = order.Id
join production
on supplier.[productFK or Id] = product.[Id or supplierFK]
where orderId = 123456789
I'm unfamiliar with the php script portion of your question but in order to update a table with the value of another table you might find success using the following INSERT INTO statement:
INSERT INTO table_abc
SELECT * FROM table_xyz WHERE condition
This code assumes that table_abc and table_xyz have the same structure. SQL will first execute the SELECT statement and grab the data that meets the WHERE condition. It will then grab those lines and insert them into table _abc.
Please try to provide more information on the structure of the 3 tables. The above code will probably not work since it assumes the structure is the same. By structure, I mean same number and names of columns.
Check this out as well: https://www.w3schools.com/sql/sql_insert_into_select.asp

INSERT ... SELECT to same table

I have a table that has a number of columns. For each row, I'd like to select three columns (PAR_BOOK, PAR_PAGE, PAR_LINE) and concatenate the contents of those three columns into a new fourth column (APN).
So, if PAR_BOOK=0108, PAR_PAGE=291 and PAR_LINE=07, APN should be 010829107
Make sense?
But, I'm unsure of what query I should use to do this. I need the results stored back in the same table as it needs to be ultimately exported out as a csv to work with the program that's going to map the data.
Assuming your fourth column is already in the table, you would use the following update query:
UPDATE YourTable
SET APN = CONCAT(PAR_BOOK, PAR_PAGE, PAR_LINE)
If your fourth column is not present in the table yet, you should use the ALTER TABLE statement to add it first before running the UPDATE statement:
ALTER TABLE YourTable
ADD APN VARCHAR(256) NULL
Inserting into the same table with INSERT INTO ... SELECT ... is no problem at all. MySQL holds the selected rows in a temporary table.

mysql: update several records with one query

i am currently using the following query syntax for updating records like the following:
update items set sort_index=1 where id=11
update items set sort_index=2 where id=33
update items set sort_index=3 where id=52
so i was wondering, is it possible to update several records by ONE single query?
thanks
Yes it is possible to update multiple records with one query, it however is not possible to update multiple records with different data, all records are updated with the same "SET" criteria.
So the method your currently using is the correct method for updating those records, if you have multiple records that get their sort index updated to 1,2,3 you can collect those ids then run the following to update all collected records
UPDATE items set sort_index=1 WHERE id IN (1,2,3); //where 1, 2 and 3 are the ids
Yes, you can update row(s) based upon the give where expression.

Multiple set and where clauses in Update query in mysql

I don't think this is possible as I couldn't find anything but I thought I would check on here in case I am not searching for the correct thing.
I have a settings table in my database which has two columns. The first column is the setting name and the second column is the value.
I need to update all of these at the same time. I wanted to see if there was a way to update these values at the same time one query like the following
UPDATE table SET col1='setting name' WHERE col2='1 value' AND SET col1='another name' WHERE col2='another value';
I know the above isn't a correct SQL format but this is the sort of thing that I would like to do so was wondering if there was another way that this can be done instead of having to perform separate SQL queries for each setting I want to update.
Thanks for your help.
You can use INSERT INTO .. ON DUPLICATE KEY UPDATE to update multiple rows with different values.
You do need a unique index (like a primary key) to make the "duplicate key"-part work
Example:
INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6)
ON DUPLICATE KEY UPDATE b = VALUES(b), c = VALUES(c);
-- VALUES(x) points back to the value you gave for field x
-- so for b it is 2 and 5, for c it is 3 and 6 for rows 1 and 4 respectively (if you assume that a is your unique key field)
If you have a specific case I can give you the exact query.
UPDATE table
SET col2 =
CASE col1
WHEN 'setting1'
THEN 'value'
ELSE col2
END
, SET col1 = ...
...
I decided to use multiple queries all in one go. so the code would go like
UPDATE table SET col2='value1' WHERE col1='setting1';
UPDATE table SET col2='value2' WHERE col1='setting1';
etc
etc
I've just done a test where I insert 1500 records into the database. Do it without starting a DB transaction and it took 35 seconds, blanked the database and did it again but starting a transaction first, then once the 1500th record inserted finish the transaction and the time it took was 1 second, so definetely seems like doing it in a db transaction is the way to go.
You need to run separate SQL queries and make use of Transactions if you want to run as atomic.
UPDATE table SET col1=if(col2='1 value','setting name','another name') WHERE col2='1 value' OR col2='another value'
#Frits Van Campen,
The insert into .. on duplicate works for me.
I am doing this for years when I want to update more than thousand records from an excel import.
Only problem with this trick is, when there is no record to update, instead of ignoring, this method inserts a record and on some instances it is a problem. Then I need to insert another field, then after import I have to delete all the records that has been inserted instead of update.

MySQL Query needed: I need to delete all data from a single column

I have an ecommerce store that I am currently working on and there are approx 300 products which have a field named "product_url"
These fields contains an old url that I need to delete altogether.
How can I create a query that will replace all "product_url" fields with data in them witha null value?
This will set every product_url to NULL which is currently not null.
UPDATE table_name
SET product_url = NULL
WHERE product_url is not null;
First of all, if you have 300 tables (one for each product), you cannot write one query that will set product URLs to NULL.
You have to write a query for each table (UPDATE table_name SET product_url = NULL as others have already said).
And if you have 10,000 products one day, you will have 10,000 tables if you continue like this. This will become a maintenance nightmare since you can see now what kind of problems you have with 300 tables.
Your database is denormalised. If your products share the same attributes, then they should be in one table named "Products", and every product should be represented as one row. Only then can you do what you wanted with one query.
Do you have a table for each product? If so I don't know.
Otherwise;
UPDATE products_table
SET product_url=null
Just an interpration...
UPDATE table_name SET column_name = NULL WHERE column_name = 'product_url'
Assuming ALL entries are to be filled with NULLs:
UPDATE table_name SET product_url = NULL
If you have lots of tables with the same prefix and the same structure, you could do a SHOW TABLES LIKE prefix_%. Then loop through the result set of this query and run separate queries to each table.
This will delete all data in that column without deleting the column itself .
UPDATE `table_name` SET `column_name`=null