Jump to content

MAX() Issues


tbarsness

Recommended Posts

I have three tables defined here:

 

1.  item

+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id_item     | int(10) unsigned | NO   | PRI | NULL    | auto_increment | 
| name        | varchar(50)      | NO   |     |         |                | 
| description | text             | NO   |     |         |                | 
| image_name  | varchar(50)      | NO   |     |         |                | 
| active      | enum('0','1')    | NO   |     | 1       |                | 
+-------------+------------------+------+-----+---------+----------------+

 

2.  price

+----------+------------------+------+-----+---------+----------------+
| Field    | Type             | Null | Key | Default | Extra          |
+----------+------------------+------+-----+---------+----------------+
| id_price | int(10) unsigned | NO   | PRI | NULL    | auto_increment | 
| id_item  | int(10) unsigned | NO   |     |         |                | 
| min_qty  | int(10) unsigned | NO   |     |         |                | 
| price    | float(7,2)       | NO   |     |         |                | 
+----------+------------------+------+-----+---------+----------------+

 

3.  cart

+------------+------------------+------+-----+---------+----------------+
| Field      | Type             | Null | Key | Default | Extra          |
+------------+------------------+------+-----+---------+----------------+
| Field      | Type             | Null | Key | Default | Extra          |
+------------+------------------+------+-----+---------+----------------+
| id_cart    | int(10) unsigned | NO   | PRI | NULL    | auto_increment | 
| session_id | char(32)         | NO   |     |         |                | 
| id_item    | int(10) unsigned | NO   |     |         |                | 
| quantity   | int(10) unsigned | NO   |     |         |                | 
+------------+------------------+------+-----+---------+----------------+

 

I'm trying to write a query to join the three together, selecting the minimum `price`.`price` (and thus maximum `price`.`min_qty` that is less than `cart`.`quantity`).  I'm running into some problems. 

 

This query:

 

SELECT *, MAX(`min_qty`) AS `maxquantity` 
FROM `cart`,`item`,`price` 
WHERE `session_id` = 'cbf4d44213e5250ce3e444aff138313d' 
AND `cart`.`id_item` = `item`.`id_item` 
AND `cart`.`id_item` = `price`.`id_item` 
AND `price`.`min_qty` < `cart`.`quantity` 

GROUP BY `cart`.`id_cart`

Returns me everything I hoped it would, including a correct maxquantity.  The problem is the rest of the row is from an incorrect part of the quantity row.  How do I select data from the row that has this max quantity?  I've tried a couple of things with HAVING and haven't had any luck.  Do I have to write an inner query?  How would I go about doing that?

Link to comment
https://forums.phpfreaks.com/topic/106373-max-issues/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.