问题
Please check the images i want complete ROW of query-id "43"(and with another nic_id max query_id row) but it give me only max query_id, I tried different queries for-example:
SELECT `Query_id`, `nic_id`, `date`, `subject`, `followup_no`, MAX(Query_id) as queryid FROM `sales_queries` GROUP BY `nic_id` HAVING MAX(`Query_id`)
SELECT `Query_id`, `nic_id`, `date`, `subject`, `followup_no`, MAX(Query_id) as queryid FROM `sales_queries` WHERE GROUP BY `nic_id` HAVING MAX(`Query_id`)
SELECT `Query_id`, `nic_id`, `date`, `subject`, `followup_no`, MAX(Query_id) as mxqueryid FROM `sales_queries`WHERE `Query_id` = (SELECT MAX(`Query_id`) FROM `sales_queries`) GROUP BY `nic_id`
when customer visit again followup increase(+1) with his same nic_id with different subject.
i want complete 43 row with subject "Registration4", date etc
NOT THIS, it should be Registration4 date:2015-09-11
回答1:
Similarly to the answer to this question, you can use a subquery and join in order to fetch the results you are after. Something similar to the following should work:
SELECT `Query_id`, `nic_id`, `date`, `subject`, `followup_no`
FROM `sales_queries` AS sq
INNER JOIN (
SELECT MAX(`Query_id`) AS mId
FROM `sales_queries`
GROUP BY `nic_id`
) AS subsq ON subsq.mId = sq.Query_id
回答2:
i am using below three queries in three different pages. I want IF MAX(Query_id
) is not available specific range of dates then it ignore it.
///////////// QUERY 1 \\\\\\\\\\\\\\\\\\\\\
$sql="SELECT * FROM sales_queries
AS sq
INNER JOIN ( SELECT MAX(Query_id
) AS mId FROM sales_queries
WHERE follow_up
> '".$s_date."' GROUP BY nic_id
) AS subsq
ON subsq.mId = sq.Query_id";
///////////// QUERY 2 \\\\\\\\\\\
$sql="SELECT * FROM sales_queries
AS sq
INNER JOIN ( SELECT MAX(Query_id
) AS mId FROM sales_queries
WHERE follow_up
BETWEEN '".$s_date."' AND '".$e_date."' GROUP BY nic_id
) AS subsq
ON subsq.mId = sq.Query_id";
///////////// QUERY 3 \\\\\\\\\\\
$sql="SELECT * FROM sales_queries
AS sq
INNER JOIN ( SELECT MAX(Query_id
) AS mId FROM sales_queries
WHERE follow_up
BETWEEN '".$s_date."' AND '".$e_date."' GROUP BY nic_id
) AS subsq
ON subsq.mId = sq.Query_id";
来源:https://stackoverflow.com/questions/32517231/how-to-further-filter-group-by-record-in-mysql