How do I fix my code so it runs correctly? It keeps telling me I have Error Code:
Error Code: 1054. Unknown column 'Number_Of_Ticketed_Events' in 'order clause').
Below is my code
SELECT Venue.VenueID, Venue.Capacity, VenueName,
COUNT(*) AS Number_Of_Ticketed_EventsĀ
FROM (
(Events INNER JOIN Non_Ticketed_Events)
INNER JOIN Venue ON Venue.VenueID = Events.VenueID
)
GROUP BY Events.VenueID
ORDER BY Number_Of_Ticketed_Events DESC;
Each JOIN needs telling which columns to use to make the join between the 2 tables
SELECT Venue.VenueID, Venue.Capacity, VenueName,
COUNT(*) AS Number_Of_Ticketed_Events
FROM Events e
INNER JOIN Non_Ticketed_Events nt ON Events.someColumn = nt.someColumn
INNER JOIN Venue ON Venue.VenueID = Events.VenueID
. . .
I can't figure out why I keep getting error #1054 - Unknown column 'Filename' in 'field list'
SELECT 'R' AS Type, er.ID, er.RecipeID AS RecipeID,
RecipeName AS Name,er.Quantity,UnitID,
SUBSTRING_INDEX(PFCC,',',1) AS Protein,
SUBSTRING_INDEX(SUBSTRING_INDEX(PFCC,',',2),',',-1) AS Fat,
SUBSTRING_INDEX(SUBSTRING_INDEX(PFCC,',',-2),',',1) AS Carbs,
SUBSTRING_INDEX(PFCC,',',-1) AS Calories,
Rating, AvgRating, AvgCount, 0 AS SpecificID,
COALESCE(rimg.Filename,'0.png') AS DefaultImage,
s.Price, s.MaxQuantity
FROM `event-recipe` er
LEFT JOIN(
SELECT r.ID,RecipeName,
SUM(Quantity) AS Quantity,
IFNULL(Rating,0) AS Rating
FROM recipe r
LEFT JOIN(
SELECT RecipeID, Filename
FROM recipe_image
WHERE IsDefault) rimg
ON r.ID=rimg.RecipeID
LEFT JOIN `recipe-ingredient` ri
ON r.ID=ri.RecipeID
LEFT JOIN(
SELECT RecipeID,Rating
FROM `recipe-rating`
WHERE CustomerID=2) rr
on r.ID=rr.RecipeID
GROUP BY r.ID) r ON er.RecipeID=r.ID
LEFT JOIN(
SELECT RecipeID, GROUP_CONCAT(Value ORDER BY Nutr_No) AS PFCC
FROM nutrient_view_ingredient nv
WHERE Nutr_No IN (203,204,205,208)
GROUP BY RecipeID) n
ON r.ID=n.RecipeID
LEFT JOIN sell_recipe s ON (er.EventID=s.EventID AND s.RecipeID=er.RecipeID)
LEFT JOIN(
SELECT RecipeID, COUNT(RecipeID) AS AvgCount, ROUND(AVG(Rating),1) AS AvgRating
FROM `recipe-rating`
GROUP BY RecipeID) avgrate
ON r.ID=avgrate.RecipeID
WHERE er.EventID=53
The issue is in how you've used your parentheses to group your table, as well as the fact that you never select filename in the inner join.
The first big join, which you've named r is the table that has reference to filename. No where in that table do you select rimg.fileName so in the outer query, you can't even call r.fileName. To fix it, change the line:
SELECT r.ID, RecipeName
To:
SELECT r.ID, RecipeName, rimg.FileName
Then, in your outer query, you can select r.FileName.
SELECT IFNULL(oea.`name`,op.name) AS "Consultation"
FROM `a` ca
JOIN `b` oea ON ca.`consultation_id` = oea.id AND oea.status = 1
LEFT OUTER JOIN c oeal ON oea.`location_id` = oeal.id
WHERE oea.employee_profile_id IN (
SELECT profile_id
FROM d pp
WHERE pp.status='1'
UNION
SELECT op.id
FROM e op
WHERE op.status
) AND
ca.status = 1
I am getting this error
Unknown column 'op.name' in 'field list'
Is there any possibility to use op.name outside the scope?
JOIN the subquery in the WHERE clause instead of using the IN predicate. This way you can select the name from it.
But you didn't select the column name from in the subquery. You selected only the profile_id, you have to select it with the profile_id. Something like:
SELECT
IFNULL(oea.`name`, op.name) AS Consultation
FROM `a` ca
JOIN `b` oea ON ca.`consultation_id` = oea.id AND oea.status = 1
INNER JOIN
(
SELECT profile_id, name
FROM d pp
WHERE pp.status='1'
UNION
SELECT op.id, name
FROM e op
WHERE op.status
) AS op ON oea.employee_profile_id = op.profile_id
LEFT OUTER JOIN c oeal ON oea.`location_id` = oeal.id
WHERE ca.status = 1;
I assumed that the two tables have the name column, if one of them didn't have it, you have to use null instead.
The following query fails to be parsed:
select t.ename, t.received, d.loc from
(
select e.ename, eb.received , e.deptno
from emp e left outer join emp_bonus eb
on
e.empno=eb.empno
)
as t
join dept as d on d.deptno = t.deptno;
with the error:
Column 'deptno' in field list is ambiguous
But this query is parsed succesfully:
select t.ename, t.received, d.loc from
(
select e.ename, eb.received , e.deptno
from emp e left outer join emp_bonus eb
on
e.empno=eb.empno
)
as t, dept as d where d.deptno = t.deptno
I only changed the JOIN ON to t, dept where
Why does the first version fail?
the first query runs perfectly in mysql version 5.1
see the demo
So the other day, I asked this question about how to combine three complex queries and found a way to do it. Now I'm trying to use those queries to update a field in the users table and can't find a way to make it work. Here's the query:
update users set field_sum =(
select sum(field_sum) from (
select sum(field_one) as field_sum
from t_a join t_b on (t_a.bid = t_b.id) where t_b.user_id=users.id
union all
select sum(field_two) as field_sum
from t_c join t_d on (t_c.did = t_d.id) where t_d.user_id=users.id
union all
select sum(field_three) as field_sum
from t_e where t_e.user_id=users.id
) as field_sumT
)
When I try to run it, I get the following error: ERROR 1054 (42S22): Unknown column 'users.id' in 'where clause'. When I try removing the .user_id=users.id bit from each where clause, it will run but ends up with the total sum of field_sum, not just the field_sum for that user. Is there any way to accomplish this?
Use:
UPDATE USERS u
LEFT JOIN (SELECT t_b.user_id,
SUM(field_one) as field_sum
FROM t_a
JOIN t_b on t_a.bid = t_b.id
GROUP BY t_b.user_id) a ON a.user_id = u.id
LEFT JOIN (SELECT t_d.user_id,
SUM(field_two) as field_sum
FROM t_c
JOIN t_d on t_c.did = t_d.id
GROUP BY t_d.user_id) b ON b.user_id = u.id
LEFT JOIN (SELECT t_e.user_id,
SUM(field_three) as field_sum
from t_e
GROUP BY t_e.user_id) c ON c.user_id = u.id
SET field_num = COALESCE(a.field_sum, 0) + COALESCE(b.field_sum, 0) + COALESCE(c.field_sum, 0)
Caveat
This will set any users with no records in the supporting rows to have a field_sum value of zero. Do you only want to update those with a record in at least one of those tables?