self-join

What is a self join for? (in english)

血红的双手。 提交于 2019-11-26 21:02:30
I already know what a self-join does. Thank you, I also read all the other computerised operational descriptions on stack overflow, so I know this is not actually a duplicate question, so please do not give me tables or join lists. What I am seeking to why it would be done (and please, not just the self-referencing employee-manager example ). In plain English, what would I seek to achieve from a self join? My usage is in a university course, and coming from a Relational Algebra angle. I have done some SQL for a few years but the instructor loves to do self-joins on tables (after renaming one

Join two tables (with a 1-M relationship) where the second table needs to be 'flattened' into one row

亡梦爱人 提交于 2019-11-26 17:48:50
问题 Given the following tables: Student +----+-------+ | id | Name | +----+-------+ | 1 | Chris | | 2 | Joe | | 3 | Jack | +----+-------+ Enrollment +---------------+------------+-----------+----------+ | enrollment_id | student_id | course_id | complete | +---------------+------------+-----------+----------+ | 1 | 1 | 55 | true | | 2 | 1 | 66 | true | | 3 | 1 | 77 | true | | 4 | 2 | 55 | true | | 5 | 2 | 66 | false | | 6 | 3 | 55 | false | | 7 | 3 | 66 | true | +---------------+------------+----

recursive self query

Deadly 提交于 2019-11-26 16:09:56
问题 I have the following table: myTable: +----+----------+ | id | parentID | +----+----------+ | 1 | null | | 2 | 1 | | 3 | 1 | | 4 | 2 | | 5 | 4 | ----------------- i would like to get all rows tracing back until there's no parentID anymore. So ".... WHERE id=5" would give me: 5, 4, 2, 1 回答1: You are organizing your hierarchical data using the adjacency list model. The fact that such recursive operations are difficult is in fact one major drawback of this model. Some DBMSes, such as SQL Server

combinations (not permutations) from cross join in sql

我与影子孤独终老i 提交于 2019-11-26 14:24:07
问题 If I have a table that I'd like to cross join to itself, how can I remove the duplicate rows? Or to put it another way, how can I do a "order doesn't matter" cross join? So for example, if I have a table T: field | ------- A | B | C | and I cross join to itself so that i don't get the A | A rows T as t1 cross join T as t2 on t1.field != t2.field I would get the following: field | field ------+------- A | B A | C B | A B | C C | A C | B However, to me A, B is the same as B, A. Is there a good

Rails: self join scheme with has_and_belongs_to_many?

流过昼夜 提交于 2019-11-26 09:53:24
问题 I would like to create a structure of Users having many friends , also of class User : class User < ActiveRecord::Base has_and_belongs_to_many :friends, class_name: \"User\" end I do not need any details of their relationship thus I do not use :through with kind of class Friendship . But now I cannot find any way how to create corresponding database (neither with migration file nor using rails g model User username:string ... command). Any ideas? 回答1: Here are some resources which may be

What is a self join for? (in english)

孤人 提交于 2019-11-26 07:49:58
问题 I already know what a self-join does. Thank you, I also read all the other computerised operational descriptions on stack overflow, so I know this is not actually a duplicate question, so please do not give me tables or join lists. What I am seeking to why it would be done (and please, not just the self-referencing employee-manager example ). In plain English, what would I seek to achieve from a self join? My usage is in a university course, and coming from a Relational Algebra angle. I have

How to get matching data from another SQL table for two different columns: Inner Join and/or Union?

故事扮演 提交于 2019-11-26 01:09:41
问题 I\'ve got two tables in MS Access that keep track of class facilitators and the classes they facilitate. The two tables are structured as follows: tbl_facilitators facilID -> a unique autonumber to keep track of individual teachers facilLname -> the Last name of the facilitator facilFname -> the First name of the facilitator tbl_facilitatorClasses classID -> a unique autonumber to keep track of individual classes className -> the name of the class (science, math, etc) primeFacil -> the

What is SELF JOIN and when would you use it? [duplicate]

耗尽温柔 提交于 2019-11-26 01:06:01
问题 Possible Duplicate: sql: self-joins explained What is self join and when would you use it? I don\'t understand self joins so a layman explanation with an example would be great. 回答1: You use a self join when a table references data in itself. E.g., an Employee table may have a SupervisorID column that points to the employee that is the boss of the current employee. To query the data and get information for both people in one row, you could self join like this: select e1.EmployeeID, e1

Explanation of self-joins

冷暖自知 提交于 2019-11-26 00:52:57
问题 I don\'t understand the need for self-joins. Can someone please explain them to me? A simple example would be very helpful. 回答1: You can view self-join as two identical tables. But in normalization, you cannot create two copies of the table so you just simulate having two tables with self-join. Suppose you have two tables: Table emp1 Id Name Boss_id 1 ABC 3 2 DEF 1 3 XYZ 2 Table emp2 Id Name Boss_id 1 ABC 3 2 DEF 1 3 XYZ 2 Now, if you want to get the name of each employee with his or her boss

Simplest way to do a recursive self-join?

╄→尐↘猪︶ㄣ 提交于 2019-11-26 00:32:27
问题 What is the simplest way of doing a recursive self-join in SQL Server? I have a table like this: PersonID | Initials | ParentID 1 CJ NULL 2 EB 1 3 MB 1 4 SW 2 5 YT NULL 6 IS 5 And I want to be able to get the records only related to a hierarchy starting with a specific person. So If I requested CJ\'s hierarchy by PersonID=1 I would get: PersonID | Initials | ParentID 1 CJ NULL 2 EB 1 3 MB 1 4 SW 2 And for EB\'s I\'d get: PersonID | Initials | ParentID 2 EB 1 4 SW 2 I\'m a bit stuck on this