sql - MySQL Group by and Having clause issue -
I am trying to get the correct record, but due to the reason I think any issue I think, Can anyone please help me?
Case 1: is trying to select the rows, where order_id = 1 (new order), but should not exceed 1 record with the same order ID Case 2: where order_id = 2 (printed order), but they should also choose new order and in other words, SEZ 1 should be applied, select the query Where order_id = 2 or where order_id = 1 if (order_id = 1 then records more than 1 with the same order ID I have a table where: This is my simple mySQL query: I have also created SQL Bella for reference, please check whether I am doing wrong or do I need a complex question with case or IF statement? If this link is not working, please create one with this code: Here's the inclusion: Your earliest response should be appreciated! Thanks for the time. Desire results: Case: 1 which is quite simple, where the result should be something like: only more than 1 New orders with records Case 2 results: It looks like reading between the lines of your question, that You want to display the highest numerical value of the position for each order ID, that is, it seems that the status of your order is moving from 1 to 2 to 3 and further. Here's how you do that. First, you determine that the highest position for each order is as follows: This query gives you a line for each ID, which shows the maximum value for the status ID. Then, you use it as a subqueries and thus join your original table. This will give you a good result with the highest position for each order. Please note : If you enter your
order_id = ID of the order - status_id = different status IDs like 1 = new, 2 = printed, 3 = processing etc ... status_change_by = The administrator's ID that changes the status of the order to print from new to processing ...
order_id | Condition_id | Position_change_by 1 1 | (NULL) 1. 2 | 12 1 3 | 12 2 1 | (NULL) 3 | 1 | (NULL) 4 | 1 | (Faucet) 1 | 4 | 13 5 1 | (Faucet) 3 | 2 | (Zero)
create table order_tracking (track_id int auto_invironment primary Key, order_id int (50), static int (20) position_change_by varchar (30));
Include order_traking (order_id, status_id, status_world_b) value (1,1 ',' '), (1,2) , 12), (1,3, 12), (2,1, ''), (3,1, ''), (4,1, ''), (1,4, 13), (5, 1, ''), (3, 2, '');
Order_id | Condition_id | Position_change_by 2 | 1 | (NULL) 4 | 1 | (Faucet) 3 | 1 | (NULL)
Order_id | Condition_id | Position_change_by 1 4 (maximum id). (NULL 2) 1 | (NULL) 4 | 1 | (NULL) 3 | 2 (max id). (NULL)
SELECT MAX (status_id) AS status_id, order_tracking from order_id by order GROUP order_id
SELECT o.order_id, o.status_id, o.status_changed_by command_hack as tracking (select MAX (status_id) AS status_id, order_id from order_id order_id by group) Oorder_id = M.order_id and o.status_id = m.status_id by order o.order_id
| ORDER_ID | STATUS_ID | STATUS_CHANGED_BY | | ---------- | ----------- | ------------------- | | 1 | 4 | 13. | 2 | 1 | | | 3 | 2 | | | 4 | 1 | | | 5 | 1 | |
ID column in your
order_tracking table, then Things can work better for you You will be able to get the most recent
INSERT ad status for each
order_id instead of the highest statistically highest position . This will be very useful changes in my opinion in your table layout. You want to do this:
SELECT o.order_id, o.status_id, o.status_changed_by order_tracking object (id as option max, order_id by order_id order_tracking group) ASM Oi id = mid order oder_id
Comments
Post a Comment