我想獲得正確的記錄,但由於我有一些問題在我有條款我想,任何人都可以請幫我嗎?MySQL集團通過和有條款問題
案例1: 試圖選擇行,其中的order_id = 1(新訂單),但不應該有超過1個記錄以相同的順序編號
案例2: 選擇行,其中的order_id = 2(打印訂單),但也應選擇新訂單並應用CASE 1,換句話說,查詢應選擇order_id = 2或where order_id = 1 if(order_id = 1,那麼不應該有多個具有相同訂單ID的記錄)
我有一張桌子,其中:
order_id =訂單ID
status_id =不同的狀態ID例如1 =新的,2 =打印的,3 =處理等...
status_change_by =將訂單狀態從新改變爲打印到處理的管理員的id。 ..
order_id | status_id | status_change_by
1 | 1 | (NULL)
1 | 2 | 12
1 | 3 | 12
2 | 1 | (NULL)
3 | 1 | (NULL)
4 | 1 | (NULL)
1 | 4 | 13
5 | 1 | (NULL)
3 | 2 | (NULL)
這裏是我的簡單的MySQL查詢:
SELECT * from order_tracking
where status_id = 1
group by order_id
having count(order_id) <= 2;
我甚至創建的SQL搗鼓參考,請檢查,如果我做錯了或者我需要複雜的查詢使用CASE或IF語句?
http://sqlfiddle.com/#!2/16936/3
如果此鏈接不起作用,請創建一個由這樣的代碼:
CREATE TABLE order_tracking
(
track_id int auto_increment primary key,
order_id int (50),
status_id int(20),
status_changed_by varchar(30)
);
這裏是插入:
INSERT INTO order_tracking
(order_id, status_id, status_changed_by)
VALUES
(1,1,''),
(1,2,12),
(1,3,12),
(2,1,''),
(3,1,''),
(4,1,''),
(1,4,13),
(5,1,''),
(3,2,'');
你的最早的響應應該理解! 謝謝你的時間。
慾望結果:
案例:1這是很簡單的,其中的結果應該是這樣的:只有新的不超過1記錄
Order_id | status_id | status_changed_by
2 | 1 | (NULL)
4 | 1 | (NULL)
3 | 1 | (NULL)
案例2結果訂單:
Order_id | status_id | status_changed_by
1 | 4(max id)| (NULL)
2 | 1 | (NULL)
4 | 1 | (NULL)
3 | 2(max id)| (NULL)
請根據您的樣品數據提供所需的解決方案。 – 2014-10-08 22:11:01
請將你想要的**結果添加到你的文章(使用'edit'選項)。 – 2014-10-08 22:14:55
更新,再次檢查。 – Nadeem 2014-10-08 22:18:55