i want to get maximum of price from table laptop in yii2.
how can i do that?
i used below code
<?
Laptop::find()->max('price')
?>
but it seems not working :(
how can i access this data.
and in other type i coded before like below
<?
$min=Laptop::find()->select('min(price)');
?>
but it s not working again ..
how to add this query ??
Tecnically you don't need an Active Recordefor get the max or min result you could try using active Query
$max = (new \yii\db\Query())
->from('laptop_table')
->max('price');
echo $max;
If you have the error
Missing argument 1 for yii\db\Query::max()
is because you must pass the column name for calculate the max
anyway the code above is equivalent to
$max = Laptop::find()->max('price');
Like below :
echo $max_price = Laptop::find()->max("price");
Multi select and max price in the product table with model product:
$product = \common\models\Product::find()->where(['status'=> 1])->andWhere(['>','count',0])->max('price');
Related
I want to get data that Price is greater than or equal to a specified value (taken from the input).
$minPrice = $request->min_price;
$maxPrice = $request->max_price;
$value = Dproduct::where(['status' => 1])->where('price', '>', $minPrice)->get();
But the Price is JSON in database, I can't get it. Currencies such as: VND, USD, TWD,...
All answers are respected!
Thanks so much!
if you are using laravel 5.7 or greater then you may be able to select within the JSON. See the docs here for more details.
Note: for this to work you must use json data type for your field in database.
$minPrice = $request->min_price;
$maxPrice = $request->max_price;
$value = Dproduct::where(['status' => 1])->where('price->USD', $minPrice)->get();
I have the following SQL statement:
$query = "SELECT item, COUNT(*) as number FROM shop GROUP BY item";
This will give me the following result:
item number
item1 23
item2 15
item3 4
I want to use this to make menu items, so normally the menu would look:
item1
item2
item3
But I want to do a check if an item has less than 10 records, that I don't want to display this item.
So in this example, the menu would be like:
item1
item2
Any idea how to achieve this?
I would like to do this in PHP because I need all the items in the query but will only want to show them which are greater then 10 and need the other items later on.
If you want to do this in PHP then you can do like this
function filterArray($value){
return ($value.number > 10);
}
$filteredArray = array_filter($yourDBArray, 'filterArray');
foreach($filteredArray as $k => $v){
//your desired array
}
In terms of speed Mysql option is good as suggested above.
Just change your query from
SELECT item, COUNT(*) as number FROM shop GROUP BY item
to
SELECT item, COUNT(*) as number FROM shop GROUP BY item HAVING number>=10
As you really need to perform this in PHP you could use array_filter() which, using a closure, will remove items which number is less than 10:
$more_than_ten = array_filter($items, function ($i) { return $i['number'] >= 10; });
Doing it with SQL would be a better solution (about performances). In case you'd need it, you could use the HAVING clause (you can't perform a WHERE number >= 10):
SELECT
item,
COUNT(*) as number
FROM shop
GROUP BY item
HAVING number >= 10
I noticed php is tagged. For the sake of options, here's how I'd go about separating the unneeded data in php if you were to get it from the database as-is:
foreach ($data as $item) {
$num = (int) $item['number']; // force of habit
if ($num >= 10) {
// display it
}
}
I'd probably separate the data at the database step, but this works if it's the route you want to take.
There is two options to filter the data so only the rows with more then 10 will appear.
At the SQL query
__
SELECT item, COUNT(*) as number FROM shop GROUP BY item HAVING number > 9
This will cause you to recieve only the requested rows from the database
Filter with PHP - every time you want to print the menu or testing it out, just can the value of 'number' in the array reutrned from the query. You can also allocate new array and insert all the values that contains 'number' that bigger then 10.
I try to make a "getThreeWorks" method with Laravel. Each view post to its 'orientation'. It is stored in the same table. For example, "Work 1 has the web orientation, Work 2 has the 2D orientation and Work 3 has the 3D orientation".
At the end of each post, I would like to propose a link to 3 other works (in a random order).
So I would like a link to a work that has the "web" orientation, another that has the "2D" orientation and one that has the "3D" orientation.
I can't get SQL query at all. Can you help me ? Thank you !
public function getThreeWorks()
{
$workFrom3D = Work::where('orientation', '3D')->inRandomOrder->limit(1)->get();
$workFrom2D = Work::where('orientation', '2D')->inRandomOrder->limit(1)->get();
$workFromWeb = Work::where('orientation', 'web')->inRandomOrder->limit(1)->get();
}
Can't you remove the limit(1) and instead use a group by orientation? Then you get one of each.
You can create a new collection with your current code.
$work = collect([$workFrom3D, $workFrom2D, $workFromWeb]);
If i understood, you want show some "posts" on footer of each post, right ? you can try this:
public function getThreeWorks()
{
$workFrom3D = Work::where('orientation', '3D')->get();
$workFrom2D = Work::where('orientation', '2D')->get();
$workFromWeb = Work::where('orientation', 'web')->get();
$randomFrom3d = $workFrom3D->random();
$workFrom2D = $workFrom2D->random();
$workFromWeb = $workFromWeb->random();
}
also, you should include one more condition on your where, to avoid repeat the same post on page where will show this list, like:
$workFromWeb = Work::where('orientation', 'web')
->where('post_id','!=', $actualPostId)->get();
I want to select maximum an field which is integer in my db.
So, First I try the orthodok's way :
SELECT max(a.nomor_surat) as max FROM request a
WHERE YEAR(a.tanggal_persetujuan) = YEAR(CURDATE())
Works. (In my case : max = 3)
Now, using AR,
Request::find()->select('max(nomor_surat) as max')->where(['YEAR(tanggal_persetujuan)' => 'YEAR(CURDATE())' ])->scalar();
I got max = 1,
How AR interpretatioin them correctly ?
Pleas Advice.
Thanks
Request::find()
->andWhere(['YEAR([[tanggal_persetujuan]])' => new \yii\db\Expression('YEAR(CURDATE())')])
->max('[[nomor_surat]]');
I have a custom post type created in my WordPress blog called 'reviews'. Inside this I have a custom field with a number out of 5.
I am trying to add all of these numbers together and then divide by the amount of posts to get an aggregate score.
I am attempting to use this code...
<?php
$total_score=0;
$meta_key = 'target';//set this to your custom field meta key
$allscores=$wpdb->get_col($wpdb->prepare("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = %s", $meta_key));
foreach ($allscores as $score) {
$total_score = $total_score + $score;
}
echo 'Total score is '.$total_score;
?>
But I am getting a result of 0, can anyone see where I am going wrong?
Turns out it was me all along, the numbers entered in the database were spelt out alphabetically rather than numerical so the 0 result I was receiving was technically correct :)