I have the following SQL, which gives me the error that this union tabled called brokeredTable is not updateable.
UPDATE (SELECT chid,brokered,bid,uid,rate FROM spot_channels UNION SELECT tid,brokered,bid,uid,rate FROM tremor_tags) as brokeredTable SET brokered = 1, rate = 5, bid = 5, uid = 7 WHERE chid = 110399
As you can see the SQL is pretty simple, instead of running two update statements on two different tables I wanted to Union them into one set and then run the update against that set of data. Which apparently I cannot do this way.
Any Suggestions? Again I just want one SQL statement to accomplish this.
"The SQL UNION operator combines the result of two or more SELECT statements"
That query doesn't even have the UPDATE sintax. UPDATE table SET column = 'value'.
You should search before ask:
Performing an UPDATE with Union in SQL
Related
I want update query after select query.
first SELECT query , SELECT * from be_settings order by seq desc limit 1
this select query is based on seq, retrieve the last inserted record.
I want update on this record.
second Update Query, UPDATE be_settings set appgubun ='CCTV', running ='on'.
how to update query after select query?
thanks.
You can do this via an update with a subquery:
UPDATE be_settings
SET appgubun = 'CCTV', running = 'on'
WHERE seq = (SELECT t.max_seq FROM (SELECT MAX(seq) AS max_seq FROM be_settings) t );
The subquery in the WHERE clause is necessary because it involves the be_settings table, which is the target of the update. The following would give an error:
WHERE seq = (SELECT MAX(seq) FROM be_settings)
You need to do
UPDATE be_settings set appgubun ='CCTV', running ='on' where seq= '4'
Assuming your there is seq column on be_settings and it is primary key.
and 4 is the last inserted id you are getting from select query.
if you are getting that seq in any variable based on programming language then you have to use that particular variable.
You have to this if you do not want to use subquery.
This must be fairly straight forward, as I tend to use ORMs I don't have to get my hands dirty often and am therefore struggling!
I have a database and want to get several fields from a table, that bit is easy..
SELECT main_table.registration_number, main_table.registered_name FROM main_table;
I want to filter the results based on another table, which is also easy..
SELECT second_table.registration_number FROM second_table WHERE this_field = '' AND that_field = '0';
Now the problem is I want to run the first query based on the second queries result set, I was thinking something like this:
SELECT main_table.registration_number, main_table.registered_name FROM main_table WHERE main_table.registration_number IN (SELECT * FROM second_table WHERE this_field = '' AND that_field = '0');
This gives me: Error Code: 1241. Operand should contain 1 column(s)
Am I handling this completely wrong?
Your subquery should do something like below,
(select * from table) in subquery is not what you really need to do your
so the subquery should return one column
(SELECT registration_number FROM second_table WHERE this_field = '' AND that_field = '0');
You cannot have multiple columns being returned in a subquery like
that, doing so it will result in such error
You have to select a column
SELECT main_table.registration_number, main_table.registered_name FROM
main_table WHERE main_table.registration_number IN (SELECT
registration_number FROM second_table WHERE this_field = '' AND
that_field = '0');
I want to combine three different UPDATE queries, in different table, with different WHERE conditions, into a single mySql query. Is it possible?
Reason: send 1 request to mySql server is faster than sending 3
requests separately :)
UPDATE client SET clientCount = clientCount + 1 WHERE clientType = 2
UPDATE storage SET soldItem = soldItem + 1 WHERE itemType = 5
UPDATE employee SET doWork = 1, totalSale = totalSale + 1 WHERE employeeId = 12
The UPDATE statements are independent, and are not related to each other. I tried to find some solution, however, the
UPDATE client, storage, employee SET client.clientCount = ... , storage.soldItem = ... WHERE ... ? ? ? ...
does not fit to my case as my three UPDATE statements are independent...
Is it possible to combine 3 independents queries into a 1 query?
Create a stored procedure with all these update statements and call the stored procedure from your code.
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
I have 2 tables: tbl_taxclasses, tbl_taxclasses_regions
This is a one to many relationship, where the main record ID is classid.
I have a column inside the first table called regionscount
So, I create a Tax Class, in table 1. Then I add regions/states in table 2, assigning the classid to each region.
I perform a SELECT statement to count the regions with that same classid, and then I perform an UPDATE statement on tbl_taxclasses with that number. I update the regionscount column.
This means I'm writing 2 queries. Which is fine, but I was wondering if there was a way to do a SELECT statement inside the UPDATE statement, like this:
UPDATE `tbl_taxclasses` SET `regionscount` = [SELECT COUNT(regionsid) FROM `tbl_taxclasses_regions` WHERE classid = 1] WHERE classid = 1
I'm reaching here, since I'm not sure how robust MySQL is, but I do have the latest version, as of today. (5.5.15)
You could use a non-correlated subquery to do the work for you:
UPDATE
tbl_taxclasses c
INNER JOIN (
SELECT
COUNT(regionsid) AS n
FROM
tbl_taxclasses_regions
GROUP BY
classid
) r USING(classid)
SET
c.regionscount = r.n
WHERE
c.classid = 1
Turns out I was actually guessing right.
This works:
UPDATE `tbl_taxclasses`
SET `regionscount` = (
SELECT COUNT(regionsid) AS `num`
FROM `tbl_taxclasses_regions`
WHERE classid = 1)
WHERE classid = 1 LIMIT 1
I just needed to replace my brackets [] with parenthesis ().