Get child rows in an array based on Parent table in YII - yii

student table:
student_id name
-------------- -----
1 aaaa
2 bbbb
3 cccc
subjects table:
subject_id student_id sub1 sub2 sub3
----------- ---------- ----- ---- ----
1 1 30 40 60
2 1 40 13 88
3 1 50 76 45
4 1 40 65 87
5 2 30 34 78
6 2 40 54 76
7 2 70 45 67
One to Many relationship
I have the students list form. when I click on edit for student 1
I want to get all the child rows related to student1 in an array.
How do I do this. any help is appreciated.

Open the model of student table check the relations function..
public function relations() {
return array(
'subjects' => array(self::HAS_MANY, 'Subject', 'student_id'),
......
);
}
where Subject is the name of the model of your subject table..
// List data need when populating dropdown from database values
<?php echo $form->dropDownList($model,'subject_id', CHtml::listData($model->subjects,'subject_id','subject_name'));?>
Here $model is the model of the student in your student form view, the listdata function will list all the subjectsof the student and the dropDownList function will make dropdown of them...., if you are comfirtable in showing the subjects by id then replace subject_name with subject_id, otherwise What you need is a subject title in subject table..
I hope I solved your problem..fr more refrences read relations, dropdownlist and listdata in yii documentation.

Related

how to check and match data in column1 inside table 1 with column2 inside table 2 and get the updated values in side table 3

how to check and match data in column1 inside table 1 with column2 inside table 2 and get the updated values in side table 3
table 1
ID name: status : age
1 john F 28
2 peter G 20
3 Roger K 67
Table 2:
ID name: status : age
1 john Y 28
2 peter J 20
3 Roger K 67
4 trump U 120
5 Donald F 450
Table 3 should contain the updated values
1 john Y 28
2 peter J 20
3 Roger K 67
I need to get the updated status of IDs present in table 1 in table 3 how can I do that.
Note: I am using exacttarget SQL activity and update and many more functionalities does not work so I need some work around> I have tried this but this does not work.
UPDATE
1C-C1-MatchStatus_72hoursSubscribers
SET
1C-C1-MatchStatus_72hoursSubscribers.current_status = B.current_status
FROM
1C-C1-MatchStatus_72hoursSubscribers A
INNER JOIN
a_query B
ON
A.current_status = B.current_status

How to add values to the pandas dataframe coulmn depending upon value of column in other dataframe

I have pandas dataframe somthing like this. This dataframe contains unique user_id and corresponding user-name
df
user_id user_name
1 Jack
2 Neil
3 Peter
4 Smith
5 Neev
And I have second dataframe something like this
df1
user_id item_id user_name
1 23 Null
1 24 Null
2 34 Null
3 35 Null
5 45 Null
I want to fill user_name column above from the 1st dataframe. So,where user_id is matched it should enter corresponding user_name in that position.
So it should look like this..
df1
user_id item_id user_name
1 23 Jack
1 24 Jack
2 34 Neil
3 35 Peter
5 45 Neev
I am doin following in python
b = df.user_name[df['user_id'].isin(df1['user_id'])]
df1['user_name'] = b
But,It drops duplicates. I don't want to do that. Please help.
Use merge:
In [299]:
df1[['user_id','item_id']].merge(df,on='user_id')
Out[299]:
user_id item_id user_name
0 1 23 Jack
1 1 24 Jack
2 2 34 Neil
3 3 35 Peter
4 5 45 Neev

How to create a view using relation from a different table

There are two tables STUDENT and RELATION
Student
Roll No | Name | Marks
1 Peter 40
2 Daniel 45
3 Emma 43
4 Drake 47
5 John 49
Relation
Roll No | Younger Sibling
1 2
2 NULL
3 NULL
4 3
5 NULL
Now i want to create a view which shows data like this
Roll No | Marks | Sibling Marks
1 40 45
2 45 0
3 43 0
4 47 43
5 49 0
Assume that there can only be max two siblings in the table and Roll No is the primary key for both tables. Relation between siblings is only one way (younger sibling). I am really new to SQL, any help would be much appreciated.
Try this:
SELECT
R.RolNo,
COALESCE(S1.Marks, 0) Marks,
COALESCE(S2.Marks, 0) SiblingMarks
FROM Relation R
LEFT JOIN Student S1
ON R.RolNo = S.RolNo
LEFT JOIN Student S2
ON R.YoungerSibling = S2.RolNo

