MySQL Update with Subquery - mysql

I've got an annoying issue with an update query I'm trying to get working... The following statement SHOULD update channels.media_view_count to the result of the subquery (for all channels).
UPDATE channels c
SET c.media_view_count = (
SELECT SUM(t.view_count)
FROM (
SELECT DISTINCT m.viewkey, m.view_count
FROM media m
INNER JOIN participants p ON m.id = p.medium_id
WHERE p.user_id = c.id AND m.is_viewable = 1
AND (p.pending = 0)
) AS t
);
The subquery works fine independently (when specifying an actual id for c.id, like 47778 or whatever), but when I execute this statement, I get:
ERROR 1054 (42S22): Unknown column 'c.id' in 'where clause'
I thought I would be able to access the channels table (aliased as c) from within the subquery? Am I missing something or am I totally wrong here?
Any and all help is appreciated :)
Thanks,
Jeff

UPDATE channels c, (
SELECT t.user_id, SUM(t.view_count) cnt
FROM (
SELECT DISTINCT p.user_id, m.viewkey, m.view_count
FROM media m
INNER JOIN participants p ON m.id = p.medium_id
WHERE m.is_viewable = 1
AND (p.pending = 0)
) AS t GROUP BY t.user_id ) temp
SET c.media_view_count = temp.cnt
WHERE c.id = temp.user_id
Try like this... Did not test it though :) ..
Conceptually, it should work

Related

Why does Mysql keep telling me there is an unknown column even though I've declared the alias?

I have this mySQL query here which is supposed to give me the count of all the friends of a user. However, I need to further filter this to only include confirmed friend requests which are designated by 'status = 20'.
When I try to do this in the WHERE clause, I keep getting the following error
Error Code: 1054. Unknown column 'f.status' in 'field list'.
My query is here:
Select
u.uuid, u.nick,
count(distinct f.friend_uuid) as number_of_friends, f.status
From
user u
Left Outer Join (
Select user_uuid, friend_uuid from user_friend
Union All
Select friend_uuid, user_uuid from user_friend
) as f
On u.uuid = f.user_uuid
WHERE f.status = '20'
GROUP BY 1,2,4
HAVING number_of_friends >=10;
your query has 3 issues.
You can use alias in this way. Alias will be created after creation of result set. REsult set wiill use the HAVING CLause. So, the having clause must have the name in result set. This is corrected in below query.
You have used WHERE f.status = '20'. f is used as LEFT OUTER JOIN. If you use ouer join column in where clause then it will become inner join.This is corrected in below query.
The inner query with left outer join doesn't have status column in select. Therefore it is throwing error.
Select u.uuid, u.nick, count(distinct f.friend_uuid) as number_of_friends, f.status
From user u
Left Outer Join
(
Select user_uuid, friend_uuid, [STATUS_COLUMN REQUIRED] from user_friend
Union All
Select friend_uuid, user_uuid, [STATUS_COLUMN REQUIRED] from user_friend
) as f On u.uuid = f.user_uuid and f.status = '20'
GROUP BY 1,2,4
HAVING count(distinct f.friend_uuid) >=10;
Let me know if you still face any issue.

Group Concat from a select statement

I am not sure if my method is possible, but i'm trying to do a group_concat on a select statement that concats 2 fields. I get the error: Subquery returns more than 1 row each time. Can anyone help me as to a solution, or better way around this.
select t.recnum, (select group_concat((select concat(b.origtests,'^', d.name) as testing
from order_origtests b
join profile c on c.code = b.origtests
join department d on d.recnum = c.dept
)))
FROM order_ t
You don't put SELECT inside GROUP_CONCAT. It should be
select t.recnum, (
select group_concat(concat(b.origtests,'^', d.name))
from order_origtests b
join profile c on c.code = b.origtests
join department d on d.recnum = c.dept
) AS testing
FROM order_ t
Note that your subquery isn't correlated to anything in t, so you'll get the same testing column for every recnum.

Subquery for SQL

