Yii2: Select last 3 rows of each item - php

Given these tables/fields:
people:
id
socialID
name
peopleStats:
id
socialID
drVisitsAmount
receipAmounts
operationsAmount
Im trying to get last 3 records from peopleStats (ordered by id DESC) for a given array of socialIDs (3 records per each socialID):
$dummyData = PeopleStats::find()
->where(['socialID' => $socialIDs])
->groupBy(['socialID'])
->orderBy(['id' => SORT_DESC])
->limit(3)
->all();
but i just get 3 records and not 3 per each. How can i get this data?
Thank you in advance,

Related

how to find same id and group and count the same id on different days

currently i have a database structure like this :-
what i want to create (in laravel eloquent) is first get user_id and date_access. then based on the dates for example on the 2018-03-07 the are 3 rows, so user_id 1 have 2 times so = 2018-03-07 => 2
this is because it should group same user_id together to make it 1 and another user_id plus together..
how can i do this?
Can you try this:
$collections = DB::table('my_table')
->select(DB::raw('count(user_id) as users'))
->groupBy('date_access')
->get();

Query to join two table and sort the data array using PHP and MySQL

I need one help.I have two table i need to join two table and display them inside user's table.I am explaining my table structure below.
db_summary:
id member_id hit_type device_type
1 20 1 IOS
2 12 2 IOS
3 20 3 Android
4 14 4 Android
5 12 3 IOS
6 14 5 IOS
From the above table the hit_type column value is linked to the following table's type column.
db_hit_type:
id name type
1 Page 1
2 Map 2
3 Gallery 3
4 Phone 4
5 Web 5
From the db_summary table if hit_type=1 means its a page hit which is defined in second table. Here i need to fetch both table data by joining and sort them as per member id to display in following user's ui table.
sl no member page Hit Map Hit Gallery Hit Phone Hit WebHit TOTAL
i need to fill the above table by using the db data.suppose for member id 20 how many nos of page hit,Map hit...... and total nos of hit.I need to fetch all data and sort them as per user's table structure.Please help me.
Use below query to get both table data
SELECT * FROM db_summary AS ds, db_hit_type AS dht WHERE ds.hit_type = dht.type GROUP BY ds.member_id
SELECT dbs.*,dbht.name,dbht.id as dbHitId
FROM db_summary as dbs
LEFT JOIN db_hit_type as dbht
ON dbs.hit_type=dbht.type
ORDER BY dbs.member_id
I don't know how or when you want to view the resulting data, but at some point you will need to run a query counting each type of hit per user from the db_summary table, then updating the user table with the resulting data.
You could do something like this:
$stmt = $db->prepare("SELECT * FROM `db_summary` WHERE `member_id` = :member");
$stmt->bindParam(':member', $memberid);
$stmt->execute();
$hit = array(0,0,0,0,0);
while ($row = $stmt->fetch(PDO::ASSOC))
{
++$hit[$row['hit_type']-1];
}
echo 'Type 1 Hits: ', $hit[0];
echo 'Type 2 Hits: ', $hit[1];
echo 'Type 3 Hits: ', $hit[2];
echo 'Type 4 Hits: ', $hit[3];
echo 'Type 5 Hits: ', $hit[4];
If you need to update all members, you can wrap all of that into a loop that loops through each member id.

PHP MYSQL - Compare 2 comma separated lists

In my Database i have a field with a comma separated list of ids (itemids).
For an example i fill some rows with test data:
Row | itemids
1 => 2,5,8,11,22,45
2 => 4,6,9,13
3 => 1,3,5,16,23
4 => 6,12,18,21,24
Now i have a search string which contains all ids i want to serch for:
searchstring => 4,23,40
What i want to do now is to get all rows from the database which contains one or more values from my search string. From my example i should get the following rows:
Row | itemids
2 => 4,6,9,13
3 => 1,3,5,16,23
How do i do this? Can you give me an example SQL Query?
Thanks for help!
Query for searchstring => 4,23,40
select * from
item_table
where find_in_set(4,itemids)>0
UNION
select * from
item_table
where find_in_set(23,itemids)>0
UNION
select * from
item_table
where find_in_set(40,itemids)>0

mysql 2 tables updation/deletion based on condition

Hello friends I have 2 Mysql tables with 1:N relationship between category and category_Dates
Category:
ID Category Frequency
1 Cat A Half-yearly
2 Cat B Quarterly
category_Dates:
ID CatID Date
1 1 01-Jan-15
2 1 01-Jul-15
3 2 01-Jan-15
4 2 01-Apr-15
5 2 01-Jul-15
6 2 01-Oct-15
based on the category frequency I am entering number of records automatically in category_date. Eg
When category frequency = quarterly, I am entering 4 records in category_date with ID of that category. And dates will be entered later.
I am little confused if in case on wants to edit the frequency from halfyearly to yearly. How to change number of records. Please help with your valuable suggestions. I am using laravel 4 framework with mysql
best way would be with 3rd table joining Dates and Categories. See little carefully ,you can see its actually Many to Many relationship (N to N) as 1 category can have multiple dates. and one date may be part of multiple categories, like say 01-Jan-15 is part of Category 1 and 2 as well.
So use
category table
id Category Frequency
1 Cat A Half-yearly
2 Cat B Quarterly
date table
id Date
1 01-Jan-15
2 01-Apr-15
3 01-Jul-15
4 01-Oct-15
categories_dates table
ID CatID Date_id
1 1 1
2 1 3
3 2 1
4 2 2
5 2 3
6 2 4
If you change the frequency in Category table, retrieve the update category_id,
delete all from category_dates where CatId=category_id then insert the new entries in category_Dates.
Hope this help.
I assume your models are Category and CategoryDates.
let's update category id 1 from Half-yearlyto to Quarterly
$query = Category::find(1);
$query -> Frequency = 'Quarterly';
$query -> save();
return $query -> id;
in the CategoryDates model you would delete the catID = 1 and insert new data
$catID = 1;
$query = CategoryModel::where('CatId',$catId) -> delete();
$data = ['CatId' => $catID,'date' => 01-Jan-15, ....];
CategoryModel::create($data);
of course assuming that you would return the newly updated category id to your controller and call a funtionn to do the update in your CategoryModel.
Hope this help.

how can we add two different id by using group by

Sir
i have two table
first one name as screen_main having field
screen_main_id,screen_date,start_date,end_date
1 12 15
2 13 15
3 14 16
i have to fetch a screen_main_id between two start date and end date after that i have another table name screen_product table
fields:
screen_id screen_main_id article_id quantity price %age
1 1 1 5 234 12%
2 2 1 6 221 12%
3 1 2 5 111 10%
now first table provide me screen_main_id when i am selecting start date and end date after selecting screen_main_id i have to sum all the data having artical group wise like if artical=1 then it do the sum of both id which we got from first table.
my answer
for select first screen_main_id
select * from screen_main where screen_date between start_date='$start_date' and end_date='$end_date'
after fetching screen_main_id
select sum(quantity) ,sum(price),sum( %age ) from screen_product where screen_main_id='$screen_main_id' group by aitical_id
it add but if there is change in screen_main_id then it break in two parts it does not give us sum
please help me if there is any problem to understand my question in please ask me
Thanks
select sum(quantity) ,sum(price),sum( %age ) from screen_product where
screen_main_id='$screen_main_id' group by aitical_id
In Grouping , the fields used in GROUP BY clause must be in the selected fields so the second one becomes
select aitical_id,sum(quantity) ,sum(price),sum( %age ) from screen_product where screen_main_id='$screen_main_id' group by aitical_id
I hope that this would help you

Categories