I have this SQL Code:
SELECT * FROM `clients_branches`
WHERE NULLIF(clients_branches.invoice_email, '') IS NULL
GROUP BY client_code
HAVING COUNT(*) = 1
It returns all rows which appears only once in the database, also it returns only the ones with no email set. Now I need to apply UPDATE function to all of this select statement. How could I do it? I need to set clients_branches.invoice_send to 0 for all these rows.
I can't seem to use HAVING COUNT on UPDATE statement like this:
UPDATE `clients_branches`
SET clients_branches.invoice_send = 0
WHERE NULLIF(clients_branches.invoice_email, '') IS NULL
HAVING COUNT(*) = 1
Without HAVING COUNT I will change all of the rows which repeats at least once in this table. And I need to change only the ones with count = 1.
You could use a join for allow the use of the table for update and the result of your query
update `clients_branches`
JOIN
(
select client_code, count(*)
FROM `clients_branches`
WHERE NULLIF(clients_branches.invoice_email, '') IS NULL
group by client_code
HAVING COUNT(*) = 1
) t on t.client_code = `clients_branches`.client_code
set clients_branches.invoice_send = 0
;
Related
Multiples posts asking about it already exist, solutions have been given and they worked for my previous queries.
But it won't work now that I try the same query to SELECT inside an UPDATE from the same table.
Here are the differents queries I tried :
UPDATE usersDB.random
SET total_partners =
(
SELECT total_partners FROM usersDB.random
WHERE year = 2022
)
WHERE uid = 1
and
UPDATE usersDB.random
SET total_partners =
(
SELECT total_partners
FROM usersDB.random
AS x
WHERE year = 2022
)
WHERE uid = 1
But I get this error message everytime :
Error Code: 1093. You can't specify target table 'random' for update
in FROM clause
The same query but with two differents table work tho :
UPDATE usersDB.random
SET total_partners =
(
SELECT COUNT(*)
FROM usersDB.partners AS x
)
WHERE (month = MONTH(CURRENT_DATE()) and year = YEAR(CURRENT_DATE())) OR uid = 1
How can I make it work when it's the same table ? Version of Mysql is 8.0
Here is how the table random looks like https://i.imgur.com/HYSS5DJ.png
I use Mysql Workbench to send the queries
Because you did not provide a simple test data Create Table/Insert Into (copypaste text) script I had to experiment with my own table scheme. See this with aTemp temporary table how I use two-level inner table.
Update person Set
city=(
Select Concat(now(),"-",aTemp.val1) from
(select count(*) as val1 from person) as aTemp
)
Where id=4
So out of my mind it may translate to this query.
UPDATE usersDB.random
SET total_partners=(
Select aTemp.val1 From
(SELECT count(*) as val1 FROM usersDB.random WHERE year=2022) aTemp
)
WHERE uid = 1
I have table where there is column named uid , It uses Autoincrement and its updated with 1,2,3 etc. Now I have cron job that deleted rows older than 2 days.So now my uid column is 2345 to n..I want to reset it to 1 to n again.I tried below code
UPDATE `tv` SET `uid` = ''
I was thinking to loop through all rows and update uid via php script, Is there any other alternative with single SQL command ?
You can try something like this:
UPDATE `tv` t
set t.`uid` = (SELECT count(*)
from `tv` s
WHERE t.`uid` >= s.`uid`)
This will count how many uid's are there that are smaller or equal then the one being updated, so when the first UID, lets say 2345 is being updated, there is only 1 uid that is smaller/equal to him so it will get the value 1 and so on...
EDIT: Try this-
UPDATE `tv` t
INNER JOIN(SELECT s.`uid`,count(*) as cnt
from `tv` s
INNER JOIN `tv` ss
ON(s.`uid` >= ss.`uid`)
GROUP BY s.`uid) tt
ON(t.`uid`=tt.`uid`)
SET t.`uid` = tt.cnt
Why don't decrease the uid by:
update tv set uid = uid -1
I'm trying to update multiple rows in MySQL by selecting the highest number and adding 1 to it to generate the next one.
UPDATE orders SET delivery_number = (
SELECT new_number FROM (
SELECT (
MAX(delivery_number) + 1
) AS new_number FROM order_invoice
) AS result
) WHERE delivery_number = 0 AND invoice_number != 0;
The query above only seems to SELECT once, then use the same number for each update.
How can I force it to scan the table again for the highest number on each update?
update orders, (select #n:=max(delivery_number) from order_invoice) n
set delivery_number = #n:=#n+1
where delivery_number = 0 and invoice_number != 0;
I have a MySQL view with the fields id and set. Because it's a view, most ids are repeated and have duplicate entries. For example, and id = 120158 may have 5 rows, 3 where set = A and 2 where set = B. I want to run a query off of the view to display the number of rows each id has associated with its corresponding sets.
I tried:
SELECT `id`,
`set`,
(SELECT COUNT(set)) AS `CountOfSet`
FROM `view1`
However, this simply returns the same view (duplicate rows still exist) with CountOfSet equal to 1 for every row. Any ideas?
You should be grouping your results by id and set to get the desired result:
SELECT `id`, `set`, COUNT(*) AS `CountOfSet` FROM `view1`
GROUP BY `id`, `set`
This would return the results as
120158 A 3
120158 B 2
You need to group your data using GROUP BY clause:
GROUP BY `id`, `set`
Composing all together:
SELECT `id`,
`set`,
COUNT(*) AS `CountOfSet`
FROM `view1`
GROUP BY `id`, `set`
Hi Is there any option in mysql query to return NULL is there is no such row exists in database otherwise return the row data. Here is my query
SELECT DISTINCT estimateResourceMthObj
FROM EstimateResourceMth estimateResourceMthObj
WHERE estimateResourceMthObj.estimateResource.id IN
(
SELECT estimateResourceObj.id
FROM EstimateResource estimateResourceObj
WHERE estimateResourceObj.estimateSub.id = 7
)
AND estimateResourceMthObj.monthOrder >= 0
AND estimateResourceMthObj.monthOrder < 7;
I need to return the result of this query null if there is no such row exists otherwise i need to select that row. Is it possible to retrieve this in a single query?
As a tricky solution you can add UNION SELECT NULL LIMIT 0,1 to the end if you expect only one row, as you specified. So the result will always have one value - NULL if there are no matches in table and estimateResourceMthObj if there are.
SELECT DISTINCT estimateResourceMthObj
FROM EstimateResourceMth estimateResourceMthObj
WHERE estimateResourceMthObj.estimateResource.id IN
(
SELECT estimateResourceObj.id
FROM EstimateResource estimateResourceObj
WHERE estimateResourceObj.estimateSub.id = 7
)
AND estimateResourceMthObj.monthOrder >= 0
AND estimateResourceMthObj.monthOrder < 7
UNION
SELECT NULL
LIMIT 0,1;