0

I am working on a php page and have a table named

computer_complaint

all i want is to fetch data from this table by the latest complaint made by user. when user files any complaint it stores in this table which gives him a unique

computer_id

and

problem_date

is column which enters the date with time when user made complaint. and same user can made complaint more than once

now i want a query by which i can able to display the latest details of that user's complaint so i need to sort the table on the last problem_date on that computer_id. My current query is

select *,date_format(cc.problem_date,'%d/%m/%Y')as problem_date,ifnull(cc.attended_on,'Pending')as com_status from computer_complaint cc,computer_master cm,computer_st_complaint_mode ccm,computer_company_master cmy WHERE cc.`status`='Active' and cm.`status`='Active' and ccm.`status`='Active' and cmy.`status`='Active' and cc.computer_id=cm.computer_id and cc.method=ccm.comp_mod_id  and cm.company_id=cmy.company_id

but it is showing all the records of that particular id while i only want the latest one Thus i want all computer_id(s) to show only the latest filed complaint not to show all the complaints for each id as this make my display screen wired.

Thanks in advance.

1 Answer 1

1

To fetch the last one of each cc.computer_id, you can use this:

select *,date_format(cc.problem_date,'%d/%m/%Y')as problem_date,ifnull(cc.attended_on,'Pending')as com_status from computer_complaint cc,computer_master cm,computer_st_complaint_mode ccm,computer_company_master cmy WHERE cc.`status`='Active' and cm.`status`='Active' and ccm.`status`='Active' and cmy.`status`='Active' and cc.computer_id=cm.computer_id and cc.method=ccm.comp_mod_id  and cm.company_id=cmy.company_id ORDER BY cc.problem_date DESC GROUP BY cc.computer_id

That will give you the latest entry, according to cc.problem_date!

Sign up to request clarification or add additional context in comments.

3 Comments

thanks Lars but this query only shows the last record in table i want the last record of each computer_id not only the last entered record in table.
Please don't say sorry you tried to help me and really thanks for that.
this is giving the following error "Error Msg:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'GROUP BY cc.computer_id' at line 1"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.