Select Count Row then update value - mysql

Just need some idea
Is there a way I can Select the count then update its value?
Here is my code to be edit.
I'm getting the number of new message in my table message
select count(id) as new from message where owner_id = '1' and status = '1' and isRecieve = '0'
When I get the number of message I want it to set all the message got from select query to isRecieve = 1
is there a way once I query I updte all isRecieve to 1?
like
select count(id) as new from message where owner_id = '1' and status = '1' and isRecieve = '0' then update isRecieve = 1
Thanks.

You should not select COUNT for your case. In fact, what your are trying to achieve is:
UPDATE corresponding records
Discover, how many rows was touched
This can be resolved with:
UPDATE
message
SET
isReceive = '1'
WHERE
owner_id = '1'
AND
status = '1'
AND
isRecieve = '0'
and then either do
SELECT ROW_COUNT()
to retrieve affected rows in SQL, or do something like mysqli_affected_rows() in application (function is in PHP, but the same thing is available in any mysql connection driver in any language).

UPDATE message set
isRecieve = 1 where
owner_id = '1' and
status = '1' and
isRecieve = '0' and
exists (
select 1 from message where
owner_id = '1' and
status = '1' and
isRecieve = '0'
group by id having count(*) = 1
)

Related

mysql UPDATE statement where pricelist.import_date = ( SELECT max(pricelist.import_date) )

im trying to complete an update in mysql where it selects only the highest import_date and i cant seem to get it to work. the below statement updates both prices
UPDATE material, pricelist
SET price = '23'
WHERE material.id = 1936 AND
material.id = pricelist.material_id AND
pricelist.import_date = (SELECT max(pricelist.import_date))
Does this do what you want?
UPDATE pricelist pl
SET pl.price = '23'
WHERE pl.material_id = 1936
ORDER BY pl.import_date DESC
LIMIT 1;

AND clause on multiple values for same column

How can I fix this query?
SELECT COUNT(*)
FROM user_notifications
WHERE notification_status = '0'
AND notification_sent_to = ?
AND notification_category = 'ticket reply'
AND notification_category = 'groupdoc'
I need to count result filtering only notification category for ticket reply and groupdoc.
Many thanks for your help.
A particular column can have only one value in a given row. So the following condition can never be True
notification_category = 'ticket reply'AND notification_category= 'groupdoc'
When people think I need rows with value of 'a' and rows with value of 'b', the actual logical operator to use is OR
SELECT COUNT(*) FROM user_notifications WHERE notification_status = '0' AND notification_sent_to=? AND
(notification_category = 'ticket reply' OR notification_category= 'groupdoc')
A column can not have 2 values at same time. I think what you want is an OR
SELECT COUNT(*)
FROM user_notifications
WHERE notification_status = 0 AND notification_sent_to=?
AND (notification_category = 'ticket reply' OR notification_category= 'groupdoc')
Use in:
SELECT COUNT(*)
FROM user_notifications
WHERE notification_status = 0 AND
notification_sent_to = ? AND
notification_category IN ('ticket reply', 'groupdoc');

MYSQL SELECT is slow when crossing multiple tables

I have a mysql query which is to return the only 1 record that need to cross multiple table. However, the mysql query is slow when executing.
Query:
SELECT *,
(SELECT TreeName FROM sys_tree WHERE TreeId = Mktg_Unit_Booking.ProjectLevelId) AS PhaseName,
(CASE WHEN ProductType = 'U' THEN (SELECT UnitNo FROM prop_unit pu WHERE pu.UnitId = mktg_unit_booking.UnitId)
ELSE (SELECT BayNo FROM prop_car_park pcp WHERE pcp.CarParkId = UnitId) END) AS UnitNo,
(SELECT CustomerName FROM mktg_customer mc WHERE mc.CustomerId = mktg_unit_booking.CustomerId) AS CustomerName
FROM Mktg_Unit_Booking
WHERE IsDeleted <> '1' AND IsApproved = '1'
AND UnitId = 1110 AND ProductType = 'U'
ORDER BY UnitNo
I have run EXPLAIN in the query and I got this:
Any other suggestion on how to improve the speed of the query?
Thank you!
you are doing the cross product, instead of that you should use join.
Don't use sub-queries in select statement instead use proper join on Mktg_Unit_Booking in after from statement.
you query should something look like :
select
sys_tree.TreeName AS PhaseName,
case
WHEN Mktg_Unit_Booking.ProductType = 'U' then prop_unit.UnitNo
else prop_car_park.BayNo
end as UnitNo,
mktg_customer.CustomerName AS CustomerName
FROM Mktg_Unit_Booking
left join sys_tree on sys_tree.TreeId = Mktg_Unit_Booking.ProjectLevelId
left join prop_unit on prop_unit.UnitId = Mktg_Unit_Booking.UnitId
left join prop_car_park on prop_car_park.CarParkId = Mktg_Unit_Booking.UnitId
left join mktg_customer on mktg_customer.CustomerId = Mktg_Unit_Booking.CustomerId
WHERE IsDeleted <> '1' AND IsApproved = '1'
AND UnitId = 1110 AND ProductType = 'U'
ORDER BY UnitNo;
I have assumed that each table consists of only 1 matching tuple. If there are more then your logic needs to be modified.

Update same table with IF ELSE Condition in MySQL

I am trying to update the user_status filed with condition:
update pm_users
set user_status = if (
(select u.user_status from pm_users u where u.user_id = 3
) = '1', '0', '1' )
where user_id = 3
means if user_status = 1 then update the status with 0 and if user status is 0 then with 1.
I am getting the error: You can't specify target table 'pm_users' for update in FROM clause
I think it means i can't use this query like above for the same table? I am not sure.
Please help me to move on the right way, and make me correct.
try the following
update pm_users
set user_status = case when user_status = 0 then 1 else 0 end
where user_id = 3
Rather then maybe try CASE
update pm_users
set user_status = CASE WHEN user_status = '1' THEN '0' ELSE '1' END
WHERE user_id = 3
SQL Fiddle DEMO

How to get individual count using this MySql Query

I have table and this table contain result column with some entries. I just wanted to know how to get individual count these entries using MySql Query like this (see result required)...
also
(see the result column image and query) or helps are definitely appreciated
Result Column Image
Query
SELECT cpd.result FROM cron_players_data cpd
WHERE cpd.`status` = '1'
AND (cpd.`result` = '1' OR cpd.`result` = '2')
AND cpd.`player_id` = '81'
Result Required
result count
1 2
2 6
SELECT cpd.result, count(*) FROM cron_players_data cpd
WHERE cpd.`status` = '1'
AND (cpd.`result` = '1' OR cpd.`result` = '2')
AND cpd.`player_id` = '81' group by cpd.`result`
SELECT result, COUNT(result) `count` FROM
(
SELECT cpd.result result
FROM cron_players_data cpd
WHERE cpd.`status` = '1'
AND (cpd.`result` = '1' OR cpd.`result` = '2')
AND cpd.`player_id` = '81'
) a
GROUP BY result