I have mysql table Vegetables
with fields price
and unit
. Unit can be 1
or 2
, it means kilogram (1
) or ton (2
). It is unit weight. Price depends on unit, so price=200
with unit=1
is less then price=20
with unit=2
, because 1 ton = 1000 kilogram
Q: I have query like ... ORDER BY 'price' ACS
. How to make order by price
that depends on unit
field of the same table?
Just no idea how to do that..
On words it is like ... ORDER BY {if(this.unit==1):price;else:price*1000} ACS
I am using yii2 for this project: Vegetable::find()->addOrderBy('price asc');
You use column-ref to reference a column (database field), optionally qualified by a table and/or schema name. The column reference comprises the data type of the database field that the column represents (see Data Types).
To select rows using selection symbols for character or graphic data, use the LIKE keyword in a WHERE clause, and the underscore and percent sign as selection symbols. You can create multiple row conditions, and use the AND, OR, or IN keywords to connect the conditions.
The WHERE clause is used in the selection of rows according to given conditions whereas the HAVING clause is used in column operations and is applied to aggregated rows or groups.
SELECT *
FROM Vegetables
ORDER BY IF(unit=1,price,price*1000) ACS
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With