MySQL Specific condition for each row in same query +PHP - php

Please consider my tables
store - table with information of all my stores
zone - An alias table with zone_id and the name of the zone.
store_zone - Table that contains the information when a store was changed to a different zone.
cashdesk - table with local_id that contains information about the
income for each day and each store
store_zone table:
+----+----------+---------+-------------+
| id | store_id | zone_id | modified_at |
+----+----------+---------+-------------+
| 1 | 1 | 2 | 2019-01-01 |
| 2 | 1 | 1 | 2019-01-04 |
| 3 | 2 | 4 | 2019-01-03 |
+----+----------+---------+-------------+
store_id is the foreign key for my local table id
store table also contains a fk for zone_id
zone_id is the foreign key for my zone table id
Now consider my problem:
I need to select all stores
get the sum(income) per row (stores)
filter by zone
consider only incomes that correspond to the local_zone table between
dates. (if I select the whole month and a zone was changed the day
15.. I only care about the 15 first days). I can use php to help build it.
something like:
$zone_modified_dates = //result of
"SELECT modified_at
FROM store_zone
WHERE zone_id = 1 AND store_id = 1"
$zone_modified_dates = '2019-01-04'
SELECT sum(income) from cashdesk c
inner join store s ON s.id = c.store_id
inner join store_zone sz ON (sz.store_id = s.id AND sz.zone_id = z.id
WHERE zone_id = 1
AND cashdesk created_at >= '2019-01-01'
AND cashdesk created_at >= '2019-01-10'
AND //IF ZONE WAS CHANGED IN THIS CREATED_AT BETWEEN DATES ONLY CONSIDER THE DAYS THAT THE SPECIFIC ROW STORE WAS CONSIDERED ZONE 1
GROUP BY s.id
Thanks in advance.
#edited for better explanation of the problem.

Related

Joining a calendar table to a sales table to get total sales for every day in a specified range

I have a 'sales' table called phpbb_sold which records each 'sale' as a row.
I am able to use a WHERE clause with the uitemid field to select one particular item in the sales records, as seen below:
SELECT uitemid, locktime, migrated_sold FROM phpbb_sold WHERE uitemid=342;
+---------+------------+---------------+
| uitemid | locktime | migrated_sold |
+---------+------------+---------------+
| 342 | 1632523854 | 1 |
| 342 | 1634239244 | 1 |
| 342 | 1634240072 | 1 |
| 342 | 1636367271 | 1 |
+---------+------------+---------------+
uitemid = number that identifies this as a sale of X item. locktime = UNIX timestamp that shows the datetime that the item was sold. migrated_sold = the quantity of the item sold. So this is nice, I have a table that keeps a record of each sale as it happens.
What I want to achieve though, is a record of the total number of sales of this item type, for each day in a 6 month period spanning back from the current date, and including each day regardless of whether a sale was made or not. So the desired output of my query would be:
SELECT (the query I want goes here) and returns the following rows...;
+------------+------------+
| caldate | sold_total |
+------------+------------+
| 2021-09-23 | 2 |
| 2021-09-24 | 0 |
| 2021-09-25 | 1 |
| 2021-09-26 | 0 |
| 2021-09-27 | 0 |
| 2021-09-28 | 1 |
+------------+------------+
Note that each day is included as a row in the results, even where the sales total for that day is 0. I read that to do this, I would be required to create a calendar table with one column and all the days I want as rows, so I went ahead and did that:
SELECT caldate FROM phpbb_calendar;
+------------+
| caldate |
+------------+
| 2021-09-23 |
| 2021-09-24 |
| 2021-09-25 |
| 2021-09-26 |
| 2021-09-27 |
| 2021-09-28 |
+------------+
Now all that remains is for me to make the query. I need to somehow return all the rows from the phpbb_calendar table, joining the data from sum() (?) of the total migrated_sold for those days where exists, and a 0 where no sales took place.
I anticipated some issues with the UNIX timestamp, but it's okay because I am able to get caldate and locktime fields to be the same format by using from_unixtime(locktime, '%Y-%m-%d'), so both dates will be in the YYYY-MM-DD format for comparison.
Please could someone help me with this. I've gotten so close every time but it seems that everyone else's request is only slightly different from mine, so existing questions and answers have not been able to satisfy my requirements.
End goal is to use a JS chart library (AnyChart) to show a line graph of the number of sales of the item over time. But to get there, I first need to provide it with the query necessary for it to display that data.
Thanks
Update
Using this query:
SELECT c.caldate, u.uitemid, sum(v.migrated_sold) as total_sales
from phpbb_calendar c cross join
(select distinct uitemid from phpbb_sold) u left join
phpbb_sold v
on c.caldate = from_unixtime(v.locktime, '%Y-%m-%d') WHERE u.uitemid = 39 and c.caldate <= curdate() GROUP BY c.caldate ORDER BY c.caldate;
Returns:
But as you can see, it's just tallying up the total number of sales ever made or something - its clearly incrementing in a way I don't understand.
I don't want it to do that - I want it to count the number of total sales on each day individually. The results should look like this:
So that what is returned is basically a 'histogram' of sales, if any occurred, including 'empty' days where there were no sales (so these empty days must still be returned as rows).
SELECT c.caldate, u.uitemid, COALESCE(SUM(v.migrated_sold), 0) AS total_sales
FROM phpbb_calendar c
CROSS JOIN (SELECT DISTINCT uitemid FROM phpbb_sold WHERE uitemid = 37) u
LEFT JOIN phpbb_sold v
ON v.locktime BETWEEN UNIX_TIMESTAMP(TIMESTAMP(c.caldate)) AND UNIX_TIMESTAMP(TIMESTAMP(c.caldate, '23:59:59'))
AND u.uitemid = v.uitemid
WHERE c.caldate BETWEEN CURDATE() - INTERVAL 6 MONTH AND CURDATE()
GROUP BY c.caldate, u.uitemid
ORDER BY c.caldate;
N.B. I have changed your join to use the unix_timestamp as it should be more efficient and it can use any existing index on locktime
check this out:
select id, d, sum(s) from (
select U.id, d, 0 s from (
select adddate(current_date(),-rows.r) d from (
select (#row_number := #row_number + 1) r
from information_schema.columns,
(SELECT #row_number := 0) AS x
limit 200
) rows
) dates,
(SELECT distinct uitemid id FROM `phpbb_sold`) U
where d > adddate(current_date(), interval -6 month)
union
select uitemid, date(from_unixtime(locktime)),sum(migrated_sold)
from `phpbb_sold`
group by uitemid, date(from_unixtime(locktime))
) sales_union
group by id, d
order by id, d;
see dbfiddle
no need for calendar table

Facing difficulties to write a sql query in laravel

We have three tables users,answers and stretches. When someone login to our system we stored user_id,created_at and updated_at in stretches table and when he/she logout updated deleted at in the same row.
Session time 30 minutes.
users table
id
f_name
l_name
email
answers table
id
user_id
body
created_at
updated_at
stretches table
id
user_id
created_at
updated_at
deleted_at
Now i want to get only those rows from the stretches table where an answer has been given. I wrote this query but it takes to much time but did not return accurate data.
$query = "select count(a.id), a.user_id
from answers a
where a.created_at in
(select s.created_at
from stretches s
where s.created_at >= a.created_at
) group by a.user_id";
$results = DB::select(DB::raw($query));
I'd be curious to hear how some of you write this query.
Answers table
id | body | user_id | created_at | updated_at
10 | text | 10 | 2015-10-10 0:0:0 | 2015-10-10 0:0:0
Stretches Table
id | user_id | created_at | updated_at | deleted_at
1 | 10 | 2015-10-00 0:0:0 | 2015-10-10 0:0:0 | 2015-10-20 0:0:0
Now i want to get those rows from stretches table where an answer has been given.
Query results should be:
id | user_id | created_at | updated_at | deleted_at
1 | 10 | 2015-10-10 0:0:0 |2015-10-10 0:0:0 | 2015-10-10 0:0:0
If I correctly understood you. SQL query below will return only those records from table Stretches where answer was given to user. Use operator JOIN, it will do filter work for you.
SELECT S.user_id, COUNT(*) as answers_count FROM Stretches AS S
JOIN Answers AS A ON S.user_id = A.user_id
GROUP BY S.user_id
This should do the job
SELECT DISTINCT Stretches.* FROM Stretches
JOIN Answers ON Stretches.user_id = Answers.user_id
GROUP BY Stretches.user_id

php mysql Get the latest records based on multiple conditions and count them

I have one table - staff
id | staff Name | adress
-------------------------
1 | Mr.A | Any Address
2 | Mr. B | Any Address
2nd Table - employment_history
eid | staff_id | school_id | type | grade | date_of_appointmet
--------------------------------------------------------------------
1 | 1 | 1 |Promotion | 17 | 2012-12-12
2 | 1 | 2 |promotion | 18 | 2013-2-2
3 | 2 | 2 |appointment | 17 | 2013-3-3
and so on tables moves
Now the Question is that
i want to get the latest job of the person with his details from the staff table
how can i count how many of 17 grade staff works in school_id 1
(remembering that staff_id 1 (mr.a) now have been promoted to 18 and now works in school_id 2.)
select staff_id, max(date_of_appointment) as date_of_appointment
from employment_history
group by staff_id
This query will return the most recent staff record for each staff_id. Turn it into a subquery and join onto the employment history table
Select grade, count(1)
from
(select staff_id, max(date_of_appointment) as date_of_appointment
from employment_history
group by staff_id) a
inner join employment_history e on e.staff_id = a.staff_id and a.date_of_appointment = e.date_of_appointment
group by grade
This solution makes the assumption that staff_id + date_of_appointment is a unique key...if you have multiple rows where one staff_id has multiple employment history entries for one date, this won't work. You need some logic to make the 'most recent employment history entry' return a unique combination of data...if staff_id + max(date_of_appointment) is not unique, you'll need to come up with logic in the 'a' subquery that returns unique data.
What about something like
SELECT *
FROM employment_history eh1
WHERE eh1.date_of_employment = (
SELECT max(eh2.date_of_employment)
FROM staff s
JOIN employment_history eh2 ON s.id = eh2.staff_id
WHERE s.id = ?
)
Recplacing the ?, or using bind_param() as appropriate.

summing column in SQL from two tables

I'm using MySQL and trying to sum the number of hours that user has participated in events, but only for a specific organization.
EDIT: I have another table thrown into the mix (misEvents). I think the GROUP BY statement is causing some problems, but I'm not quite sure what to do about it.
table events: contains the "event" information. orgID is the ID of the organization hosting it
-each listing is a separate event
ID | orgID | hours
1 | 1 | 1
2 | 1 | 2
3 | 2 | 1
table eventUserInfo: shows which users attended the events
-refID references the ID column from the events table
ID | refID | userID
1 | 1 | 1
2 | 1 | 3
3 | 2 | 2
4 | 2 | 1
5 | 3 | 2
table miscEvents
-these are entered in manually if an event by an organization wasn't posted on the site, but
the users still wants to keep track of the hours
ID | userID | orgID | Hours
1 | 1 | 1 | 2
So, when I view the member activity for organization 1, the following table should display
userid | total hours
1 | 5 //participated in ID #1 and 2 and a misc event, total of 4 hours
2 | 2 //participated in ID #2 only for org 1
3 | 1 //participated only in ID #1
assume the given input is $orgID which is set to 1 in this case
SELECT eventUserInfo.userID, sum(events.hours) as hours,sum(miscEvents.hours) AS mhours FROM events
JOIN eventUserInfo ON events.ID = eventUserInfo.refID
JOIN miscEvents ON events.orgID = miscEvents.orgID
WHERE events.orgID = '$orgID'
GROUP BY eventUserInfo.userID
I think it should be:
SELECT eventInfo.userID --- there is no "events.userID" column
, SUM(events.hours) AS hours --- alias added
FROM events
JOIN eventInfo --- no need for LEFT JOIN
ON events.ID = eventInfo.refID
WHERE events.orgID = '$orgID'
GROUP BY eventInfo.userID
The problem lies probably in that you are trying to print the "hours" with: $event['hours'] but there is no hours column returned (only userID and SUM(event.hours). Use an alias in the SELECT list, as above.
Or since eventINFO seems to be you primary table in the query:
SELECT eventINFO.userID, SUM(events.hours)
FROM eventINFO
JOIN events ON eventINFO.refID = events.ID
WHERE events.orgID = '$orgID'
GROUP BY eventINFO.userID
Should result in the same as ypercube but to me seems a little more clear calling your primary table in the FROM call
To your new problem, you need to get rid of this:
sum(events.hours + miscEvents.hours)
If I'm not mistaken, you can't have multiple columns in a SUM you can only add a single column per call.
I would try something like:
SUM(events.hours) AS hours, SUM(miscEvents.hours) AS mhours
Then, in your function, add together those values $total = hours + mhours

JOIN 2 Tables with different columns

I Have 2 Tables, One For New Pictures and One For New Users, i want to create like a wall that mixes the latest actions so it'll show new users & pictures ordered by date.
What i want is a single query and how to know inside the loop that the current entry is a photo or user.
TABLE: users
Columns: id,username,fullname,country,date
TABLE: photos
Columns: id,picurl,author,date
Desired Output:
Daniel from California Has just registred 5mins ago
New Picture By David ( click to view ) 15mins ago
And so on...
I'm begging you to not just give me the query syntax, i'm not pro and can't figure out how to deal with that inside the loop ( i only know how to fetch regular sql queries )
Thanks
You could use an union:
SELECT concat(username, " from ", country, " has just registered") txt, date FROM users
UNION
SELECT concat("New picture By ", username, " (click to view)") txt, date FROM photos INNER JOIN users ON author=users.id
ORDER BY date DESC
LIMIT 10
This assumes that author column in photos corresponds to the users table id. If author actually is a string containing the user name (which is a bad design), you'll have to do this instead:
SELECT concat(username, " from ", country, " has just registered") txt, date FROM users
UNION
SELECT concat("New picture By ", author, " (click to view)") txt, date FROM photos
ORDER BY date DESC
LIMIT 10
Make sure you have an index on date in both tables, or this will be very inefficient.
I've put together this little example for you to look at - you might find it helpful.
Full script can be found here : http://pastie.org/1279954
So it starts with 3 simple tables countries, users and user_photos.
Tables
Note: i've only included the minimum number of columns for this demo to work !
drop table if exists countries;
create table countries
(
country_id tinyint unsigned not null auto_increment primary key,
iso_code varchar(3) unique not null,
name varchar(255) unique not null
)
engine=innodb;
drop table if exists users;
create table users
(
user_id int unsigned not null auto_increment primary key,
country_id tinyint unsigned not null,
username varbinary(32) unique not null
-- all other detail omitted
)
engine=innodb;
drop table if exists user_photos;
create table user_photos
(
photo_id int unsigned not null auto_increment primary key,
user_id int unsigned not null,
-- all other detail omitted
key (user_id)
)
engine=innodb;
The important thing to note is that the primary keys of users and photos are unsigned integers and auto_increment (1,2,3..n) so I can find the latest 10 users and 10 photos by ordering by their primary keys (PK) descending and add a limit clause to restrict the number of rows returned.
-- change limit to increase rows returned
select * from users order by user_id desc limit 2;
select * from user_photos order by photo_id desc limit 2;
Test Data
insert into countries (iso_code, name) values ('GB','Great Britain'),('US','United States'),('DE','Germany');
insert into users (username, country_id) values ('f00',1),('bar',2),('stack',1),('overflow',3);
insert into user_photos (user_id) values (1),(1),(2),(3),(1),(4),(2),(1),(4),(2),(1);
So now we need a convenient way (single call) of selecting the latest 10 users and photos. The two tables are completely different so a union isnt going to be the best approach so what we'll do instead is write a stored procedure that returns two resultsets and handle generating the wall (merge resultsets) in our php script.
Stored procedure
Just a wrapper around some SQL code - think of it like SQL's version of a function call
drop procedure if exists list_latest_users_and_photos;
delimiter #
create procedure list_latest_users_and_photos()
begin
-- last 10 users
select
'U' as type_id, -- integer might be better
u.user_id,
u.country_id,
u.username,
-- other user columns...
c.name as country_name
from
users u
inner join countries c on u.country_id = c.country_id
order by
u.user_id desc limit 10;
-- last 10 photos
select
'P' as type_id,
up.photo_id,
up.user_id,
-- other photo columns...
u.username
-- other user columns...
from
user_photos up
inner join users u on up.user_id = u.user_id
order by
up.photo_id desc limit 10;
end #
delimiter ;
Testing
To test our stored procedure all we need to do is call it and look at the results.
mysql> call list_latest_users_and_photos();
+---------+---------+------------+----------+---------------+
| type_id | user_id | country_id | username | country_name |
+---------+---------+------------+----------+---------------+
| U | 4 | 3 | overflow | Germany |
| U | 3 | 1 | stack | Great Britain |
| U | 2 | 2 | bar | United States |
| U | 1 | 1 | f00 | Great Britain |
+---------+---------+------------+----------+---------------+
4 rows in set (0.00 sec)
+---------+----------+---------+----------+
| type_id | photo_id | user_id | username |
+---------+----------+---------+----------+
| P | 11 | 1 | f00 |
| P | 10 | 2 | bar |
| P | 9 | 4 | overflow |
| P | 8 | 1 | f00 |
| P | 7 | 2 | bar |
| P | 6 | 4 | overflow |
| P | 5 | 1 | f00 |
| P | 4 | 3 | stack |
| P | 3 | 2 | bar |
| P | 2 | 1 | f00 |
+---------+----------+---------+----------+
10 rows in set (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
Now we know that works we can call it from php and generate the wall.
PHP Script
<?php
$conn = new Mysqli("localhost", "foo_dbo", "pass", "foo_db");
$result = $conn->query("call list_latest_users_and_photos()");
$users = array();
while($row = $result->fetch_assoc()) $users[] = $row;
$conn->next_result();
$result = $conn->use_result();
$photos = array();
while($row = $result->fetch_assoc()) $photos[] = $row;
$result->close();
$conn->close();
$wall = array_merge($users, $photos);
echo "<pre>", print_r($wall), "</pre>";
?>
Hope you find some of this helpful :)

Categories