Jump to content

[SOLVED] Using PHP to sort data in MySQL table 1 by values in table 2


ChambeRFienD

Recommended Posts

Well.. I have a MySQL database with 2 tables in it. products and prices.. Products has a field in it called 'prodno'. Prices has a field in it called 'prodid'.

 

There is one of each item in products and each product has it's own unique 'prodno'

 

There are multiple instances of each product's pricing in prices.. One for each price.. So if a product had multiple prices depending on quantity it would have an entry for each price.. Example:

 

id = 1

prodid = 7001

qty = 300

price = 12.5

 

id = 2

prodid = 7001

qty = 400

price = 15.5

 

I was wondering if I could query the database getting info from the products table but sort it according to the prices in the prices table.

 

Kinda like doing a "SELECT * FROM products" ordering it by the lowest price value from each item..

 

This is all very confusing to me, and I'm the one writing it. Let's try one last time..

 

Query the database selecting * from products (I plan on using all the info in products) and ordering them by the lowest price for each item in the prices table. I've tried sorting the results of just a basic SELECT * FROM products using Javascript and PHP but with pagination in the results it makes it a bit hard.

Link to comment
Share on other sites

Alrighty.. Figured it out myself..

 

$sql = "SELECT prices.price AS price, products.pname AS pname, products.prodno AS prodno FROM prices, products WHERE prices.prodid = products.prodno ORDER BY prices.price ASC LIMIT 20";

 

$query = mysql_query($sql);

 

while($row = mysql_fetch_array($query)) {

echo $row['price'] . " - " . $row['pname'] . " - " . $row['prodno'] . "<br />\n";

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.