I'm trying to return some data from a query in this format (or at least very similar)
Array
(
[0] => Array
(
[id] => 1
[name] => Gym Opening Times
[times] => Array
(
[0] => Array
(
[id] => 1
[label] => Mon-Thu
[time] => 6:00am : 10:00pm
)
[1] => Array
(
[id] => 2
[label] => Fri
[time] => 6:00am : 09:00pm
)
[2] => Array
(
[id] => 3
[label] => Sat & Sun
[time] => 6:00am : 10:00pm
)
)
)
[1] => Array
(
[id] => 1
[name] => Resteruant Times
[times] => Array
(
[0] => Array
(
[id] => 1
[label] => Mon-Thu
[time] => 6:00am : 10:00pm
)
[1] => Array
(
[id] => 2
[label] => Fri
[time] => 6:00am : 09:00pm
)
[2] => Array
(
[id] => 3
[label] => Sat & Sun
[time] => 6:00am : 10:00pm
)
)
)
)
I have this query so far, however, it throws an error Error executing PDO query: SQLSTATE[21000]: Cardinality violation: 1242 Subquery returns more than 1 row
SELECT
otg.groupName,
otg.id,
(SELECT
ot.id,
ot.label,
ot.time
FROM
opening_times ot
WHERE
ot.groupId = otg.id) as times
FROM
opening_time_groups as otg
WHERE
otg.venueId = $venueId
AND otg.active = 1
What have I gotten wrong? Or is this something I simply can't do in 1 query?
You sub query is returning more than one row. A sub query in the SELECT must only return a single row.
However it would seem that you could do this simple with a normal join, then sort the array out in the script that calls it:-
SELECT
otg.groupName,
otg.id,
ot.id,
ot.label,
ot.time
FROM opening_time_groups as otg
LEFT OUTER JOIN opening_times ot
ON ot.groupId = otg.id) as times
WHERE otg.venueId = $venueId
AND otg.active = 1
Related
I am trying to retrieve distinct value using this query from my salary table
SELECT DISTINCT gross,sl_month,sl_year
FROM wp_attn_emp_salary where `emp_id` = 22 order by gross DESC
But it returns duplicate value too. please, check the image.
Query Result
My expecting result is like this
Array
(
[0] => stdClass Object
(
[gross] => 9595
[sl_month] => 2
[sl_year] => 2021
)
[1] => stdClass Object
(
[gross] => 9226
[sl_month] => 1
[sl_year] => 2020
)
[2] => stdClass Object
(
[gross] => 8875
[sl_month] => 2
[sl_year] => 2019
)
[3] => stdClass Object
(
[gross] => 8420
[sl_month] => 1
[sl_year] => 2019
)
[4] => stdClass Object
(
[gross] => 8405
[sl_month] => 12
[sl_year] => 2018
)
[5] => stdClass Object
(
[gross] => 6464
[sl_month] => 1
[sl_year] => 2018
)
)
I need the sl_month,sl_year in descending order too.
You misunderstand how DISTINCT works in SQL. It returns a row if the whole row (i.e. all columns) is distinct from any other row in the result. These two rows are distinct from each other:
gross
sl_month
sl_year
9595
1
2021
9595
2
2021
From the desired result you described, it looks like you want a greatest-n-per-group query.
SELECT gross, sl_month, sl_year
FROM (
SELECT gross, sl_month, sl_year,
ROW_NUMBER() OVER (PARTITION BY gross ORDER BY sl_year, sl_month) AS rownum
FROM wp_attn_emp_salary
WHERE emp_id = 22
) AS t
WHERE t.rownum = 1
ORDER BY sl_month DESC, sl_year DESC;
Note: Window functions require MYSQL 8.0, which was released in 2018 so you should be using it by now. If you need a solution that runs on an earlier version of MySQL, search past answers with the greatest-n-per-group tag.
I have a number like this
34.59704151614417
and I put datatype on decimal(16,14) but it has saved as 34.59704151614400
it means mysql save just 12 digit decimal
this is my array
Array
(
[geometry] => Array
(
[type] => Polygon
[coordinates] => Array
(
[0] => Array
(
[0] => Array
(
[0] => 32.34375
[1] => 48.690960390925
)
[1] => Array
(
[0] => 16.875
[1] => 34.597041516144
)
[2] => Array
(
[0] => 43.2421875
[1] => 31.653381399664
)
[3] => Array
(
[0] => 64.3359375
[1] => 40.97989806962
)
[4] => Array
(
[0] => 32.34375
[1] => 48.690960390925
)
)
)
)
)
I understand
after I do json_decode of my array it reduce it's digit to 12 number
Set precision directive:
ini_set('precision', 16);
It is currently 14 and that's why the decimals get reduced. Check this link about more information
I need to combine those two array into a single array.
1st query result
Array ( [0] => stdClass Object ( [p_type] => Bank [tsales] => 131000 [tdues] => 55000 [tpaid] => 75000 ) [1] => stdClass Object ( [p_type] => Cash [tsales] => 104000 [tdues] => 50000 [tpaid] => 54000 ) [2] => stdClass Object ( [p_type] => bKash [tsales] => 25000 [tdues] => 10000 [tpaid] => 5000 ) [3] => stdClass Object ( [p_type] => Pause [tsales] => 4000 [tdues] => 1000 [tpaid] => 2000 ) )
2nd query result
Array ( [0] => stdClass Object ( [dpayment] => 1000 ) [1] => stdClass Object ( [dpayment] => 5000 ) [2] => stdClass Object ( [dpayment] => 1000 ) [3] => stdClass Object ( [dpayment] => 5000 ) )
try to use array_merge. That should be very easy.
Your query result shows that you using $query->result() to get the value,
Instead of using result(), use $query->row_array();
then you get single dimensional array for both queries
then use array_merge() that would be easy
I have two tables. There is a table place that contains data as shown below:
id name
-----------
1 A1
2 A2
3 A3
4 B1
Other table place_conditions contains data as sown below:
id placeID desription
----------------------------
1 1 Windy
2 1 Dry
3 3 Wet
4 3 Rainy
4 4 Dry
I like to made a query in laravel 4 that make result like this below:
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => A1
[place_cond] => Array
(
[0] => stdClass Object
(
[description] => Windy
)
[1] => stdClass Object
(
[description] => Dry
)
)
)
[1] => stdClass Object
(
[id] => 2
[name] => A2
[place_cond] => Array
(
)
)
[3] => stdClass Object
(
[id] => 3
[name] => A3
[place_cond] => Array
(
[0] => stdClass Object
(
[description] => Wet
)
[1] => stdClass Object
(
[description] => Rainy
)
)
)
)
Here part of my query (it still error) :
$query = DB::table('place');
if(Input::has('search')){
$query->where(function($a){
$a->where('name','LIKE','%'.Input::get('search').'%');
});
}
/* $query->select('place.*',DB::raw('SELECT Description as place_cond FROM place_conditions WHERE place_conditions.placeID = place.id')); // sub query . Arrggghhhhhh... whyyyy.. stillllll.. errorrrrrrr.. */
$data = $query->get();
Is possible to produce results like the above in laravel 4? Can you help me? Thank you
When I perform the following:
$popular_posts = $this->Blog->find('all', array('limit' => 5));
I get the following:
Array
(
[0] => Array
(
[Blog] => Array
(
[id] => 4fcfb37d-3eb0-4ec2-a744-175c987a2b72
[title] => This is a post example2
[short_description] => You've stumbled across our blog! Welcome! Here
[created] => 2012-06-06 21:46:05
[modified] => 2012-06-07 16:01:24
)
[Reply] => Array
(
[0] => Array
(
[id] => 4fcfb305-0c58-421b-9149-175c987a2b72
)
[1] => Array
(
[id] => 4fd0ae9e-dca0-4afe-862c-1258987a2b72
)
)
),
[1] ...
[2] ...
)
How can I order the results by the number of Reply ??? (desc)?
Try this
$data = $this->Blog->find('all',array('group' =>array('Reply.id'),
'order' => array('COUNT(Reply.id) DESC'),
'limit'=> 5));