TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-22-2011, 08:29 PM   #1 (permalink)
The Wanderer
 
Join Date: Jan 2010
Posts: 13
Thanks: 0
pepelepew1962 is on a distinguished road
Default Duplicating mysql information on SUM ...

Hello:

I am having problems getting information from 2 tables and think this is a mysql issue instead of php. I have a MAIN table that lists computer models and another PART table that has it's components. My query result needs to be, list the model, manuf and type ( main ) and all it's components (parts ), including the unit price, the max unit price, the min unit price and the average unit price. My primary focus is with the PART table. The result will list the same model and manuf along with all the parts and prices. Maybe an example would help:
//
$ritelist2101 = "SELECT tblMain.tblMain_Main00, tblMain.tblMain_Main05, tblMain.tblMain_Main06, tblPart.tblPart_Part00, tblPart.tblPart_Part01, tblPart.tblPart_Part02 FROM tblMain, tblPart WHERE (tblMain.tblMain_Main00 = tblPart.tblPart_Part00) ORDER BY tblMain_Main05, tblMain_Main00";
$ritelist2102 = mysql_query($ritelist2101);
//
Sample Data:
Main00=125
Main05=Sony
Main06=Desktop
//
Main00=136
Main05=Sony
Main06=Desktop
//
Main00=142
Main05=Dell
Main06=Laptop
//
//
Part00=125
Part01=Drive
Part02=500.00
//
Part00=125
Part01=Video
Part02=100.00
//
Part00=125
Part01=Keyboard
Part02=63.00
//
//
Part00=136
Part01=Drive
Part02=450.00
//
Part00=136
Part01=Video
Part02=300.00
//
RESULT: (Manu-Model-Part-Price-Max-Min-Avg)
Sony-125-DRIVE-500-500-63-221
Sony-125-VIDEO-100-500-63-221
Sony-125-KEYBOARD-63-500-63-221
Sony-136-DRIVE-450-450-300-375
Sony-136-VIDEO-300-450-300-375
etc...
//
I fully understand that a lot of information is duplicated, but it is not my place to ask, but to do. Supposedly more calculations will be made, but first things first. Also, I need to read the data with:

$Main00 = $row['tblMain_Main00']; // Main Model
$Main05 = $row['tblMain_Main05']; // Manufacturer
$Main06 = $row['tblMain_Main06']; // Type
$Part00 = $row['tblPart_Part00']; // Part Model

Can anyone please help me on this.
pepelepew1962 is offline  
Reply With Quote
Old 05-22-2011, 09:02 PM   #2 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

I think what you need to do explicitly make an inner join query something like
sql Code:
SELECT tblMain.tblMain_Main00, tblMain.tblMain_Main05, tblMain.tblMain_Main06, tblPart.tblPart_Part00, tblPart.tblPart_Part01, tblPart.tblPart_Part02
FROM tblPart
INNER JOIN tblMain ON tblMain.tblMain_Main00 = tblPart.tblPart_Part00
ORDER BY tblMain.tblMain_Main05, tblMain.tblMain_Main00

P.S. Just an advice but I find the prefix tbl and the double Part_Part naming a bit redundant and fills up with clutter, just a personal note.
tony is offline  
Reply With Quote
Old 05-26-2011, 05:34 PM   #3 (permalink)
The Acquainted
 
wGEric's Avatar
 
Join Date: Nov 2007
Posts: 166
Thanks: 0
wGEric is on a distinguished road
Default

Sounds like a one-to-many relationship so you would need to do a left or right join. Inner join is used for a one-to-one relationship.

If you want to do SUM and other functions like that then you'll need a group by.

I agree with Tony. Name your columns something useful so that in the future it won't take a manual to figure out what data is in each column.
__________________
Eric
wGEric is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
exporting mysql db to excel sarmenhb Absolute Beginners 8 06-01-2008 06:07 PM
MySQL Sell Up Alan @ CIT The Lounge 12 01-17-2008 05:46 PM
Error in connecting to MySQL via PHP EyeDentify MySQL & Databases 0 01-03-2008 01:06 PM
Notepage like application to open large MySQL files Wildhoney General 6 12-07-2007 02:18 PM


All times are GMT. The time now is 01:37 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design