I need assistance in joining the two query statements together using subquery. I am confused on how I can combine the two together. I appreciate the help.
SELECT * FROM MEDICAL_PROCEDURE
JOIN PROCEDURE_CATEGORY ON medical_procedure.procedure_category_id = PROCEDURE_CATEGORY.PROCEDURE_CATEGORY_ID;
SELECT
Medical_procedure.medical_procedure_id,
COUNT(procedure_tool_supply.medical_procedure_id) AS Supply_Needed
FROM Procedure_tool_supply
JOIN Medical_Procedure on Procedure_tool_supply.medical_procedure_id = Medical_procedure.medical_procedure_id
GROUP BY Procedure_tool_supply.medical_procedure_id
HAVING COUNT(Procedure_tool_supply.medical_procedure_id) < 3;
Can't really test without test data, but this should work. Hopefully I figured out correctly what you're trying to do:
SELECT *
FROM
MEDICAL_PROCEDURE P
JOIN PROCEDURE_CATEGORY C ON
P.procedure_category_id = C.PROCEDURE_CATEGORY_ID
cross apply (
SELECT
COUNT(T.medical_procedure_id) AS Supply_Needed
FROM
Procedure_tool_supply T
where
T.medical_procedure_id = P.medical_procedure_id
GROUP BY
T.medical_procedure_id
HAVING
COUNT(T.medical_procedure_id) < 3
) T
It's not clear what you are trying to achieve. But if your intent is to include the derived Supply_Needed column from the second query on each row from the first query, and to restrict the rows returned to those that have a medical_procedure_id value returned by the second query, then...
you could do something like this:
SELECT mp.*
, pc.*
, ct.Supply_Needed
FROM MEDICAL_PROCEDURE mp
JOIN PROCEDURE_CATEGORY pc
ON mp.procedure_category_id = pc.PROCEDURE_CATEGORY_ID
JOIN ( SELECT pr.medical_procedure_id
, COUNT(ts.medical_procedure_id) AS Supply_Needed
FROM Procedure_tool_supply ts
JOIN Medical_Procedure pr
ON ts.medical_procedure_id = pr.medical_procedure_id
GROUP BY ts.medical_procedure_id
HAVING COUNT(ts.medical_procedure_id) < 3
) ct
ON ct.medical_procedure_id = mp.medical_procedure_id

Mysql: WHERE not working in subquery?

I'm trying to get the WHERE part of my subquery to work below. I can see that 'where event_id=..' is ambiguous because the parent query is looking at the same table.
Is it even possible to have a WHERE in a same-table subquery?
UPDATE tickets SET tickets.ticket_number = (
SELECT max_ticket
FROM (
SELECT (MAX(ticket_number)+1) AS max_ticket
FROM tickets
WHERE event_id=10045
)
AS sub_table
)
WHERE ticket_id=68
Any help really appreciated.
Possibly try it as a join
UPDATE tickets a
INNER JOIN
(
SELECT (MAX(ticket_number)+1) AS max_ticket
FROM tickets
WHERE event_id = 10045
) b
SET a.ticket_number = b.max_ticket
WHERE a.ticket_id = 68

case update, two tables and count

I want to perform a case update, unfortunately I am getting an error that tells me that am making an Invalid use of group function
Error Code: 1111. Invalid use of group function
update l, m
set l.requests = sum(
case when m.event = 'rRequested' then
m.id end )
where
l.id = m.id
or
update l, m
set l.requests = (
case when m.event = 'rRequested' then
count(m.id) end )
where
l.id = m.id
Any idea how can i fix this?
I could do a full select after the set, but i want to learn how to use (even if it's possible) the case update for aggregations...
You try to distinguish based on a m.event, and the result is a grouped value (count(m.id)). I think you should sum 0 or 1 based on your value.
update l
set l.request = (select sum(if m.event = "rRequested", 0, 1) from m where m.id = l.id))
See MySQL Update query with left join and group by for the topic.
EDIT:
The question's focus seems to be avoiding the full subselect. I think since there are no real restrictions on l that the database has to go through all lines of m with event="rRequested". So I could imagine going once through m grouping by l.id:
update l
inner join
(
select id,
count(*) as cnt
from m where m.event = "rRequested"
group by id
) as grp_m on grp_m.id = l.id
set l.request = grp_m.cnt
;
It sounds a bit strange that table m has many entries with the same id, but since you gave no example, it is hard to guess.