If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > MySQL join help needed.....

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-19-04, 12:11
sn202 sn202 is offline
Registered User
 
Join Date: Dec 2004
Posts: 2
Question MySQL join help needed.....

Hi,

I'm using PHP with a MySQL backend, basically this part of my system allows the user (student) to purchase items. this will then update the quantity of that product so something like:


Code:
$sql="update stock set quantity = quantity-'$quantity' where stkid = '$id'"

and then update the students new balance so:


Code:
$sql="update student set balance = balance-$price where stkid = $id"

Obviously this will not work and requires a join of some sort but am pretty new to MySQL and can't think how the join will work exactly...

Any help with this will be much appreciated,

Regards,

Simon.
Reply With Quote
  #2 (permalink)  
Old 12-19-04, 12:32
Vmusic Vmusic is offline
Registered User
 
Join Date: Dec 2004
Posts: 54
This might be a better answer

Simon,
I posted on the other site. I'm not clear on your design.

I also wanted to say, when you create your query, you have understand what you want back. Very often when you have a 'parent - child' relationship, you'll write a left join query that brings back a result set of both tables. You only display

For example, if you displayed ALL the orders student #49 has, and lets say they have three orders.

SELECT std.std_id, std.f_nm, std.l_nm, std.addr, std.bal, ord.ord_id, ord.std_id, ord_total FROM student std LEFT JOIN order ord on ord.std_id = std.std_id WHERE std.std_id ='49'


This type of query returns three rows, each of those rows has the exact same student id, student first namd, student last name (std_id, f_nm, l_nm). So when you loop through this you'll retrieve the student information once and each unique order this student has.

Hope that helps
Good Design, Saves Time
Reply With Quote
  #3 (permalink)  
Old 12-19-04, 17:07
sn202 sn202 is offline
Registered User
 
Join Date: Dec 2004
Posts: 2
Hi yeah sorry guess I didn't explain very well.

Ok, the purpose of this query is simply to update the students balance by taking the current balance and deducting the cost of the product. So we need to take the current "BALANCE" of the student (eg. studentid: 1) from table "STUDENTS" and deduct it by the "PRICE" of the product (eg. productid: 1) from table "PRODUCTS".

Table structure is shown below:

PRODUCTS.
productid, name, description, price, quantity.

STUDENTS.
studentid, name, userid, balance.


I have a seperate query which updates the quantity of the stock. And another seperate query which adds all the required details into table TRANSACTIONS. But these two I think i've sorted out.

Hope this helps, thanks for the response and any futher help will be much appreciated.
Reply With Quote
  #4 (permalink)  
Old 12-21-04, 08:23
matt_p matt_p is offline
Registered User
 
Join Date: Dec 2004
Location: Europe
Posts: 20
You're mixing PHP and SQL in your thoughts, I dare say ...
where does PHP-variable "$price" come from in the second query ?
Still then , there is no stockid in the students table - or is there ?

Try it this way:

$sql="update student set balance = balance -
(select price from stock where stkid = $id)"

<edit or better>

$sql="update student set balance = balance -
(select price from products where productid = $id)"



..should work if stkid is unique within stock resp. productid in products .. which I suppose it is ...

Last edited by matt_p; 12-21-04 at 12:19.
Reply With Quote
Reply

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On