My purpose:
select entries with has_t
"1"
whenever it is there, if its not there, select entry with has_t
"2"
.
All remaining entries for the relevant visit, should go inside "more_items" column
The issue:
The current code is first selecting the has_t
"2"
, instead of has_t
"1"
SELECT iu.*,`t`.`o_id`,
v.GID,v.MSID,
GROUP_CONCAT(DISTINCT CONCAT(iu.item_no,'|##|',t.s_amount,'|##|',t.e_item_ordered,'|##|',IFNULL(t.e_item_name,'No-Name-In-T-TBL'),'|##|',IFNULL(t.com,''),'|##|',IFNULL(t.post_s_e,''),'|##|',IFNULL(t.o_id,'')) SEPARATOR '|#|') AS MoreItems,
t.post_s_e,t.e_item_name,t.click_time
FROM `items_unique` iu
LEFT JOIN visits v ON v.id = iu.visit_id
LEFT JOIN trans t ON t.visit_id = iu.visit_id AND t.e_item_ordered = iu.e_item_ordered
WHERE iu.product_id = $PID AND has_t > 0 AND (t.com >= 10 OR has_t = 1)
GROUP BY `v`.`id` ORDER BY v.visited_at DESC LIMIT 10
Note: Previous code version seemed to have done it using non-compliant SQL sub-query-order, which anyway doesn't work on MariaDB:
ORDER BY `iu`.`has_t` LIMIT 18446744073709551615