joins

MySQL: Multi-column join across multiple tables?

I have a set of three tables: Dining_Tables; +--------------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------------+--------------+------+-----+---------+-------+ | dining_table | int(11) | NO | PRI | NULL | | | bus_boy ...

Can't define :joins conditions in has_many relationship ?

Hi, I have a relationship table : create_table "animal_friends", :force => true do |t| t.integer "animal_id" t.integer "animal_friend_id" t.datetime "created_at" t.datetime "updated_at" t.integer "status_id", :default => 1 end linking animals to others. Best way to retreive associations in SQL is : SE...

sql query joins multiple tables - too slow (8 tables)

hi, i'm trying to join 8 tables into one in order to create index used by other application, my query is like : (my mysql skill's very amateur) SELECT t1_id, t2_name, t3_name, t4_name, t5_name, t6_name, t7_name, t8_name, t9_name FROM t1 LEFT JOIN t2 ON (t1_id = t2_id) LEFT JOIN t3 ON (t3_id = t1_id) LEFT JOIN t4 ON (t4...

What's the difference between just using multiple froms and joins?

Say I have this query: SELECT bugs.id, bug_color.name FROM bugs, bug_color WHERE bugs.id = 1 AND bugs.id = bug_color.id Why would I use a join? And what would it look like? ...

Is this a bad way to structure my Sql Server database?

Hi folks, I have a table that contains a few columns and then 2 final (nullable) columns which are varbinary (actually, they are SQL 2008 geography types, but I want to keep this post database agnostic). I've hit around 500mb with around 200K rows. The varbinary is the problem - and I need the data. So, I was wondering if it's bad if ...

Store forum threads replies

I'm making a forum. I'm gonna store the id, subject, date, poster in a table named topics and keep the content in a table named posts. Now I just have to join them together nicely. And how should I do to connect the post with the topic? I haven't slept in 30 hours so I'm a little slow :P What do you guys think about this approach? ...

How to Join Multiple Joins yet return Distinct Values

Hi, I am trying to Join Multiple Tables. Table 1 has distinct ID's Table 2 has multiple names for each ID SAY 3 NAMES Table 3 has 2 dates for each ID when i join the Three tables, I get 6 rows of data for each ID with each of the Names appearing Twice and each of the dates appearing thrice. I want only 3 rows to be written returning d...

SQL join over five tables

I have five tables: models: id, name, specification models_networks: id, model_id, network_id networks: id, name, description countries_networks: id, country_id, network_id countries: id, countryName, etc, etc the models table is connected to the networks table via models_networks with a many to man...

Mysql join syntax

If I want to perform joins on 3 or more tables, what is the best syntax? This is my attempt: Select * from table1 inner join table2 using id1, table2 inner join table3 using id2, table3 inner join table4 using id4 where table2.column1="something" and table3.column4="something_else"; does that look right? The things I'm not sure ...

How to retrieve non-matching results in mysql

I'm sure this is straight-forward, but how do I write a query in mysql that joins two tables and then returns only those records from the first table that don't match. I want it to be something like: Select tid from table1 inner join table2 on table2.tid = table1.tid where table1.tid != table2.tid; but this doesn't seem to make alot ...

mysql joins are driving me mad!

So I've asked a couple of questions about performing joins and have had great answers, but there's still something I'm completely stumped by. I have 3 tables. Let us call them table-b, table-d and table-e. Table-b and table-d share a column called p-id. Table-e and table-b share a column called ev-id. Table-e also has a column called da...

Understanding how JOIN works when 3 or more tables are involved. [SQL]

I wonder if anyone can help improve my understanding of JOINs in SQL. [If it is significant to the problem, I am thinking MS SQL Server specifically.] Take 3 tables A, B [A related to be by some A.AId], and C [B related to C by some B.BId] If I compose a query e.g SELECT * FROM A JOIN B ON A.AId = B.AId All good - I'm sweet with ho...

MySQL joins, how to output relation "the proper way"

Hello, First of all, excuse my poor topic title. I simply have no idea how to formulate this question or what to Google after, so don't shoot me if this is something easy answerable. Assume I have the following tables: [AUTHORS] id, name [NEWS] id, item, author_id If I wanted to display a news item and output the corresponding autho...

SQL where everything else

Consider two database tables, Reports and Reports_has_properties Reports ------ id_report 1 2 3 Reports_has_properties ---------------------- id_report property 1 red 1 big 2 orange 3 blue 3 tiny Problem: I only get only the reports with the property red, for example, but the query returns all ...

Grouping results under a heading

This is probably really obvious and simple - but it isn't to me with my rudimentary knowledge:) I've got two tables: table_items ----------- item_id ¦ item_name ¦ item_describtion ¦ more item stuff..... table_subitems ----------------- subitem_id ¦ item_id ¦ subitem_name ¦ More subitem stuff Each item can have zero to pretty much un...

How do I use ActiveRecord to find unrelated records?

I have a many-to-many relationship set up through a join model. Essentially, I allow people to express interests in activities. class Activity < ActiveRecord::Base has_many :personal_interests has_many :people, :through => :personal_interests end class Person < ActiveRecord::Base has_many :personal_interests has_many :activitie...

How to cross join two tables in SQL server

HI I have a table EMP_SCHED which contains COMMENTS Column for EMP_ID and another table SHIFT which also contains COMMENTS column for EMP_ID.Now for a given date range I need to pick COMMNETS from both tables . For example in I have 7 EMP_IDs in EMP_SCHED table and 2 EMPIDs in SHIFT .I need all the EMPIDS and COMMNETNS and place the co...

getting data from 2 different table with JOIN sql. Codeigniter

I use codeigniter, and I need to get data from 2 different table. for now it returns data only from works_image table. how can I get data from both 2 table ? thanks a lot!! $this->db->select('works_image.*', 'works.*'); $this->db->from('works_image', 'works'); $this->db->join('works', 'works.id = works_image.id_work'); $result = $thi...

Subqueries in linq

Hello, I've been trying for a couple of days to write to "translate" this query in LINQ with no success so far. Could you guys please help me? I would also appreciate some explanation to learn actually something out of it. Here is the T-SQL query: SELECT R.ResourceID, R.DefaultValue FROM Resources as R JOIN (SELECT [t0].[NameResou...

SQL to get messages only from people I follow.

Hi guys, I'm wondering how to create SQL query to get messages only from people I follow. I assume that create a SQL join query is the right way but I don't know how. Please could someone help me? I have these three tables now: CREATE TABLE `users` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `username` VARCHAR( 255 ) NOT NULL , `...