Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi ,

I am trying to achieve so that latest one will be at top , I have Union query which merge two table , now order by descneding on primary key will not work as both might have differnt numbers and whatver comes next doesnt needs to be higher one , as they have differnt data count ,

below is what i was trying


C#
var query = " SELECT * FROM ( " +
            " SELECT '1' AS Type, c.StartDate, ifnull(cu.FirstName,'') AS FirstName, ifnull(cu.LastName,'') AS LastName,c.ClientComBox, c.DeviceCallID AS CallID " +
            " FROM Call c " +
            " LEFT OUTER JOIN  Customer cu ON c.CustomerID = cu.CustomerID " +
            
            " UNION " +
            " SELECT '2' AS Type, c.StartDate, ifnull(cu.FirstName,'') AS FirstName, ifnull(cu.LastName,'') AS LastName,c.ClientComBox, c.DeviceChemistCallID AS CallID " +
            " FROM ChemistCall c  " +
            " LEFT OUTER JOIN  Customer cu ON c.CustomerID = cu.CustomerID " +
            
            " ) c  WHERE 1 = 1";
         
            if (searchText != null && searchText != undefined && searchText.length > 0) {
                query += " AND (FirstName like '%" + searchText + "%'" +
                            " OR LastName like '%" + searchText + "%' " +
                            " OR ClientComBox like '%" + searchText + "%') ";
            }

            query += " ORDER BY c.CallID DESC LIMIT ? , ?";


Please suggest , so that whicver is latest will come at top
Posted
Comments
_Asif_ 29-Jan-16 2:02am    
what do you mean by "latest" one here?
Torakami 29-Jan-16 2:49am    
latest one means , wchiever goes last should be at top of the table , if I have one table then i can easily achive by using order by descending on primary table , but here , if my second table has got old but many records as compare to first table , no matter even if i add first tables records it will not at top , as second tables id who has large number than frst table will come first
_Asif_ 29-Jan-16 3:04am    
You have a startdate column, why can't you try Order By c.StartDate desc?
Torakami 29-Jan-16 4:38am    
I am not getting why start date is not working ,

in chromes resources section of websql , i checked data of my table

1450677593.757 updatedon
1450638000 startdate

the dates are showing like this , is this mean start date has got not time
_Asif_ 29-Jan-16 5:09am    
if you have a fieled updatedOn which is more reliable then you can also order by UpdatedOn desc as well

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900