473,387 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

getting data across 3 tables

32
I am trying to get data from 2 tables, however there is a 3rd reference table, and I am having trouble with the query.


Expand|Select|Wrap|Line Numbers
  1. SELECT 'parts.number', 'printers.name'
  2.      FROM parts 
  3.      UNION printers ON printer_id = 'user_input' 
  4.      JOIN parts_printer_consumables ppc ON printer_id = 'user_input' 
  5.      WHERE printers.printer_id = 'user_input' AND parts.part_id = 'ppc.part_id'
  6.  
I dont think that is the the right query though

The input is a given printer (integer)
grabs the part id and printer_id from ppc
gets part info from parts table
gets printer info from printers table

so its a join across 3 tables? :-? thanks for the help
Sep 5 '07 #1
6 1990
pbmods
5,821 Expert 4TB
Heya, jgentes.

Instead of 'UNION', try using 'JOIN'.

UNION is for appending the results of one or more queries onto the end of the first one, vertically.
Sep 5 '07 #2
jgentes
32
okay i tried that, and still giving me an error.
ERROR 1054 unknown column 'parts_printer_consumable.printer_id' in the where clause.

Expand|Select|Wrap|Line Numbers
  1. SELECT parts.number, printers.name FROM printers
  2.           JOIN printers ON printers.id = parts_printer_consumable.printer_id
  3.          WHERE parts_printer_consumables.printer_id = '19'
  4.                AND parts.part_id = parts_printer_consumable.consumable_id
  5.  
where 19 = user-input
Sep 6 '07 #3
mwasif
802 Expert 512MB
You have not mentioned your 2nd table name parts_printer_consumables in your JOIN statement.
Sep 6 '07 #4
jgentes
32
okay, how would i join it? because the only 2 fields are the printer_id and the consumable_id ? this table was intended to be reference only

okay i made an adjustment and got another error

Expand|Select|Wrap|Line Numbers
  1. SELECT parts.number, printers.name FROM parts
  2.      JOIN parts_printer_consumable ppc ON ppc.printer_id = '19' 
  3.      JOIN printers ON printers.id = ppc.printer_id
  4.      WHERE ppc.printer_id = '19' 
  5.              AND parts.part_id = ppc.consumable_id
  6.  
I now have the error

unknown column 'ppc.printer_id' in ON clause

ON A SIDE NOTE... what is the notation for the mysql code? i tried a number of things but couldn't figure it out, and i see that someone edited my first post to abide by the correct notation
Sep 6 '07 #5
pbmods
5,821 Expert 4TB
Heya, Jgentes.

Now then.

Think about it like this: When you JOIN tables, you're creating one big table out of those multiple tables and searching that, instead of individual tables. That's not exactly how it works under the hood, but it's close enough.

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         `parts`.`number`,
  3.         `printers`.`name`
  4.     FROM
  5.     (
  6.             `parts_printer_consumable`
  7.                 AS `ppc`
  8.         LEFT JOIN
  9.             `parts`
  10.                 ON
  11.                     `parts`.`part_id` = `ppc`.`consumable_id`
  12.         LEFT JOIN
  13.             `printers`
  14.                 ON
  15.                     `printers`.`id` = `ppc`.`printer_id`
  16.     )
  17.     WHERE
  18.         `ppc`.`printer_id` = '19'
  19.  
I changed your JOINs to LEFT JOINs and switched the order so that you will only get results if there are results in the `ppc` table.

Aside from being easier to look at, this setup makes more sense. What you are doing is taking all of the results from `ppc` and appending rows from `parts` where the IDs match up, then appending rows from `printers` where those IDs match up and then filter the whole thing by `printer_id`.

Note that because `ppc` is the origin table for the JOIN, MySQL is smart enough to evaluate the WHERE clause before joining the other tables.

PS, you're looking for [code=mysql].
Sep 6 '07 #6
jgentes
32
thank you pb, that worked.

and ahh.. i was using [code-mysql] and [mysql] and [sql]
ill have to remember that.

and whats the difference between just a join and a left join?
is it just a matter of "proper" coding techniques?
Sep 6 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: DebbieD | last post by:
Greetings, This seems a very complicated task, so I welcome any input. My boss wants a data grid or matrix of the top 6 orders with ordered items, and products, for a particular customer and...
11
by: deko | last post by:
If I release a new version of my mbd (in mde format) and users want to upgrade - how do they migrate their data? For example, if the original was released as data1.mde and then I release...
0
by: Tony Epton | last post by:
I am not quite a newbie in the area of SQL server - more "knows just enough to be dangerous" - so please be gentle with me. Cross posted to several groups - apologies if too far off topic I...
5
by: Kevin C | last post by:
I was curious to know what some developers out in the industry are doing when it comes to exposing Data access logic, specifically persistence. This is assuming that your not using an O/R framework...
4
by: Steve | last post by:
Hi, I've been running postgres on my server for over a year now and the tables have become huge. I have 3 tables that have data over 10GB each and these tables are read very very frequently. In...
4
by: Sean Shanny | last post by:
To all, Running into an out of memory error on our data warehouse server. This occurs only with our data from the 'September' section of a large fact table. The exact same query running over...
4
by: HLCruz via AccessMonster.com | last post by:
I am working with a database that has client information separated in to 4 related tables - tFolder, tAddress, tEmail, tPhone number. In addition there are related tables tGifts and tCalls. The...
0
by: Chuck W1CEW | last post by:
So we have a MySQL database that suddenly started reporting "can't open *.ibd file" errors on InnoDB tables in phpMyAdmin. (MyISAM are fine). To recover the data is all I want at this point -- so...
10
by: nflacco | last post by:
I'm tinkering around with a data collection system, and have come up with a very hackish way to store my data- for reference, I'm anticipating collecting at least 100 million different dataId...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.