MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 8th, 2004, 10:57 AM
slipnfall slipnfall is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 1 slipnfall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question [MySQL] Search for PrimID across multiple tbls

Hi there,
I'v been working with PHP and MySQL for roughly 6 months now and am having trouble with searching our DB. FYI, this is a parts inventory system. There are 8 tables, each containing different types of parts. In each of these tables are fields such as part number, qty, price, etc, etc. The part location is the primary ID, and is a common field to all tables. I have setup a working search within each of these tables(or part category), but not a 'global' search.

This 'global' search is actually a barcode scanner input textbox. I would like to search, across all of the 8 categories[tables] for the Primary ID(location). I have read about using the JOIN statement, but this seems to only be useful for tables which have associated data. Ideally, I could query like: "SELECT * FROM SEMI_Diodes, SEMI_Regulators, Resistors WHERE Location LIKE 'B5-45'" Of course this doesn't work.

Although a bit dirty, I have considered getting a list of all the tables in the DB, then querying each one, until I returned a non-null result set. Any ideas on how to achieve the above simply without getting burried in ugly code?

Any input greatly appreciated,
-Jamie

Reply With Quote
  #2  
Old March 10th, 2004, 03:08 PM
merliin's Avatar
merliin merliin is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: Melbourne, Australia
Posts: 30 merliin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
I suspect that the process of normalisation is news to you.
You should have one table for all parts (this way you also avoid duplicate id's) then the part type should be a field.

Let me have a rough guess here (might not be 100% compliant mysql code).
Code:
create table parts (
id longint autoincrement,
barcode varchar notnull,
name varchar notnull,
constraint part_pk primary key(id)
);

Next we add our inventory:
Code:
create table inventory (
part_id longint notnull,
location varchar notnull,
quantity int,
price money not null,
constraint inventory_pk primary key (part_id,location),
constraint inventory_part_id_fk foreign key part on (id)
);

Now to your code:
Code:
select location
from inventory
inner join part on part.id = inventory.part_id
where part.barcode = $barcode <-- Your variable here
and part.id = inventory.part_id;


Excuse my bad sql, but I hardly ever work with MySQL.
Had you not been using mysql you cold even have done it with subqueries, but you should probably redesign your database to comply with 3rd level noramlisation to save yourself from grief and headaches later on.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > [MySQL] Search for PrimID across multiple tbls


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT