這是簡化版我正在做的事情。MySQL:可以/我的MIN()和MAX()查詢結合起來嗎?
A)
SELECT id, foreign_key_id, type, quantity, MAX(price) AS price // <--- Max
FROM quotes
WHERE type = 'tier 1' // <--- Equal
AND prod_id_requested = 12345
B)
SELECT id, foreign_key_id, type, quantity, MAX(price) AS price // <--- Max
FROM quotes
WHERE type != 'tier 1' // <--- NOT equal
AND prod_id_requested = 12345
C)
SELECT id, foreign_key_id, type, quantity, MIN(price) AS price // <--- Min
FROM quotes
WHERE type = 'tier 1' // <--- Equal
AND prod_id_requested = 12345
d)
SELECT id, foreign_key_id, type, quantity, MIN(price) AS price // <--- Min
FROM quotes
WHERE type != 'tier 1' // <--- NOT equal
AND prod_id_requested = 12345
有沒有一種好方法可以保存一些資源,並且可以將這些資源合併爲更少的查詢?
或者這是相當多的方式來做到這一點?
[UPDATE]
下面是一些樣本數據一起玩:
CREATE TABLE IF NOT EXISTS `quote_item` (
`id` int(2) unsigned NOT NULL AUTO_INCREMENT,
`quote_id` int(2) unsigned NOT NULL,
`product_id_quoted` int(3) unsigned NOT NULL,
`product_id_requested` int(3) unsigned NOT NULL,
`type` varchar(52) DEFAULT NULL,
`price` float(10,4) NOT NULL,
`quantity` int(9) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
INSERT INTO `quote_item` (`id`, `quote_id`, `product_id_quoted`, `product_id_requested`, `type`, `price`, `quantity`) VALUES
(1, 1, 12, 12, 'tier 1', 0.0100, 100),
(2, 2, 1, 12, 'tier 3', 0.0038, 8200),
(3, 2, 13, 12, 'tier 2', 0.0041, 10000),
(4, 3, 7, 14, 'tier 1', 0.0060, 25000),
(5, 3, 8, 12, NULL, 0.0180, 250),
(6, 3, 9, 15, NULL, 0.0019, 5000),
(7, 4, 12, 12, NULL, 0.0088, 7500),
(8, 4, 16, 12, 'tier 1', 0.0040, 10000),
(9, 5, 12, 9, 'tier 2', 0.0089, 1200),
(10, 5, 12, 12, 'tier 1', 0.0072, 6400);
最終,我們希望所有的數據很好地收拾到一個數組。
目前,我們正在從每個查詢得到一個行(不分組),我們有實際ID,數量,等 ...爲單行的最小值,以及相同的信息對於具有最大價格的單行,按「類型」類別,然後構建數組。
例如:
$flash_report = array(
'tier1' => array(
'qty' => array(
'min' => array(
'id' => 1
,'quote_id' => 1
,'price' => 0.01
,'quantity' => 100
)
,'max' => array(
'id' => 8
,'quote_id' => 4
,'price' => 0.004
,'quantity' => 10000
)
,'sum' => array(
'value' => 16500
,'count' => 3
)
)
,'price' => array(
'min' => array(
'id' => 8
,'quote_id' => 4
,'price' => 0.004
,'quantity' => 10000
)
,'max' => array(
'id' => 1
,'quote_id' => 1
,'price' => 0.01
,'quantity' => 100
)
)
)
,'other' => array(
'qty' => array(
'min' => array(
'id' => 5
,'quote_id' => 3
,'price' => 0.018
,'quantity' => 250
)
,'max' => array(
'id' => 3
,'quote_id' => 2
,'price' => 0.0041
,'quantity' => 10000
)
,'sum' => array(
'value' => 25950
,'count' => 4
)
)
,'price' => array(
'min' => array(
'id' => 2
,'quote_id' => 2
,'price' => 0.0038
,'quantity' => 8200
)
,'max' => array(
'id' => 5
,'quote_id' => 3
,'price' => 0.018
,'quantity' => 250
)
)
)
)
[/UPDATE]
現在,我是從我們個人的查詢得到的所有數據,然後將其全部組裝成一個大醇」陣列。
我想有了是一個更好的方式來做到這一點:)
授予查詢簡化/ obfusicated,但像我這樣的人將專注於缺乏'GROUP BY'這意味着結果可能是任意的,因爲[MySQL的GROUP BY支持](http://dev.mysql.com/ DOC/refman/5.0/EN /組逐隱藏columns.html)。 –
@ omg-ponies謝謝。這個問題現在已經更新了更多的信息,以更好地闡明我想要達到的目標。 – mOrloff
順便說一句,我把所需的結果作爲一個數組。不僅因爲這是我計劃編制數據的方式,還因爲我認爲這是最容易理解的。 ...我希望對其他人也是如此:) – mOrloff