Hi I’m trying to make a small CRM where I storing some master data for Clients and I have made a CRUD that saves to a MySQL table called clients – so far so good.
Now I would like to register worked time within each Client, so I need arrays within arrays, but as I can see that is not possible with MySQL tables.
My idea is that when I have finished working on a task, then I (frontend) select a the Client, and then I select “Register time” within that client then I select the Year I have done the task, and then I select the Month I have done the task and then I add a new line with in that table (Time sheet) where I set the date, the name of the task and the time used or the fixed priced and save.
I have the table Clients which contains the basic master data:
Clients (simplified) – where I want to add the new column with register_time.
+-----------+-------------+---------------+---------------+---------------+
| client_id | client_name | clients_phone | clients_email | register_time |
+-----------+-------------+---------------+---------------+---------------+
| 1 | Ford | 111-222-333 | info#ford.com | [years] |
| 2 | BMW | 444-555-666 | info#bmw.com | [years] |
| 3 | KIA | 777-888-999 | info#kia.com | [years] |
+-----------+-------------+---------------+---------------+---------------+
Years
+---------+
| year |
+---------+
| 2017 |
| 2018 |
| 2019 |
| 2020 |
+---------+
Months (for 2020)
+-------------+
| month |
+-------------+
| january |
| february |
| march |
| april |
| may |
| june |
| july |
| august |
| september |
| october |
| november |
| december |
+-------------+
Time sheet (for January)
+------------+-------------+---------------+---------------+
| date | task | time_worked | fixed_price |
+------------+-------------+---------------+---------------+
| 01-01-2020 | Webdesign | 2 | - |
| 02-01-2020 | Logo | - | 500 |
+------------+-------------+---------------+---------------+
I hardly know any MySQL (or backend in general) and I made the node CRUD connection with a tutorial - so I’m really having trouble getting my head around how to join the table and use the client_id as the main key/identifier. And also how to make it so that I can have a CRUD insdie my current CRUD so that I can create new years and new lines in the time sheets.
Maybe I’m going about it all wrong – I hope you can help.
Let me know if you need more info from me.
// Rue
Related
I have this problem - where i have (time Schedule) saved in mysql table
the table is for example like this :-
+----+--+------+--+--+--------------+--+--------------+
| day | | Group A | | | Group B | | Group C
+----+--+------+--+--+--------------+--+--------------+
| sat| | physics | | | Language | | Algebra
| sun| | Chemistry| | | Math | | Science
| mon| | History | | | French | | GYM
| ...| | ..... | | | ....... | |
+-----+--+----------+--+--+------------+--+-----------+
So at the end of every month - there is a rotation in the schedule - So the result should be
+----+--+------+--+--+--------------+--+--------------+
| day | | Group A | | | Group B | | Group C
+----+--+------+--+--+--------------+--+--------------+
| sat| | Algebra | | | physics | | Language
| sun| | Science | | | Chemistry| | Math
| mon| | GYM | | | History | | French
| ...| | ..... | | | ....... | |
+-----+--+----------+--+--+------------+--+-----------+
SO the subjects of Group B is the Subject of Group A ...> See the tables you should understand it better
So it is like rotation of the columns - i have a server side script that will run once at the end of the month to update the schedule
except i don't have any clue of how to achieve this with MySql - also i have a quite large number of groups (32 to be specific )
so any idea to how to reach this result ?
Not an answer. Too long for a comment.
An example of a normalised design might look like this
day group_ref subject
sat A physics
sat B language
sat C algebra
sun A chemistry
sun B math
sun C science
mon A history
mon B french
mon C gym
Note however that there is scope here for further optimisation
I am working on railway transport.
I have the following tables:
DAY table
| - id | name |
| 1 | sunday |
| 2 | monday |
| 3 | tuesday|
station table
|- id | name |
| 1 | zaria |
| 2 | kano |
| 3 | minna |
route table
| - id | source_station_id | destination_station_id|
| 1 | 1 | 2 |
| 2 | 1 | 3 |
| 3 | 2 | 3 |
| 4 | 3 | 2 |
| 3 | 3 | 1 |
departure table
| route_id | day_id | departure_time |
| 1 | 2 | 07:00 hrs |
| 1 | 3 | 07:00 hrs |
| 3 | 2 | 15:30 hrs |
route_id, day_id, source_station_id, destination_station_id are all foreign keys referenced from route, day, and station table respectively.
Now... how do I use sql to fetch from these tables..
output to look like the table below.
| source | destination | day | departure_time |
You want to use a JOIN:
SELECT
source.name AS source,
destination.name AS destination,
day.name AS day,
departure.time AS departure_time
FROM departure
JOIN station AS source
ON departure.source_station_id = source.id
JOIN station AS destination
ON departure.source_station_id = destination.id
JOIN day
ON departure.day_id = day.id
WHERE
# any specific criteria you may have, like:
departure.day_id = 1 # for a sunday
Should do it. For more information see MySQL JOIN Syntax
That said, this is a pretty standard question, and I would not be surprised to see it get voted down.
Further, from a structural point of view, you would really be better off storeing the "day" as a DATETIME, and converting this to and from a Human Readable date string on the fly.
As it stands, since you are only doing the day of the week, you have no way of differentiating between this week and last or last year. Have a look at the bottom-half of this comment, where I start talking about spanning periods of time, and discuss the use of DATETIME. Using native data-types is a preferable, both for performance, and ease of use.
Can anyone help me to sort this out pleaase. i have a episode table and for an episode there will be following appointments . Episode table will be like
+-------------+------------+------------+------------+----------------+------+
| Episode_id | Patientid | St_date | End_date | Status | ... |
+-------------+------------+------------+------------+----------------+------+
| 61112345 | 100001 | 12-01-2010 | | Active | |
| 61112346 | xxxxxx | 20-01-2010 | 10-10-2011 | Withdrawn | |
| ......... | xxxxxxxx | 30-01-2010 | 10-05-2011 | Lost to follow | |
| ......... | xxxxxxxx | 01-02-2011 | Active | Active | |
+-------------+------------+------------+------------+----------------+------+
Status field holds the status of each episode.A episode has 6 appointments , 3 months per appointment. so totally an episode has 18 months . some patient may complete all 6 appointment , some may withdraw in the middle, or some will be lost to follow up. i need to create a dashboard .
Appointment table will have fields for
Appointment_id
PatientId
...
Stats // Completed or pending, which is used for reporting
For example if a patient complete 2 appointment and if he is marked as Withdrawn on episdode which means that he has withdrawn from 3rd visit and active for 2 visits, if we lost to follow him on 5th app, then he will be active for 4app and then he will be added to lost to follow up on 5th visit. if he completes all then he will added to active for all 6 visits. and the report should be like
Report from 01-01-2010 to 31-12-2010
+--------+--------+-------------+----------------+---------+
| | Active | Withdrawn | Lost to follow | Revised |
+------- +--------+-------------+----------------+---------+
| visit1 | 1500 | 30 | 5 | 5 |
| Visit2 | 1800 | 20 | 4 | 3 |
| Visit3 | 1900 | 45 | 3 | 2 |
| Visit4 | 1800 | 34 | 0 | 1 |
| Visit5 | 1900 | 30 | 0 | 1 |
| Visit6 | 1200 | 20 | 0 | 5 |
+--------+--------+-------------+----------------+---------+
Currently we are fetching the query and using loop only we are generating reports like this, but it is taking time to process, is there any way i can achieve using query itself.
It isn't really clear what you want to group by, but I can give you a general answer. After your where clause you can add "group by fieldname order by fieldname" where fieldname is the element you want to count or sum. You can then count(fieldname) or sum(fieldname) to either add or count.
This may be helpful: http://www.artfulsoftware.com/infotree/qrytip.php?id=105
I have data that looks like this:
+----+------------+------------+------------+
| id | join_date | start_date | end_date |
+----+------------+------------+------------+
| 1 | 2005-11-01 | 2005-11-01 | 2014-04-11 |
| 2 | 2007-10-29 | 2007-10-29 | 2011-10-29 |
| 3 | 2005-11-17 | 2005-11-17 | 2014-11-15 |
| 4 | 2006-12-30 | 2009-12-30 | 2013-12-29 |
| 5 | 2005-12-03 | 2011-03-11 | 2013-11-29 |
+----+------------+------------+------------+
And the information about the fields above are as follows:
join_date is the date the account was first created.
start_date is the date of the start of an unbroken membership (eg, if
a member joins in 2005, lapses in 2006, and renews in 2008,
start_date would have a date in 2008).
end_date is when membership will lapse (whether a time in the future
or past).
I've been trying to come up with a query that will get me a count of all active members through time (the result being a time series graph of active accounts).
Nothing I've tried has really worked, and I think I just need another perspective. Any suggestions?
I am looking for a SQL statement that outputs missing calendar weeks based on a table. Here is a short example. We are using MySQL. I dropped all the irrelevant columns.
+------+--------------+
| Year | CalendarWeek |
+------+--------------+
| 2012 | 1 |
| 2012 | 5 |
| 2012 | 8 |
| 2012 | 9 |
| 2012 | 51 |
| 2013 | 2 |
+------+--------------+
What I am trying to get:
+------+--------------+
| Year | CalendarWeek |
+------+--------------+
| 2012 | 2 |
| 2012 | 3 |
| 2012 | 4 |
| 2012 | 6 |
| 2012 | 7 |
| 2012 | 10 |
| ... | ... |
| 2012 | 50 |
| 2012 | 52 |
| 2013 | 1 |
+------+--------------+
I added the dots to shorten the output.
Further background: The columns in each row are computed via some logic in Java. If we create a new row, lets say for the current calendar week, we have to check if we need to fill gaps. Its a simple routine. Is there a row for the previous week? Yes? Fine we are done. Otherwise compute the value for the previous week, insert it and check the week before that. The one and only do-while-loop in the whole program.
This apparently fails if we start with a table that has more than one gap. In this case we have to run through all the calendar weeks for every year and check for missing rows. Takes some time.
tl;dr I am trying to reduce roundtrips to the database with a shortcut.