self-join

MySQL A or B but NOT both

自作多情 提交于 2019-12-02 12:38:35
This seems like an easy query but I cannot seem to get it or relate it to other posts on stack overflow. Would anyone be able to explain... This is what I have so far, it is returning records for all bars where one or both people go. TBL frequents Schema - drinker VARCHAR(50) PK, bar VARCHAR(50) PK Bars which are frequented by John or Rebecca but not by both of them SELECT DISTINCT bar FROM frequents WHERE drinker = 'John' XOR drinker = 'Rebecca' AND bar NOT IN ( SELECT f1.bar FROM frequents f1, frequents f2 WHERE ( f1.drinker = 'John' AND f2.drinker = 'Rebecca' AND f1.bar = f2.bar ) );

Understanding the number of matched rows in LEFT JOIN

北战南征 提交于 2019-12-01 14:50:22
Here is my table structure: SQL Fiddle CREATE TABLE mytable ( id int, related int ); INSERT into mytable VALUES(1, NULL); INSERT into mytable VALUES(2, 1); INSERT into mytable VALUES(3, 1); And I have two queries: -- returns 3 rows SELECT t1.id, t2.id FROM mytable as t1 LEFT JOIN mytable as t2 ON t1.related = t2.id; -- returns 4 rows SELECT t1.id, t2.id FROM mytable as t1 LEFT JOIN mytable as t2 ON t1.id = t2.related; Those queries are almost similar and that's a self-join . But as you can see in the fiddle, the first query returns 3 rows and the second one returns 4 rows. That not what I've

Understanding the number of matched rows in LEFT JOIN

百般思念 提交于 2019-12-01 13:29:23
问题 Here is my table structure: SQL Fiddle CREATE TABLE mytable ( id int, related int ); INSERT into mytable VALUES(1, NULL); INSERT into mytable VALUES(2, 1); INSERT into mytable VALUES(3, 1); And I have two queries: -- returns 3 rows SELECT t1.id, t2.id FROM mytable as t1 LEFT JOIN mytable as t2 ON t1.related = t2.id; -- returns 4 rows SELECT t1.id, t2.id FROM mytable as t1 LEFT JOIN mytable as t2 ON t1.id = t2.related; Those queries are almost similar and that's a self-join . But as you can

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

眉间皱痕 提交于 2019-12-01 09:47:36
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")] # X i.X N #1: 1 1 3 #2: 2 1 3 #3: 3 1 1 #4: 1 2 3 #5: 2 2 4 #6: 3 2 1 #7: 1 3 1 #8: 2 3 1 #9: 3 3 3 #10:

ActiveRecord::AssociationTypeMismatch in Controller#create on dropdown select for a Rails self join

不羁的心 提交于 2019-11-30 20:34:56
问题 I'm getting an ActiveRecord::AssociationTypeMismatch error on my self join in Rails 5 that I can't figure out how to fix. It's a simple rails app where a user can share a quote by an Artist (such as David Bowie) about another Artist (such as Lou Reed). So, a quote might look like this: Quote Topic: David Bowie Content: "He was a master." Speaker: Lou Reed I have a Quote model and an Artist model and the Topics and Speakers are defined as self joins on the Artist model. Here are the models:

Self-referencing models in Rails 3

一世执手 提交于 2019-11-30 14:50:05
I have an Entity model and I want to display connections between the Entities. ie, Entity 1 is connected to Entity 2. My thinking, right now, is to create a join model between the two called Connection and have it work like a traditional rails join table. Except have the columns be entity_one_id and entity_two_id, then establish a many-to-many relationship between Entity and Connection. This seems like a really not-elegant way to do this. I was wondering if anyone had any better ideas? Maybe something more rails-esque that I'm just not seeing? That's the most-common way to do it. If an entity

Can't understand EclipseLink warning

血红的双手。 提交于 2019-11-30 11:09:34
I'm using EclipseLink 2.3.1 to model self referencing table with JPA 2. I get weird warning from EclipseLink when I create the EntityManager. [EL Warning]: 2011-11-27 14:28:00.91--ServerSession(8573456)--Reverting the lazy setting on the OneToOne or ManyToOne attribute [redirectID] for the entity class [class lp.db.model.Site] since weaving was not enabled or did not occur. I couldn't find any documentation about this warning, and I'm not sure what it means. I also want to know how to solve the problem that causes this warning to appear... I'm new to JPA so it might be a silly thing. My

INNER JOIN same table

邮差的信 提交于 2019-11-30 11:01:01
I am trying to get some rows from the same table. It's a user table: user has user_id and user_parent_id . I need to get the user_id row and user_parent_id row. I have coded something like this: SELECT user.user_fname, user.user_lname FROM users as user INNER JOIN users AS parent ON parent.user_parent_id = user.user_id WHERE user.user_id = $_GET[id] But it doesn't show the results. I want to display user record and its parent record. Ronnis I think the problem is in your JOIN condition. SELECT user.user_fname, user.user_lname, parent.user_fname, parent.user_lname FROM users AS user JOIN users

Most efficient method for persisting complex types with variable schemas in SQL

此生再无相见时 提交于 2019-11-29 22:01:24
问题 What I'm doing I am creating an SQL table that will provide the back-end storage mechanism for complex-typed objects. I am trying to determine how to accomplish this with the best performance. I need to be able to query on each individual simple type value of the complex type (e.g. the String value of a City in an Address complex type). I was originally thinking that I could store the complex type values in one record as an XML, but now I am concerned about the search performance of this

Self-referencing models in Rails 3

眉间皱痕 提交于 2019-11-29 21:16:43
问题 I have an Entity model and I want to display connections between the Entities. ie, Entity 1 is connected to Entity 2. My thinking, right now, is to create a join model between the two called Connection and have it work like a traditional rails join table. Except have the columns be entity_one_id and entity_two_id, then establish a many-to-many relationship between Entity and Connection. This seems like a really not-elegant way to do this. I was wondering if anyone had any better ideas? Maybe