473,396 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

query to find the closest result:K@N@!:

------=_NextPart_000_004F_01C352B1.E5B8FA20
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hi,

I'm working on a PHP based website that loads custom pricing for users =
where they logon.

I'm trying to write a query to find the custom price of an item.

Here is the table with all the prices in it.

mysql> DESCRIBE item_price;
+--------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+------------------+------+-----+---------+-------+
| majver | int(10) unsigned | | PRI | 0 | |
| minver | int(10) unsigned | | PRI | 0 | |
| iid | int(10) unsigned | | PRI | 0 | |
| price | float(6,2) | | | 0.00 | |
+--------+------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
majver and minver identify which price schedule to use. Each customer =
has a different majver minver combination.

iid is the key to the products table.

price is the price of the item.

The price schedule where majver=3D0 and minver=3D0 is called list price, =
and is the only schedule that has a price for every single item. All the =
other combinations of majver and minver make up an sub-list of items =
that sell at the same discount level. If there is no price for a majver, =
minver combo, an attempt is made to use the price where minver=3D0, =
otherwise list price is used.
To get the price of one item, I would do the following.

mysql> SELECT * FROM item_price WHERE iid=3D3 AND majver IN (0,1) AND =
minver IN (0,10) ORDER BY majver DESC, minver DESC;
+--------+--------+-----+--------+
| majver | minver | iid | price |
+--------+--------+-----+--------+
| 1 | 10 | 3 | 98.81 | <=3D first choice
| 1 | 0 | 3 | 91.21 | <=3D second choice
| 0 | 0 | 3 | 152.02 | <=3D last choice
+--------+--------+-----+--------+
3 rows in set (0.00 sec)

Sorting the table puts the most relevent price first. I grab this row =
and ignore the rest. This type of query should never have more than 3 =
rows since the tree structure of item_price is only 3 levels deep.

Most of the time I want to get information about more than one item at =
once. However this complicates things. I only want 1 row for each iid, =
specifically the row with the highest majver and minver for a given iid.

EXAMPLES:

mysql> SELECT * FROM item_price WHERE iid IN (3,4) AND majver IN (0,1) =
AND minver IN (0,10) ORDER BY majver DESC, minver DESC;
+--------+--------+-----+--------+
| majver | minver | iid | price |
+--------+--------+-----+--------+
| 1 | 10 | 3 | 98.81 |
| 1 | 0 | 3 | 91.21 | <=3D Need to eliminate these rows
| 0 | 0 | 3 | 152.02 | <=3D
| 0 | 0 | 4 | 49.29 |
+--------+--------+-----+--------+
4 rows in set (0.00 sec)

mysql> SELECT * FROM item_price WHERE iid IN (3,5) AND majver IN (0,1) =
AND minver IN (0,10) ORDER BY majver DESC, minver DESC;
+--------+--------+-----+--------+
| majver | minver | iid | price |
+--------+--------+-----+--------+
| 1 | 10 | 3 | 98.81 |
| 1 | 10 | 5 | 89.77 |
| 1 | 0 | 3 | 91.21 | <=3D Need to eliminate these rows
| 1 | 0 | 5 | 82.87 | <=3D
| 0 | 0 | 3 | 152.02 | <=3D
| 0 | 0 | 5 | 138.11 | <=3D
+--------+--------+-----+--------+
6 rows in set (0.00 sec)

mysql>
Is it possible to get a result set with 1 row for each iid specified, =
and the price where the majver and minver are the largest combination =
for each iid?

I would like to be able to do this with one query. Currently I've had to =
look up each price individually, but a page that loads prices for 100+ =
items creates a lot of overhead in running separate queries.

The webserver and mysql server are over 3000 miles apart, so bandwidth =
is kind of an issue, but processing power on the database server is not.

Any help would be appreciated.

Brian Newsham
Krackeler Scientific Inc.
be*@krackeler.com
518-462-4281 ext. 121
518-462-6011

------=_NextPart_000_004F_01C352B1.E5B8FA20--

Jul 19 '05 #1
0 1552

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jenn L | last post by:
I have a database that is pre-populated with sequential part numbers. As people reserve the parts I update a flag to show the # is no longer available. Now they want the ability to take out a...
13
by: mike | last post by:
I have ListArray with number in Eg: 1, 1.456, 2.43, 4, 6.78 next i have a decimal variable containing one number EG: 1.786 Could someone please tell me how i find the "closest match" number...
2
by: Joe | last post by:
Hello all! I need to display a list of names similar to a spell checker where the list is the closest match to the name entered. Not too sure where to begin with this - any suggestions? Thanks,...
2
by: sturgeob | last post by:
Yep. Newby. I am asked to find the closest value to user defined value if not right on the mark. I can get it to determin it I have selected one of the generated numbers, but am at a loss how to...
22
by: Steve Richter | last post by:
Does the .NET framework provide a class which will find the item in the collection with a key which is closest ( greater than or equal, less than or equal ) to the keys of the collection? ex:...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.