473,471 Members | 4,648 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Multi Table Query Help

I'm trying to do a multi table query that displays all the info I need
in one array, and I'm having problems figuring out how to do it.

The two tables are product and vendor. A vendor can be a manufacturer,
distributor, or both. This is indicated via the manufacturer_YN and
distributor_YN fields in the vendor table, and vendor_id is an
auto_number field. In the product table, there is a manufacturer_id and
a distributor_id field, and these are linked to the vendor.vendor_id
field.

What I would like to do is for each product, display the name, short
description (both fields in product table), manufacturer, and
distributor. The problem I'm having is writing a query to get all this
info at once so it can be displayed. I have a feeling it can be done
with a subquery, but haven't worked with those yet. Any help would be
appreciated.

Thanks.

Steve
Jul 17 '05 #1
5 1956
hi steve,
i wanna to display your tables first
product table:
------------------------------------------------------------------------------------------------------------------
prod_id | prod_name | prod_price | manufacturer_id | distributor_id |
-----so.on.....
------------------------------------------------------------------------------------------------------------------
vendor table :
------------------------------------------------------------------------------------------------------------------
vendor_id | vendor_name | manufacturer_YN | distributor_YN |
-----so.on.....
------------------------------------------------------------------------------------------------------------------
if is it ok lets go
--------------------------------
actully it will take time to make query (not more long) but if you use
MySQL you cannot use subquery cuz it not supportted (may be in the
MySQL 4.1.7 or 4.1.8) but i can give you new idea

instead of using 2 fileds in vendor manufacturer_YN , distributor_YN
you can make them 1 field say (vendor_activites) by this way you will
get more normalization and it will be more easy to make your query ,
and then remove manufacturer_id, distributor_id , and make it
vendor_id, i hence that your query may be somthing like this

mysql_query("SELECT prod_id,-------other fields------------ FROM
prodcut,vendor WHERE product.vendor_id=vendor.vendor_id");
i, hope it work. see u.

Jul 17 '05 #2
Steve wrote:
The two tables are product and vendor. A vendor can be a manufacturer,
distributor, or both. This is indicated via the manufacturer_YN and
distributor_YN fields in the vendor table, and vendor_id is an
auto_number field. In the product table, there is a manufacturer_id and
a distributor_id field, and these are linked to the vendor.vendor_id
field.

What I would like to do is for each product, display the name, short
description (both fields in product table), manufacturer, and
distributor. The problem I'm having is writing a query to get all this
info at once so it can be displayed.


Please post your tables definition (use the output of the DESCRIBE
command).

Just guessing I'd think something like this works:

SELECT p.name,
p.short_description,
v1.name as manufacturer,
v2.name as distributor
FROM product p,
vendor v1,
vendor v2
WHERE p.manufacturer_id=v1.id
AND p.distributor_id=v2.id
AND v1.manufacturer_YN='yes'
AND v2.distributor_YN='yes'
I guess (I'm pretty sure) the JOIN syntax would be better, but I never
liked that syntax and can't write a JOIN query without a lot of editing
out errors before it works the way I want :-)

--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Jul 17 '05 #3
lig
Sounds like your trying to do a join. To learn about them try going to
http://sqlcourse2.com/joins.html , or google for "SQL joins".
References:
http://dev.mysql.com/doc/mysql/en/JOIN.html

Jul 17 '05 #4

"Pedro Graca" <he****@dodgeit.com> wrote in message
news:sl*******************@ID-203069.user.uni-berlin.de...
Steve wrote:
The two tables are product and vendor. A vendor can be a manufacturer,
distributor, or both. This is indicated via the manufacturer_YN and
distributor_YN fields in the vendor table, and vendor_id is an
auto_number field. In the product table, there is a manufacturer_id and
a distributor_id field, and these are linked to the vendor.vendor_id
field.

What I would like to do is for each product, display the name, short
description (both fields in product table), manufacturer, and
distributor. The problem I'm having is writing a query to get all this
info at once so it can be displayed.


Please post your tables definition (use the output of the DESCRIBE
command).


mysql> describe vendor
-> ;
+-------------------+-----------------------------
---+
Field | Type
|
-----------------+---------------
----------+
vendor_id | mediumint(5) u
increment |
name | varchar(50)
|
address | varchar(30)
|
city | varchar(20)
|
state | char(2)
|
zip | mediumint(5)
|
phone | varchar(10)
|
fax | varchar(10)
|
email | varchar(50)
|
url | varchar(80)
|
contact_name | varchar(30)
|
acct_num | varchar(22)
|
distributor_YN | enum('Y','N')
|
manufacturer_YN | enum('Y','N')
mysql> describe product
-> ;
+-------------------+-----------------------------
---+
| Field | Type
ra |
+-------------------+-----------------------------
---+
| product_sku | bigint(9) unsigned zerofill
|
| subcategory2 | varchar(30)
|
| product_name | varchar(50)
|
| short_description | varchar(80)
|
| long_description | text
|
| picture_name | varchar(16)
|
| manufacturer_id | mediumint(5) unsigned
|
| distributor_id | mediumint(5) unsigned

Thanks.

Steve
Jul 17 '05 #5
Steve Edwards wrote:

"Pedro Graca" <he****@dodgeit.com> wrote in message
news:sl*******************@ID-203069.user.uni-berlin.de...
Please post your tables definition (use the output of the DESCRIBE
command).
mysql> describe vendor
-> ;

<snip badly formatted table description>
mysql> describe product
-> ;

<snip>

NEW VERSION OLD VERSION

SELECT p.product_name, -- p.name
p.short_description, --
v1.name as manufacturer, --
v2.name as distributor --
FROM product p, --
vendor v1, --
vendor v2 --
WHERE p.manufacturer_id=v1.vendor_id -- ...=v1.id
AND p.distributor_id=v2.vendor_id -- ...=v2.id
AND v1.manufacturer_YN='Y' -- ...='yes'
AND v2.distributor_YN='Y' -- ...='yes'

I didn't guess too far off the first time :-)
Not many changes between the two versions.
I think you should have managed by yourself.

To try it, you can copy it as is, including the -- comments.
Happy Coding :-)