Add AUTO INCREMENT column to Sqlite View

I have a table lets called it myTable
tID Name Degree
1 A 23
2 B 55
3 C 77
4 D 45
I want to select only pass Degrees in New View lets call it MyView
The result will be
tID Name Degree
--- ---- --------
2 B 55
3 C 77
But I want myView have sequences start from one by one as follows:
index Name Degree
----- ---- ------
1 B 55
2 C 77
It's possible?
I need to do this because I have a the original table have:
tID sID Name Lesson TryNo Degree
1 1 A a1 1 23
2 1 A a1 2 66
3 2 A b1 1 55
4 2 A b1 2 77
I want to select max tries degree for every lesson exams for each student. ex:
tID sID Name Lesson TryNo Degree
2 1 A a1 2 66
4 2 A b1 2 77
Thanks
I think this is not possible directly since View is just a mirror representation of your tables.
View is created using Select statements, as far as I remember, it is not possible to have a select statement with variables.
Let me know if this works:
CREATE or replace VIEW myView AS
SELECT rownum id,
marks,
name
FROM studentinfo
WHERE degree>50

Microsoft SQL - Remove duplicate data from query results

I am new to SQL Server and need help with one of my SQL query.
I have 2 tables (Rating and LikeDislike).
I am trying to get data from both of these tables using a LEFT JOIN like this:
SELECT distinct LD.TopicID, R.ID, R.Topic, R.CountLikes, R.CountDisLikes, LD.UserName, LD.Clikes
FROM Rating As R
LEFT JOIN LikeDislike AS LD on LD.TopicID = R.ID
The above SELECT statement displays results fine but also includes duplicates. I want to remove duplicates when the data is displayed, I tried using DISTINCT and GROUP BY, but with no luck, maybe because I am not using it correctly.
To be more clear and less confusing let me tell you what exactly each table does and what I am trying to achieve.
The Rating table has following columns (ID, Topic, CountLikes, CountDisLikes, Extra, CreatedByUser). It stores topic information and number of likes and dislikes for each topics and the UserID of the user who created that topic.
Rating table with sample data
ID Topic CountLikes CountDisLikes Extra CreatedByUser
1 Do You Like This 211 58 YesId 2
2 Or This 17 25 This also 3
79 Testing at home 1 0 Testing at home 2
80 Testing at home again 1 0 Testing 2
82 testing dislikes 0 1 Testing 2
76 Testing part 3 7 5 Testing 3 4
77 Testing part 4 16 6 Testing 4 5
The LikeDisLike table has following columns (ID, TopicID, UserName, Clikes). TopicID is a FK to the ID column in Rating table.
LikeDislike table with sample data
ID TopicID UserName Clikes
213 77 2 TRUE
214 76 2 FALSE
215 77 5 TRUE
194 77 3 TRUE
195 76 3 FALSE
196 2 3 TRUE
197 1 3 FALSE
Now what I am trying to do is get information from both of this table without duplicate rows. I need to get data all the columns from Rating table + UserName and Clikes columns from LikeDislike table without any duplicate rows
Below are the results with duplicates
TopicID ID Topic CountLikes CountDislikes UserName Clikes
NULL 79 Testing at home 1 0 NULL NULL
NULL 80 Testing at home2 1 0 NULL NULL
NULL 82 testing dislikes 0 1 NULL NULL
1 1 Do You Like This 211 58 3 FALSE
2 2 Or This 17 25 3 TRUE
76 76 Testing part 3 7 5 2 FALSE
76 76 Testing part 3 7 5 3 FALSE
77 77 Testing part 4 16 6 2 TRUE
77 77 Testing part 4 16 6 3 TRUE
77 77 Testing part 4 16 6 5 TRUE
Just like in yesterday's post, I don't think you understand what DISTINCT is suppose to return you. Because you have different values in your LikeDislike table, you are returning the DISTINCT rows.
Let's take TopicId 77 for instance. It returns 3 DISTINCT rows because you have 3 matching records in your LikeDislike table. If your desired output is a single row where the UserName and Clikes are comma delimted, that is possible -- look into using for xml and perhaps stuff (here is a recent answer on the subject). Or if you want to return the first row that matches the TopicId, then that is possible as well -- look into using a subquery with row_number.
Please let us know your desired output and we can help provide a solution.
Good luck.