self-join

How do I do reflexive self-join relationships in ActiveRecord?

萝らか妹 提交于 2019-12-18 04:25:08
问题 I'm trying to implement a social networking style friendship model and I didnt have much much luck trying to figure out the plugins available out there. I think I'll learn Rails better if I do it myself. So here's what I have : class User < ActiveRecord::Base has_many :invitee_friendships , :foreign_key => :friend_id, :class_name => 'Friendship' has_many :inviter_friends, :through => :invitee_friendships has_many :inviter_friendships , :foreign_key => :user_id, :class_name => 'Friendship' has

SQL Return every row even if Null values

…衆ロ難τιáo~ 提交于 2019-12-13 10:33:09
问题 I want to return all rows from a Contracts table but a second WHERE clause yields only rows which are not null. (In other words in the code below the 'CAD' restriction means approx half the possible rows have no value traded in Canadian Dollars and thus are not returned --whereas I want all possible rows returned showing NULL values where applicable). I figure it's a Left Self Join but am struggling with the syntax (and/or whether I need to do an Inner Select), SELECT MeasurableID, EntityID,

SQL Self-join with data comparison for different days

社会主义新天地 提交于 2019-12-13 08:19:09
问题 I need to compare data on two different days in SQL. And I really need that in a single query since I need to use the results in pagination. Problem is, when I'm doing a self join it's results in duplicate columns since INNER JOIN is a cartesian product. Here's the code on sql fiddle E.g. SELECT * FROM `my_table` as t1 INNER JOIN my_table t2 ON t1.quality = t2.quality WHERE ( t1.day = '2015-01-08' OR t1.day = '2015-01-09' OR t2.day = '2015-01-08' OR t2.day = '2015-01-09' ) Two questions: How

MySql - Self Join - Full Table Scan (Cannot Scan Index)

烂漫一生 提交于 2019-12-13 08:01:54
问题 I have the following self-join query: SELECT A.id FROM mytbl AS A LEFT JOIN mytbl AS B ON (A.lft BETWEEN B.lft AND B.rgt) The query is quite slow, and after looking at the execution plan the cause appears to be a full table scan in the JOIN. The table has only 500 rows, and suspecting this to be the issue I increased it to 100,000 rows in order to see if it made a difference to the optimizer's selection. It did not, with 100k rows it was still doing a full table scan. My next step was to try

Optimization of query using covering indices

一个人想着一个人 提交于 2019-12-13 05:55:13
问题 I have the following query with a subquery and self join: SELECT bucket.patient_sid AS sid FROM (SELECT clinical_data.patient_sid, clinical_data.lft, clinical_data.rgt FROM clinical_data INNER JOIN (SELECT clinical_data.patient_sid, clinical_data.lft, clinical_data.rgt, clinical_data.attribute_id FROM clinical_data WHERE clinical_data.attribute_id = '33' AND clinical_data.string_value = '2160-0') AS attribute ON clinical_data.patient_sid = attribute.patient_sid AND clinical_data.lft >=

No Current Record Error in MS Access 2010 while using a self join

流过昼夜 提交于 2019-12-13 04:24:28
问题 I'm replacing a subquery with an self join to improve performance of my query. The old subquery was like this: (SELECT fage2.agecat FROM people AS fage2 WHERE fage2.aacode = people.aacode AND fage2.persno = 2) AS RAge2, The new self join is like this: (SELECT [People].[AgeCat] FROM [People] INNER JOIN [People] AS p2 ON [People].[aacode] = [P2].[aacode] WHERE [P2].[PERSNO] = 2 ) AS RAge2, but returns a No Current Record error message. The goal is to find the record that has the same aacode but

MySQL - Correct approach event counting

女生的网名这么多〃 提交于 2019-12-13 03:05:32
问题 I want to list users which have a particular event count but I'm confused on which approach to take. This is the database table: CREATE TABLE `event` ( `event_id` int(11) unsigned NOT NULL AUTO_INCREMENT, `visitor_id` int(11) DEFAULT NULL, `key` varchar(200) DEFAULT NULL, `value` text, `label` varchar(200) DEFAULT '', `datetime` datetime DEFAULT NULL, PRIMARY KEY (`event_id`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; INSERT INTO `event` (`event_id`, `visitor_id`, `key`, `value`,

How to left join or inner join a table itself

夙愿已清 提交于 2019-12-12 10:54:39
问题 I have this data in a table, for instance, id name parent parent_id 1 add self 100 2 manage null 100 3 add 10 200 4 manage null 200 5 add 20 300 6 manage null 300 How can I left join or inner join this table itself so I get this result below? id name parent 2 manage self 4 manage 10 6 manage 20 As you can I that I just want to query the row with the keyword of 'manage' but I want the column parent 's data in add 's row as the as in manage 's row in the result. Is it possible? EDIT: the

Zend Select with self join overwriting fields

你说的曾经没有我的故事 提交于 2019-12-12 05:29:34
问题 Posts and comments are stored in the same table. So to get each post and its comments we do this: $posts = $this->select()->setIntegrityCheck(false) ->from(array('post' => 'Posts'), array('*')) ->where('post.idGroup = ' . $idGroup) ->where('post.idTopic IS NULL') ->order('post.date DESC') ->limit($resultsPerPage, $resultsPerPage * ($page - 1)) ->joinLeft(array('user' => 'Users'), 'post.idUser = user.idUser', array('idUser', 'fname', 'lname', 'profileUrl', 'photoUrl')) ->joinLeft(array(

Laravel Eloquent Self Join Parent Child

不问归期 提交于 2019-12-12 03:49:30
问题 Laravel Eloquent Self Join Parent Child Relationship class Product_category extends Model { // protected $table='PRODUCT_CATEGORIES'; public $timestamps = false; public function getParentCategory() { return $this->hasOne(self::class, 'id', 'parent_id'); }. public function getChildCategories(){ return $this->hasMany(self::class, 'parent_id','id'); } I am able to retrieve all child records of table by making use of getChildeCategories but not able to retrieve Parent of particular category. It