FYI, I am using PHP and MySQL.
I am working on converting our spreadsheet staff rota to a more interactive online rota system.
I can get all the functions of the rota system fine, but when it comes to formatting it to make it clear of the departments and roles within each department, it is not so obvious.
I dont want to have to create a call to the database for each department and each role within the departments, but am stumped as to how to it to group the results and then output the results correctly and with the formatting I would like.
I do understand that this may be a very vague explanation of what I am trying to get to happen, and feel free to ask as many questions as you like.
I hope I make sense :-)
Have a great day
MySQL Data
ID | Name | Dept | Role
---|--------|------|------
1 | John | 1 | 2
2 | Steve | 1 | 2
3 | Colin | 1 | 3
4 | Trevor | 1 | 3
5 | Nigel | 2 | 4
6 | Gary | 2 | 5
Desire page/table format
STAFF | ..
-------|---
John | ..
Steve | ..
-------|---
Colin | ..
Trevor | ..
-------|---
TOT D1 | ..
-------|---
Nigel | ..
-------|---
Gary | ..
-------|---
TOT D2 | ..
If you want to get the values for each employee and the sum by department you can use the WITH ROLLUP modifier of the GROUP BY clause with adds extra rows with the totals by each value of the grouped columns.
Supposing that you want to show the salary:
select coalesce(name,concat('Total D',Dept)) as Name, sum(Salary) as Salary
from Staff
group by Dept, name with rollup
having coalesce(Dept,0)!=0
Results:
| Name | Salary |
|----------|--------|
| Colin | 80000 |
| John | 100000 |
| Steve | 120000 |
| Trevor | 40000 |
| Total D1 | 340000 |
| Gary | 60000 |
| Nigel | 50000 |
| Total D2 | 110000 |
The HAVING condition if for excluding the final row with the grand total, if you want that row you have to change a bit the COALESCE() to get the proper description:
select coalesce(name,concat('Total D',Dept),'Grand total') as Name, sum(Salary) as Salary
from Staff
group by Dept, name with rollup
Results:
| Name | Salary |
|-------------|--------|
| Colin | 80000 |
| John | 100000 |
| Steve | 120000 |
| Trevor | 40000 |
| Total D1 | 340000 |
| Gary | 60000 |
| Nigel | 50000 |
| Total D2 | 110000 |
| Grand total | 450000 |
Related
I've got a small CRM and I'm trying to figure out the best way to designing the DB tables.
I've currently got a single table for users that got around 30 columns which I alter from time to time. Since I am storing two different information on that table (user + company information) I was thinking of splitting that table into 3 (user + company + connection between these 2) but I am also interested in keeping a copy of any changes that are being made in these rows.
So going from:
user_id | firstname | last_name | company_name | company_city | company_subject | rank | status
1 | John | Borrows | Boink INC | NY | Web dev | 1 | 1
2 | Mike | Smith | Smithin INC | OC | Laywer | 1 | 2
3 | Mary | Anton | Caffin | SJ | Moving | 2 | 1
to something like this
user_id | firstname | last_name | rank | status
1 | John | Borrows | 1 | 1
2 | Mike | Smith | 1 | 2
3 | Mary | Anton | 2 | 1
comp_id | company_name | company_city | company_subject
1 | Boink INC | NY | Web dev
2 | Smithin INC | OC | Laywer
3 | Caffin | SJ | Moving
con_id | user_id | comp_id
1 | 1 | 1
2 | 2 | 2
3 | 3 | 3
But I'm not sure how to track the changes when for example a user changes the company name or some other info on user's table etc.
Just follow the normalization rules for structuring your database tables. You will find anything you need for that by just searching for database normalization.
Regarding your "update-history" you could add a Timestamp to your datasets and/or a separate boolean field "outdated" to be able to filter out the latest information.
Would be the simplest solution that comes into my mind.
A lot of thought process went before I decided to post this question. Trying to explain my problem in a simplified format.
I have 2 tables in my mySQL table, one of which is the users and the other one is the questions and answers related to that user.
Simplified Example:
Users
| id | name | registered_on |
|----|--------|---------------------|
| 1 | Aaron | 2017-02-01 00:01:02 |
| 2 | Baron | 2017-02-01 01:01:02 |
| 3 | Chiron | 2017-02-01 02:01:02 |
Answer Keys
| id | user_id | keyword | value | created_on |
|----|---------|---------|---------|---------------------|
| 1 | 1 | gender | Male | 2017-02-01 00:01:02 |
| 2 | 1 | age | 24 | 2017-02-01 00:01:02 |
| 3 | 2 | gender | Male | 2017-02-01 00:01:02 |
| 4 | 2 | age | Unknown | 2017-02-01 00:01:02 |
| 5 | 3 | gender | God | 2017-02-01 00:01:02 |
I hope the relation above is clear. So what I wish to achieve is to create a CSV report like this
| name | gender | age | registered_on |
|--------|--------|---------|---------------------|
| Aaron | Male | 24 | 2017-02-01 00:01:02 |
| Baron | Male | Unknown | 2017-02-01 01:01:02 |
| Chiron | God | NULL | 2017-02-01 02:01:02 |
As my research suggests, this can be done in the following ways :
Prepared Statements (Cannot use because CodeIgniter does not Support)
Paging (The vertical table is the problem)
MySQL Pivot Tables (But with Dynamic column names -- feels complicated!)
Any other better way that I do not know of
I am thinking about paging but am yet to figure out how it could be
used in the case of vertical tables. I would like it if any of you guys have faced the same problem or have some meaningful suggestions! Any help is appreciated!
If I understand well your problem, you want a CSV file with all user informations available using the answer keys as columns?
Why not doing this with 2 queries?
First, you can select all different keys in your answer table (select distinct keyword...)
Then, create your query with joins looping on your results :
$this->db->select('u.id, u.name, u.registered_on');
$this->db->from('users u');
foreach($keywords as $i_key => $s_keyword){
$this->db->join('answers_table at'.$i_key, 'at'.$i_key.'.user_id = u.id', 'left');
$this->db->select('at'.$i_key.'.value as '.$s_keyword);
}
$a_users = $this->db->get()->result();
I'm currently working on a simple employee scheduling tool and have a problem with a SQL query. To explain my problem let use this two very abstract tables.
The first table simply consists of the employees
employees
======================
empId | name | ...
----------------------
10 | Scott |
11 | Schrute |
12 | Halpert |
13 | Howard |
In the second table you find the assigned tasks to each employee by day.
tasks
==============================================
tasId | name | task | date | ...
----------------------------------------------
10 | Scott | Support | 2014-02-17 |
11 | Scott | Bugfix | 2014-02-18 |
12 | Halpert | Bugfix | 2014-02-17 |
13 | Halpert | Develop | 2014-02-18 |
14 | Howard | Support | 2014-02-17 |
Now I want to know what the employees are working on on Feb 17th or if they have no tasks planned for that day. I use the following SQL query to do that.
SELECT e.name, t.task
FROM employees e LEFT JOIN tasks t ON e.name = t.name
WHERE date IS NULL OR date = DATE('2014-02-17')
The result delivers exactly what I need:
name | task
--------------------
Scott | Support
Schrute | NULL
Halpert | Bugfix
Howard | Support
And now to my problem. If I want to see the tasks of Feb 18th I get this result set:
name | task
--------------------
Scott | Bugfix
Schrute | NULL
Halpert | Develop
The reason to this is obvious, the date of Howard's tasks are neither NULL nor do they equal 2014-02-18.. What would be the best way to get the desired result?
I use MySQL and PHP.
(Sorry for the stupid title, I couldn't think of anything better..)
Move your filter to the join predicate:
SELECT e.name, t.task
FROM employees e
LEFT JOIN tasks t ON e.name = t.name AND t.date = DATE('2014-02-18');
Your query as it is will only return people who have no tasks at all, or have tasks on the date specified. It will omit people who have tasks, but not on the date specified. Consider the results of joining your sample data with no where clause:
empId | name | task | date | ...
----------------------------------------|
10 | Scott | Support | 2014-02-17 |
10 | Scott | Bugfix | 2014-02-18 |
11 | Schrute | NULL | NULL |
12 | Halpert | Bugfix | 2014-02-17 |
12 | Halpert | Develop | 2014-02-18 |
13 | Howard | Support | 2014-02-17 |
As you can see there is no record for Howard where Date = 2014-02-18, or where Date is null, this is why no record is returned for Howard. When you add the filter to the join predicate your results become:
empId | name | task | date | ...
----------------------------------------|
10 | Scott | Bugfix | 2014-02-18 |
11 | Schrute | NULL | NULL |
12 | Halpert | Develop | 2014-02-18 |
13 | Howard | NULL | NULL |
Which I think is the desired results.
You can use
UNIX_TIMESTAMP(`date`) = 0
Looks like you have some data which are stored as '0000-00-00' and its not null so adding the extra OR condition as above will return those data as well.
I am currently building a car booking system using PDO PHP and MySQL but am struggling with how to structure my tables for any car extras the user may have added to their booking.
I currently have the following tables at the moment which use the reservation_table to link the booking_id with the car_id (taken from their separate tables):
car_table:
ID | car_make | car_name
1 | Ford | Focus
2 | BMW | Z3
3 | Audi | A5
booking_table:
booking_ID | booking_time | total_cost | etc..
125674 | 2013-02-02 | 91.55
887463 | 2013-01-19 | 52.00
209930 | 2013-01-11 | 23.99
reservation_table:
ID | booking_ID | car_ID
1 | 125674 | 2
2 | 887463 | 2
3 | 209930 | 1
extras_table:
ID | car_extra | extra_price
1 | GPS | 22.99
2 | Baby Seat | 12.99
3 | Car Charger | 15.99
Originally I was going to add an extra column into the reservation_table using an array of the selected car extras to link them with the booking as so:
booking_table:
ID | booking_ID | car_ID | car_extras
1 | 125674 | 2 | [2,3]
2 | 887463 | 2 | [1]
3 | 209930 | 1 | [1,3]
but I have read that this is bad practice. How can I structure my tables so that I can assign the selected car extra ID's (which can range from the user selecting 0 to 12) to a particular booking?
Would the following work by creating a new table that linked just the booking_ID with the extra_ID (this would mean multiple rows of the same booking id though):
selected_extras_table:
ID | booking_ID | car_extra_ID
1 | 125674 | 2
2 | 125674 | 3
3 | 887463 | 1
4 | 209930 | 1
5 | 209930 | 3
You are exactly right: creating the selected_extras_table with a row per extra is the best solution. When you query that table, you'll get a list of all the extras in whichever booking_ID you're interested in, and you won't need to parse out multiple values from a single field (e.g., "[2,3]"). Let the database keep the values separate. You're on the right track.
Hopefully this is a quick and easy question to answer.
I have a MySQL Database with the following fields:
shoe_size and waist_size
The shoe_size entries range anywhere from 3 to 21 (incrementing by halves-- 3, 3.5, 4, 4.5, etc).
The same is true for the waist_size entries.
My question is this:
Using PHP, how can I best consolidate this into a table that counts the total number of sizes, so that it could essentially be used as an Order Form?
For example, the MySQL table looks like this:
----------------------------------------------
| Name | shoe_size | waist_size |
----------------------------------------------
| John | 9.5 | 33 |
----------------------------------------------
| Steve | 9 | 32 |
----------------------------------------------
| Tom | 9.5 | 33 |
----------------------------------------------
| Sally | 7 | 8 |
----------------------------------------------
| Jane | 7 | 8 |
----------------------------------------------
And the output HTML (table?) would look something like this:
ORDER FORM
------------------------------------------
| Shoe Size | 7 | 9 | 9.5 |
------------------------------------------
| Total Pairs | 2 | 1 | 2 | <----- This is calculated from above
------------------------------------------
------------------------------------------
| Waist Size | 8 | 32 | 33 |
------------------------------------------
| Total Pairs | 2 | 1 | 2 | <----- This is calculated from above
------------------------------------------
I'm not even sure if my format for the "Order Form" table is the best way to go on this. Any nudge in the right direction would be greatly appreciated.
MySQL might be enough for this task, try something like
SELECT shoe_size, COUNT(id) as count FROM table GROUP BY shoe_size ORDER BY shoe_size asc
I think what you are looking for is GROUP BY.
(http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html)
e.g:
SELECT `Shoe Size`, COUNT(*) FROM table_name GROUP BY `Shoe Size`