开发者

MySQL variables

开发者 https://www.devze.com 2023-03-30 14:52 出处:网络
I\'m currently in the process of learning MySQL, and I\'ve come to learning variables, however I\'m having an issue accessing whats saved in my variable; I\'m getting my variable contains with the fol

I'm currently in the process of learning MySQL, and I've come to learning variables, however I'm having an issue accessing whats saved in my variable; I'm getting my variable contains with the following command:

SELECT 
    @foods := `idFood`, `price`
from
    `foods`;

This seems to be working okay, and this is resulting a table, now I'm wantin开发者_如何转开发g to sort my table animals by the following command:

Select 
    *
From
    `Animals`
order by `Animals`.`Food consumption` * @foods.price
where @foods.idFood = `Animals`.`FoodType`

The Animals.FoodType is holding the id of the food, that the animal is consuming.

When running this, I'm getting an error in the last part, where I'm using the variable, I've been playing around with it for quite a while without any luck.


You should use one query with JOIN clause. Try this query -

SELECT a.* FROM animals a
LEFT JOIN foods f
  ON a.FoodType = f.idFood
ORDER BY a.`Food consumption` * f.price;

About the variables - variables in MySQL are scalar values, you cannot set an array into variable.

0

精彩评论

暂无评论...
验证码 换一张
取 消