self-join

Get Common Rows Within The Same Table

杀马特。学长 韩版系。学妹 提交于 2019-12-23 17:24:59
问题 I've had a bit of a search, but didn't find anything quite like what I'm trying to achieve. Basically, I'm trying to find a similarity between two users' voting habits. I have a table storing each individual vote made, which stores: voteID itemID (the item the vote is attached to) userID (the user who voted) direction (whether the user voted the post up, or down) I'm aiming to calculate the similarity between, say, users A and B, by finding out two things: The number of votes they have in

Doctrine2 Self Join, How to join on self without relationship column?

北城余情 提交于 2019-12-23 13:08:45
问题 Example mysql query: SELECT a1.* FROM agreement a1 LEFT JOIN agreement a2 on a1.agreementType = a2.agreementType and a2.id > a1.id WHERE a2.id is null The purpose of the query is to get the last agreement of the type returned. There are many types and I want only a listing of each latest agreement for each type. My example query above works as expected, but no go in DQL. How would I do this in DQL given I do not have a column that refers to itself? Note that "agreementType" is also a foreign

Comparing SQL Table to itself (Self-join)

余生颓废 提交于 2019-12-23 07:45:26
问题 I'm trying to find duplicate rows based on mixed columns. This is an example of what I have: CREATE TABLE Test ( id INT PRIMARY KEY, test1 varchar(124), test2 varchar(124) ) INSERT INTO TEST ( id, test1, test2 ) VALUES ( 1, 'A', 'B' ) INSERT INTO TEST ( id, test1, test2 ) VALUES ( 2, 'B', 'C' ) Now if I run this query: SELECT [LEFT].[ID] FROM [TEST] AS [LEFT] INNER JOIN [TEST] AS [RIGHT] ON [LEFT].[ID] != [RIGHT].[ID] WHERE [LEFT].[TEST1] = [RIGHT].[TEST2] I would expect to get back both id's

Comparing SQL Table to itself (Self-join)

家住魔仙堡 提交于 2019-12-23 07:44:42
问题 I'm trying to find duplicate rows based on mixed columns. This is an example of what I have: CREATE TABLE Test ( id INT PRIMARY KEY, test1 varchar(124), test2 varchar(124) ) INSERT INTO TEST ( id, test1, test2 ) VALUES ( 1, 'A', 'B' ) INSERT INTO TEST ( id, test1, test2 ) VALUES ( 2, 'B', 'C' ) Now if I run this query: SELECT [LEFT].[ID] FROM [TEST] AS [LEFT] INNER JOIN [TEST] AS [RIGHT] ON [LEFT].[ID] != [RIGHT].[ID] WHERE [LEFT].[TEST1] = [RIGHT].[TEST2] I would expect to get back both id's

self join vs inner join

那年仲夏 提交于 2019-12-23 06:41:13
问题 what is difference between self join and inner join 回答1: I find it helpful to think of all of the tables in a SELECT statement as representing their own data sets. Before you've applied any conditions you can think of each data set as being complete (the entire table, for instance). A join is just one of several ways to begin refining those data sets to find the information that you really want. Though a database schema may be designed with certain relationships in mind (Primary Key <->

self join vs inner join

十年热恋 提交于 2019-12-23 06:41:02
问题 what is difference between self join and inner join 回答1: I find it helpful to think of all of the tables in a SELECT statement as representing their own data sets. Before you've applied any conditions you can think of each data set as being complete (the entire table, for instance). A join is just one of several ways to begin refining those data sets to find the information that you really want. Though a database schema may be designed with certain relationships in mind (Primary Key <->

Select new or returning items for a specified year

◇◆丶佛笑我妖孽 提交于 2019-12-23 03:38:12
问题 I have a table which looks roughly like this: +---------------------+----------+ | date | item | +---------------------+----------+ | 2008-11-30 11:15:59 | Plums | | 2012-11-08 19:42:37 | Lemons | | 2013-01-30 18:58:07 | Apples | | 2013-02-12 13:44:45 | Pears | | 2014-06-08 11:46:48 | Apples | | 2014-09-01 20:28:03 | Oranges | +---------------------+----------+ I would now like to select items for two cases: 1) I'd like to select all distinct items for this year (or a different specified year

Multiple Foreign keys to a single table and single key pointing to more than one table

梦想与她 提交于 2019-12-22 09:25:33
问题 I need some suggestions from the database design experts here. I have around six foreign keys into a single table (defect) which all point to primary key in user table. It is like: defect (.....,assigned_to,created_by,updated_by,closed_by...) If I want to get information about the defect I can make six joins. Do we have any better way to do it? Another one is I have a states table which can store one of the user-defined set of values. I have defect table and task table and I want both of

Self Join in Eloquent

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-22 05:36:16
问题 How would you write a self join in eloquent? Would I need to define the relationship on the model? Here's my statement: SELECT t2.title FROM products t1, products t2 WHERE t1.id = $id AND t2.color_id = t1.color_id AND t2.id != $id 回答1: You can simply define a relation to itself. public function parent() { return $this->belongsTo(self::class, 'color_id'); } public function children() { return $this->hasMany(self::class, 'color_id'); } 来源: https://stackoverflow.com/questions/30592793/self-join

Summarize the self-join index while avoiding cartesian product in R data.table

喜你入骨 提交于 2019-12-19 10:26:06
问题 With a 2-column data.table , I'd like to summarize the pairwise relationships in column 1 by summing the number of shared elements in column 2. In other words, how many shared Y elements does each pairwise combination of X-values have? For example, I can do this in a 2-step process, first doing a cartesian cross join, then summarizing it like so: d = data.table(X=c(1,1,1,2,2,2,2,3,3,3,4,4), Y=c(1,2,3,1,2,3,4,1,5,6,4,5)) setkey(d, Y) d2 = d[d, allow.cartesian=TRUE] d2[, .N, by=c("X", "i.X")] #