Is there a way to update multiple rows with different values for each row using a single SQL query? I have to update one colum in many rows with different data. Using individual update queries for each row seems excessive so if it's possible I would like to consolidate this process into a single SQL statement or at least reduce the number of queries required.
I am using PHP with the Zend framework and MySql.
Create a temporary table and fill it with:
CREATE TEMPORARY TABLE temptable (id INTEGER, VALUE VARCHAR(200))
INSERT
INTO temptable
VALUES
('1', 'val1'),
('2', 'val2'),
('3', 'val3'),
('4', 'val4')
Then issue:
UPDATE
mytable m, temptable t
SET m.value = t.value
WHERE m.id = t.id
Don't know about MySQL specifically, but to update multiple rows based on a SELECT, or a UNION of multiple SELECTs, I would do
UPDATE U
SET MyColumn = T.OtherColumn
FROM MyUpdateTable AS U
JOIN
(
SELECT [OtherColumn] = OtherColumn1
FROM MyOtherTable1
WHERE ...
UNION
SELECT OtherColumn2
FROM MyOtherTable2
WHERE ...
) AS T
ON T.ID = U.ID
Update 10/28/2014, converted to work for MySQL:
UPDATE MyUpdateTable AS U
JOIN
(
SELECT [OtherColumn] = OtherColumn1
FROM MyOtherTable1
WHERE ...
UNION
SELECT OtherColumn2
FROM MyOtherTable2
WHERE ...
) AS T
ON T.ID = U.ID
SET MyColumn = T.OtherColumn
I know this works for SQL Server, so it's worth a try in MySQL.
update xtable
set a =
Case
when a = "a"
then z
when a = "b"
then y
End
where ...
You can construct the case statement based on your different rows.
Related
What I am trying to do is to insert records from multiple tables into 1 table by joining. Here is what I have and I can't seem to get it to work. I get the following error #1136 - Column count doesn't match value count at row 1
INSERT INTO users(`name`, `email`,`location_id`, `department_id`)
VALUES('John Doe', 'jdoe#email.com', 1, 1)
INSERT INTO extensions(`ext`)
VALUES(98765)
INSERT INTO dids(`did`)
VALUES('1-800-555-5555')
INSERT INTO users_numbers(user_id,ext_id,did_id)
SELECT extensions.*, dids.*, users.*
FROM extensions tbl_ext, dids tbl_dids, users tbl_users, users_numbers usn
Inner Join users ON users.id=usn.user_id
Inner Join extensions ON extensions.id=usn.ext_id
Inner Join dids ON dids.id=usn.did_id
WHERE tbl_users.name = 'John Doe'
AND tbl_users.email = 'jdoe#email.com'
AND tbl_ext.ext = 98765
AND tbl_dids.did='401-559-9999';
The first 3 insert statements work. The error fires on the 4 insert when I try to join. Can anyone help.
Use LAST_INSERT_ID():
INSERT INTO users(`name`, `email`,`location_id`, `department_id`)
VALUES('John Doe', 'jdoe#email.com', 1, 1);
SET #user_id = LAST_INSERT_ID();
INSERT INTO extensions(`ext`)
VALUES(98765);
SET #ext_id = LAST_INSERT_ID();
INSERT INTO dids(`did`)
VALUES('1-800-555-5555');
SET #did_id = LAST_INSERT_ID();
INSERT INTO users_numbers(user_id,ext_id,did_id)
VALUES (#user_id,#ext_id,#did_id);
Instead of using user variables (#user_id,#ext_id,#did_id) you can also fetch the LAST_INSERT_ID in your application language.
There are two problems.
First, the SELECT list has to return only the columns that you want to insert into the table. The error is because you're returning tablename.* rather than specific columns.
Second, the SELECT query is very wrong. You have the tables that you're joining listed twice: first in the FROM clause and then again in the INNER JOIN clauses. You should only list them once (unless you're doing a self-JOIN, which is not needed here).
And you shouldn't be joining with users_numbers -- that's the table you're trying to add to, so it doesn't have the IDs for these rows yet.
What you want is a simple cross join between the rows from each of the source tables that match the values you're combining into the relation table.
INSERT INTO users_numbers(user_id,ext_id,did_id)
SELECT u.id, e.id, d.id
FROM users AS u
CROSS JOIN extensions AS e
CROSS JOIN dids AS d
WHERE u.name = 'John Doe'
AND u.email = 'jdoe#email.com'
AND e.ext = 98765
AND d.did='401-559-9999';
You need to be very specific about which columns you want from that SELECT. Right now you're grabbing everything from the three tables, that's too much.
Maybe you mean:
INSERT INTO users_numbers(user_id,ext_id,did_id)
SELECT users.id, extensions.id, dids.id
FROM ...
When composing complicated queries like this, run the SELECT subcomponent separately to ensure it produces the right data. I bet if you ran your version it'd show a bunch of columns.
I'm looking for a simple way to do an update on a table only if there is no other columns present in that same table with the same value I'm trying to update, ideally in a single query. So far I'm getting an error You specify target table 't1' for update in FROM clause. Here is what I tried in a few variations so far (still unable to get working):
UPDATE emailQueue AS t1
SET
t1.lockedOn = 1470053240
WHERE
(SELECT
COUNT(*)
FROM
emailQueue AS t2
WHERE
t2.lockedOn = 1470053240) = 0
AND t1.lockedOn IS NULL
In MySQL, you need to use a join. In this case, a left join is in order:
UPDATE emailQueue eq LEFT JOIN
emailQueue eq2
ON eq2.lockedOn = 1470053240
SET eq.lockedOn = 1470053240
WHERE eq.lockedOn IS NULL AND
eq2.lockedOn IS NULL;
I need to insert a table with some values (eg: 'NDA' in this case). This seems to work well if I have just one value to be inserted. I have around a dozen of similar values, is there a was i can tweak this query to insert say { 'NDA', 'SDM', 'APM' } values. Was curious to know if it can be done without a stored procedure or copy pasting the same statements over and changing the values.
INSERT IGNORE INTO customer_feature (customer_id, feature)
SELECT c.id, 'NDA' FROM
customer as c
where c.edition = 'FREE_TRIAL';
Reference: mysql -> insert into tbl (select from another table) and some default values
Is this what you want?
INSERT IGNORE INTO customer_feature(customer_id, feature)
select c.id, f.feature
from customer c cross join
(select 'NDA' as feature union all select 'SDM' union all select 'APM'
) f
where c.edition = 'FREE_TRIAL';
I want to loop through some records and update them with an ad hoc query in MySql. I have a name field, so I just want to loop though all of them and append a counter to each name, so it will be name1, name2, name3. Most examples I see use stored procs, but I don't need a stored proc.
As a stepping stone on your way to developing an UPDATE statement, first generate a SELECT statement that generates the new name values to your liking. For example:
SELECT t.id
, t.name
, CONCAT(t.name,s.seq) AS new_name
FROM ( SELECT #i := #i + 1 AS seq
, m.id
FROM mytable m
JOIN (SELECT #i := 0) i
ORDER BY m.id
) s
JOIN mytable t
ON t.id = s.id
ORDER BY t.id
To unpack that a bit... the #i is a MySQL user variable. We use an inline view (aliased as i) to initialize #i to a value of 0. This inline view is joined to the table to be updated, and each row gets assigned an ascending integer value (aliased as seq) 1,2,3...
We also retrieve a primary (or unique) key value, so that we can match each of the rows from the inline view (one-to-one) to the table to be updated.
It's important that you understand how that statement is working, before you attempt writing an UPDATE statement following the same pattern.
We can now use that SELECT statement as an inline view in an UPDATE statement, for example:
UPDATE ( SELECT t.id
, t.name
, CONCAT(t.name,s.seq) AS new_name
FROM ( SELECT #i := #i + 1 AS seq
, m.id
FROM mytable m
JOIN (SELECT #i := 0) i
ORDER BY m.id
) s
JOIN mytable t
ON t.id = s.id
ORDER BY t.id
) r
JOIN mytable u
ON u.id = r.id
SET u.name = r.new_name
SQL Fiddle demonstration here:
http://sqlfiddle.com/#!2/a8796/1
I had to extrapolate, and provide a table name (mytable) and a column name for a primary key column (id).
In the SQL Fiddle, there's a second table, named prodtable which is identical to mytable. SQL Fiddle only allows SELECT in the query pane, so in order to demonstrate BOTH the SELECT and the UPDATE, I needed two identical tables.
CAVEAT: be VERY careful in using MySQL user variables. I typically use them only in SELECT statements, where the behavior is very consistent, with careful coding. With DML statements, it gets more dicey. The behavior may not be as consistent in DML, the "trick" is to use a SELECT statement as an inline view. MySQL (v5.1 and v5.5) will process the query for the inline view and materialize the resultset as a temporary MyISAM table.
I have successfully used this technique to assign values in an UPDATE statement. But (IMPORTANT NOTE) the MySQL documentation does NOT specify that this usage or MySQL user variables is supported, or guaranteed, or that this behavior will not change in a future release.
Have the names stored in a table. Do a join against the names and update in the second table you want to.
Thanks
As my application is expanding, I now am changing the structure of my database; I now want to control file types within the database. I wanted to start with the current file types already in the database. My Database now has a [simplified] 2 table structure like:
tbFiles: pkFileID, fileType, fileName
tblFileType: pkFileType, typeName, typeDesc
I am trying to have the output of a SELECT query update into the newly created tblFileType table. I have tried among other things:
UPDATE tblFileType
INNER JOIN
(SELECT DISTINCT fileType FROM tblFiles) as X
SET typeName = fileType
but I always seem to get 0 row(s) affected.
When I run
SELECT DISTINCT fileType
FROM `tblFiles`
I get Showing rows 0 - 22 (~23 total, Query took 0.0074 sec)
I know this must be simple, but why is the UPDATE query not affecting 23 rows?
You need to add a JOIN condition like ON t1.fileType = x.fileType as follows:
UPDATE tblFileType t1
INNER JOIN
(
SELECT DISTINCT fileType
FROM tblFiles
)as X ON t1.fileType = x.fileType
SET t1.typeName = X.fileType
Update: Since the table tblFileType is blank, you will need to use INSERT something like:
INSERT INTO tblFileType(typeName )
SELECT DISTINCT fileType
FROM tblFiles
WHERE -- a condition here
you just want to populate the table - not update anything in there (especially since nothing exists yet)
INSERT INTO tblFileType(typeName )
SELECT DISTINCT fileType FROM tblFiles