I would like to be able to list 1000 results, however I am being limited to 20 due to the ASC Limit with the gem thinking sphinx.
How can I increase the ASC Limit 0, 20 to 0, 1000?
SELECT GEODIST(0.5904448859496816, -1.464156709498043, latitude, longitude) AS geodist, *
FROM `location_core`
WHERE `geodist` BETWEEN 0.0 AND 200000.0 AND `sphinx_deleted` = 0
ORDER BY `geodist` ASC
LIMIT 0, 20
Is this something I have to modify with MYSQL?
controller:
def index
location = Location.find_by_zipcode params[:zipcode]
latitude = location.latitude * Math::PI / 180
longitude = location.longitude * Math::PI / 180
location_ids = Location.search_for_ids(
:geo => [latitude, longitude],
:with => {:geodist => 0.0..200_000.0}
)
#users = User.where(:location_id => location_ids)
end
The ASC 0,20 is fine.
Change
:geo => [latitude, longitude],
:with => {:geodist => 0.0..200_000.0}
to
:geo => [latitude, longitude],
:with => {:geodist => 0.0..200_000.0},
:per_page => 1_000
Related
I need some help to create desire output with pure MYSQL. My question is By fire SQL below:
SELECT
r.questions_id
,r.Empty_Peg_Count AS 'Empty_Pegs'
FROM
Training_Core.results AS r
ORDER BY
CAST(r.Empty_Peg_Count AS SIGNED INTEGER) DESC;
I'm getting below output:
Array
(
[0] => Array
(
[questions_id] => 256332653
[Empty_Pegs] => 2
)
[1] => Array
(
[questions_id] => 256332653
[Empty_Pegs] => 2+
)
[2] => Array
(
[questions_id] => 256332653
[Empty_Pegs] => 1
)
)
I want value with + sign as high priority and it should show first. my desired output is as below:
Array
(
[0] => Array
(
[questions_id] => 256332653
[Empty_Pegs] => 2+
)
[1] => Array
(
[questions_id] => 256332653
[Empty_Pegs] => 2
)
[2] => Array
(
[questions_id] => 256332653
[Empty_Pegs] => 1
)
)
Can anyone help me how to get this result with SQL query?
Thanks an advance!
You can use a second order by rule:
ORDER BY
CAST(r.Empty_Peg_Count AS SIGNED INTEGER) DESC
,r.Empty_Peg_Count LIKE '%+%' DESC;
This will push the entries with a + to the top.
You can use RIGHT to check if the last character is a + and use that as a secondary ORDER BY field:
SELECT
r.questions_id
,r.Empty_Peg_Count AS 'Empty_Pegs'
FROM
Training_Core.results AS r
ORDER BY
CAST(r.Empty_Peg_Count AS SIGNED INTEGER) DESC
, RIGHT(r.Empty_Peg_Count, 1) = '+' DESC
You can apply sort using the following
SELECT * FROM `data` ORDER BY
CASE
WHEN right(`Empty_Pegs`,1) = '+' THEN (left(`Empty_Pegs`,1)*10)
ELSE `Empty_Pegs`
END DESC;
Basically I have a legacy query in mysql that I will be implement into ActiveRecord,
Legacy Way, as expected
SELECT
a.container_seal,
a.size ,
count(a.size) as bundles,
sum(a.piece) as pieces
FROM pipe a
WHERE a.outgoing_pipe_id IS NULL
GROUP by a.container_seal, a.size
ORDER by a.container DESC
AR Way
$modelPipe = Pipe::find()
->select([
"container_seal",
"size",
"count(size) as bundles",
"sum(piece) as pieces"
])
->where(['outgoing_pipe_id' => NULL])
->groupBy(['container_seal', 'size'])
->orderBy(['container' => SORT_DESC])
->all();
But why, when I debug it,
<pre>
<?php
print_r($modelPipe);
?>
</pre>
The count and sum is not into.
Array
(
[0] => app\models\hanwa\Pipe Object
(
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[container_seal] => TEMU6099067/ EMCCSQ6566
[size] => 15 x 35 x 0.85 x 6000
)
[_oldAttributes:yii\db\BaseActiveRecord:private] => Array
(
[container_seal] => TEMU6099067/ EMCCSQ6566
[size] => 15 x 35 x 0.85 x 6000
)
[_related:yii\db\BaseActiveRecord:private] => Array
(
)
[_errors:yii\base\Model:private] =>
[_validators:yii\base\Model:private] =>
[_scenario:yii\base\Model:private] => default
[_events:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] => Array
(
)
)
[1] => app\models\hanwa\Pipe Object
(
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[container_seal] => TEMU6099067/ EMCCSQ6566
[size] => 35 x 35 x 0.75 x 6000
)
Is it Yii2 have another way to use sum and count in case 'select' ?
You shoudl add the pubblic var for bundles and pieces in your pipe model
(for recive the result of query )
class Pipe extends \yii\db\ActiveRecord
{
public $bundles;
public $pieces;
......
I'm having problems when using find to sum the field num_days from table events.
$events = TableRegistry::get('Events');
$query = $events->find('all')
->select(['used' => 'sum(num_days)'])
->first();
Why $query->used is always 0?
print_r($events->find('all')->select(['used' => 'sum(num_days)'])->toArray()) gives,
Array ( [0] => App\Model\Entity\Event Object ( [used] => 6 [[new]] => [[accessible]] => Array ( [*] => 1 ) [[dirty]] => Array ( ) [[original]] => Array ( ) [[virtual]] => Array ( ) [[errors]] => Array ( ) [[invalid]] => Array ( ) [[repository]] => Events ) )
6 is exactly the correct answer for the query and print_r shows it but $query->used is returning always 0.
try $query->order(['used' => 'DESC']); before $query->used
Also, we can add used as protected $_virtual = ['used']; inside Event.php Entity File
I'm trying to get the total budgets of the last x days. I tried the following but the days without budgets are not being returned.
<?php
class Budget extends \Eloquent {
public static function getTotalBudgetsByDay($days = 31)
{
$budgetByDays = self::where('created_at', '>=', \Carbon\Carbon::now()->subDays($days))
->groupBy('date')
->orderBy('date', 'ASC')
->get([
\DB::raw('Date(created_at) as date'),
\DB::raw('COUNT(*) as total')
]);
return array_map(function ($row) {
return $row->getAttributes();
}, $budgetByDays->all());
}
}
$budgetsByDays = \Budget::getTotalBudgetsByDay();
Result:
Array (
[0] => Array
(
[date] => 2015-01-08
[total] => 2
)
[1] => Array
(
[date] => 2015-01-09
[total] => 1
)
[2] => Array
(
[date] => 2015-01-11
[total] => 7
)
)
in this case on 10 not appear in the array
At the moment "matrix_mct_versions" is a table with 73 entries. When I run this query the "version_count" always returns 73, ie the full number of rows. When I run the sub select query on its own i get the real count as per the com_ID param sent. I cannot see what I am doing wrong with this.. can anyone help?
SELECT
a_ID as com_ID,
option_number,
comment,
word_count,
gender,
sample,
(
SELECT
count(a_ID)
FROM
matrix_mct_versions
WHERE
com_ID = com_ID
) as version_count
FROM
matrix_mct
WHERE
attribute_number = :attribute_number AND
grade_number = :grade_number AND
attribute_type = :attribute_type
ORDER BY
option_number
Returns results like this:
[0] => Array
(
[com_ID] => 678
[option_number] => 1
[comment] => TODO primary function missing for controller y
[word_count] => 7
[gender] => 2
[sample] => 0
[version_count] => 73
)
[1] => Array
(
[com_ID] => 679
[option_number] => 2
[comment] => TODO make this green
[word_count] => 4
[gender] => 2
[sample] => 0
[version_count] => 73
)
[2] => Array
(
[com_ID] => 680
[option_number] => 3
[comment] => TODO make this better
[word_count] => 4
[gender] => 2
[sample] => 0
[version_count] => 73
)
At least one problem is your subquery. It is not correlated. I think you mean:
(SELECT count(a_ID)
FROM matrix_mct_versions
WHERE matrix_mct_versions.com_ID = matrix_mct.com_ID
) as version_count