Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
My timetable table looks like this:
id period mon mon_tch tue tue_tch wed wed_tch
-- ------ --- ------- --- ------- --- -------
1 prd1 4 5 8 7 6 3
2 prd2 6 3 4 5 8 7
My teacher-subject table:
id tchr subject
-- ---- -------
1 5 4
2 7 8
where values in mon is the subject_id and mon_tch is the teacher_id and so on.
When admin changes the subject of a teacher in the 'teacher-subject' table via a form (example: subject of teacher with id 5 is changed from 8 to 9), I want to update my timetable table with the new subject assigned.
(consider the subject field in the teacher-subject table will be updated somehow).
A normalised design might look like this...
period day subject teacher
--------------------------
1 mon 4 5
1 tue 8 7
1 wed 6 3
2 mon 6 3
2 tue 4 5
2 wed 8 7
... where (period,day) constitutes a compound PK. That said, there may still be some redundancy here.
Related
I'm working on a multi-level referral system for signup based on PHP, but I can do the signup process to first level only. The referral system levels are managed from administrator control panel.
Table: referral_levels
id referral_level
-------- --------
1 1
2 2
3 3
4 4
Table: referrals
id new_user refferal_level referral_user_id
-------- -------- -------------- ----------------
1 7 1 6
2 8 2 6
3 8 1 7
4 9 3 6
5 9 2 7
6 9 1 8
7 10 4 6
8 10 3 7
9 10 2 8
10 10 1 9
On user signup I want to check the following:
1. If the current user who signup is referred by (1)another user - DONE!
2. If the (1)another user has been referred by (2)someone else
3. If (2)someone else is referred again by "x" user and so on...
I'm confused how to write the code for p.2 and p.3 when the signup form is posted and have to check who is referred by who and insert the data for sub-level referrals.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have one table like this.
id conversation_id messageable_id
1 1 3 <--
2 1 5
3 2 7
4 2 3 <--
5 3 7
6 3 9
One conversation has two member.
If sender is 3, I have to get all conversation and receiver like following.
conversation_id sender_id recevicer_id [sic]
1 3 5
2 3 7
How can I build query?
Thanks in advance.
You could use lead() (available in MySQL 8.0):
select *
from (
select
conversation_id,
messageable_id sender_id,
lead(messageable_id) over(partition by conversation_id order by id) receiver_id
from mytable
) t
where sender_id = 3
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
i hava a table
id sentUID recUID Amount
1 1 2 100
2 2 1 100
3 4 2 100
4 2 1 100
5 8 6 100
6 2 9 100
and i want to get userID 2 record like this
id sentUID recUID Amount
1 1 2 100
3 4 2 100
6 2 9 100
only three UserID 1, 4, 9 sent or recive amount.
I think you want:
select t.*
from t
where 2 in (sentUID, recUID)
order by id;
Try this:
SELECT * FROM data WHERE id IN
(SELECT tab1.mId FROM
(SELECT MIN(id) as mId, CASE WHEN sentUID=2 THEN recUID
WHEN recUID=2 THEN sentUID
END AS person
FROM data WHERE recUID=2 OR sentUID=2
GROUP BY person)
AS tab1);
result:
id sentUID recUID Amount
1 1 2 100
3 4 2 100
6 2 9 100
the intermediate table tab1 lists each of the unique UIDs that appear with 2 and is used to calculate the final result:
mId person
1 1
3 4
6 9
http://sqlfiddle.com/#!9/16f6a5/13/0
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have a userbase on db
______________________________________________
id | Name | so many details | Unique page link
______________________________________________
1 | abc | bla bla | username
So, user abc have a page xyz.com/username
I want to show user abc stats about visitors i.e. like
1 jan - 400
2 jan - 350
and so on for last 7 day
and also the month wise record for last 12 months
jan - 49009
feb - 73849
what would be the best MYSQL database table structure design.
_________________________________________________________________
user id | day 1 | day2| and so on for 7 day | Jan | Feb | Mar
______________________________________________________________
111111 | 400 | 300 | | 4250|24534|2435
I thought of something like this - is it OK or other optimized design is there?
I would not keep a count in a database this way.
I would store records with timestamps as a column and roll them up using a GROUP BY every time I wanted them.
If this is a reporting database, I'd recommend looking into a star schema with a time dimension.
You don't want to do it your way because you will have a long calender for each user.
It will get out of hand quickly.
You would want to have a table containing:
ID
user_id
time_stamp
info
1
324
2014/1/22
300
2
327
2014/1/20
500
3
324
2014/1/19
900
Than when you want the info.
Select * FROM table where user_id = 324
Would return
ID
user_id
time_stamp
info
1
324
2014/1/22
300
3
324
2014/1/19
900
aI think that something like this would be more easy to manage.
user_id
date
counter
For every visit check if the user is there for that date. if so increment, otherwise create record.
Then you can do as many stats as you want by using the date (day, month, interval etc.)
I've done some digging and I can't find an effective way to prevent duplicate entries based on my needs. I need columns 2 (proj_id) and column 4 (dept_id) never to be the same, as each dept would only work on a project once. So, rows 1 and 4, 6 and 7, and 14 and 15 shouldn't be allowed. I'll keep digging as well.
summary_id proj_id hours_id dept_id date_entered
1 8 3 6 9/9/2012
2 2 2 6 9/9/2012
3 1 6 19 9/9/2012
4 8 3 6 9/9/2012
5 2 5 17 9/9/2012
6 7 2 5 9/9/2012
7 7 2 5 9/9/2012
8 2 5 17 9/9/2012
9 7 4 17 10/10/2012
10 3 6 1 10/10/2012
11 5 1 15 10/10/2012
12 4 4 3 10/10/2012
13 3 5 1 10/10/2012
14 8 2 13 10/10/2012
15 8 2 13 10/10/2012
Before applying unique combine key to your table, you have to remove duplicate records first then apply the following sql command:
ALTER TABLE your_table_name ADD UNIQUE (proj_id, dept_id);
Define an unique key on both columns
ALTER TABLE `your_table` ADD UNIQUE (`proj_id`, `dept_id`);
Looks like you are new to php and mysql. So here's the easiest way to do it.
Log on to PHPMyAdmin
Select your DB and your table.
View the Structure of it (clicking the button on top of the screen).
Check the two fields (proj_id and dept_id) using the check box on the left.
At the bottom of the table you should find the the words "With selected" and in front of it some actions. Select the option to make "Primary".
Of course, if you have duplicate entries first delete them.