2

I have a problem with mysql alias.

I have this query:

SELECT (`number_of_rooms`) AS total, id_room_type, 
     COUNT( fk_room_type ) AS reservation , 
     SUM(number_of_rooms - reservation) AS result
FROM room_type
    LEFT JOIN room_type_reservation 
         ON id_room_type = fk_room_type
WHERE  result > 10
GROUP BY id_room_type

My problem start from SUM, cannot recognize reservation and then i want to use the result for a where condition. Like (where result > 10)

0

3 Answers 3

1

Not 100% but to the best of my knowledge you cant use aliases in your declarations, and thats why you are getting the column issue. Try this:

 SELECT (`number_of_rooms`) AS total, id_room_type,
     COUNT( fk_room_type ) AS reservation , 
     SUM(number_of_rooms - COUNT( fk_room_type ) ) AS result
 FROM room_type
     LEFT JOIN room_type_reservation 
         ON id_room_type = fk_room_type
 GROUP BY id_room_type
 Having SUM(number_of_rooms - COUNT( fk_room_type ) ) > 10
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for your answer but i have ready do this and returns me #1111 - Invalid use of group function
Lets see, one more try, it might be a bracketing issue. SELECT ('number_of_rooms') AS total, id_room_type, COUNT( fk_room_type ) AS reservation , SUM(number_of_rooms) - COUNT( fk_room_type ) AS result FROM room_type LEFT JOIN room_type_reservation ON id_room_type = fk_room_type GROUP BY id_room_type Having SUM(number_of_rooms) - COUNT( fk_room_type ) > 10
Thanks again but the results its not correct total id_room_type reservation result 20 10 201 3819 200 11 10 1990 10 22 100 900 22 25 0 22
Thanks for your time but i find the solution with nested select SELECT ( number_of_rooms ) AS total, id_room_type, c.reservation, SUM( number_of_rooms - c.reservation ) AS result FROM room_type LEFT JOIN ( SELECT number_of_rooms AS moutsela, fk_room_type, COUNT( fk_room_type ) AS reservation FROM room_type LEFT JOIN room_type_reservation ON id_room_type = fk_room_type GROUP BY id_room_type )c ON id_room_type = fk_room_type GROUP BY id_room_type LIMIT 0 , 30 but i ll try to find the way to optimize my query
that is why i wanted to se your table structures :)
|
1

To apply a predicate (filter condition) on the result of an aggregate function, you use a Having clause. Where clause expressions are only applicable to intermediate result sets created prior to any aggregation.

 SELECT (`number_of_rooms`) AS total, id_room_type,
     COUNT( fk_room_type ) AS reservation , 
     SUM(number_of_rooms - reservation) AS result
 FROM room_type
     LEFT JOIN room_type_reservation 
         ON id_room_type = fk_room_type
 GROUP BY id_room_type
 Having SUM(number_of_rooms - reservation) > 10

1 Comment

Thanks for your answer but SUM(number_of_rooms - reservation) AS result returns me #1054 - Unknown column 'reservation' in 'field list'
0

One way is to wrap it into one more SELECT

SELECT t.*, t.number_of_rooms - t.reservation AS result FROM
  (
    SELECT (`number_of_rooms`) AS total, id_room_type, 
     COUNT( fk_room_type ) AS reservation , 
     SUM(number_of_rooms - reservation) AS result
     FROM room_type
     LEFT JOIN room_type_reservation 
         ON id_room_type = fk_room_type
     WHERE  result > 10
     GROUP BY id_room_type
   ) t


1 Comment

I solved this in MySQL with a function here is the post: stackoverflow.com/questions/372961/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.