--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Vic | last post by:
Hi all, When I test the Delete multi table function in MySQL, DELETE table_name ...] FROM table-references I accidentally delete all data in one table. All data in that table are gone...
5
by: David Logan | last post by:
Hello, I am trying to construct a query across 5 tables but primarily 3 tables. Plan, Provider, ProviderLocation are the three primary tables the other tables are lookup tables for values the...
1
by: Joe | last post by:
I have the following 3 tables: Clients, which has a numeric PK field called CLIENT_ID Languages, which has a numeric PK field called LANGUAGE_ID Client_Languages, which has a unique PK and...
6
by: Joe | last post by:
I have 2 multi-list boxes, 1 displays course categories based on a table called CATEGORIES. This table has 2 fields CATEGORY_ID, CATEGORY_NAME The other multi-list box displays courses based on...
3
by: syounger | last post by:
Hi. I have a report in Access 2000 that is based on selection made from a series of interdependent list boxes. The boxes I have right now are Source, Table, Column, Date. The user chooses Source...
1
by: Tim Marshall | last post by:
A2003. I am getting this error message when I try to set a report's recordsource to an SQL statement or a saved querydef that uses sub-queries. I've debug.printed the SQL, and run it as a stand...
4
by: Andy_Khosravi | last post by:
I'm trying to build a search utility for users to find 'inquiries' in my database that involves several tables. This is normally easy to do with the query builder, but I have a unique situation...
7
by: 663scott | last post by:
Hi I am pretty new to ACCESS. I have created some small databases previously. I need to run a simple query searching for a USERNAME which will gather information from five to ten tables containing...
1
by: KrazyKasper | last post by:
Access 2003 – Multi-Column List Box – Select Multiple Items I have a multi-column (3 columns) list box that works well to select one set of records or all sets of records (based on the first field...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.