esport Posted August 15, 2006 Share Posted August 15, 2006 Hey guys,Having some problems trying to get my query to work.I have a query where it select the first character of the company feild, but if there is no company it then selects the first character of the surname. I was wondering how to go about it. Below is a snippet of what i currently have.[code]SELECT * FROM contacts WHERE left(company,1)='$letter'[/code]How do i do it if compnay is null so then search in surname.ThanksDaniel Quote Link to comment https://forums.phpfreaks.com/topic/17686-query-help/ Share on other sites More sharing options...
fenway Posted August 15, 2006 Share Posted August 15, 2006 WHERE LEFT( IF(company<>'',company,surname), 1 ) = '$letter' Quote Link to comment https://forums.phpfreaks.com/topic/17686-query-help/#findComment-75389 Share on other sites More sharing options...
esport Posted August 15, 2006 Author Share Posted August 15, 2006 Hey thanks for the prompt reply. It works great. Now if both company and surname where null and if I want it to check another feild can I just simply do it like this.[code]WHERE LEFT( IF(company<>'',company,surname,name), 1 ) = '$letter'[/code]ThanksDaniel Quote Link to comment https://forums.phpfreaks.com/topic/17686-query-help/#findComment-75397 Share on other sites More sharing options...
fenway Posted August 16, 2006 Share Posted August 16, 2006 No, you'd need another nested IF:[code]WHERE LEFT( IF( company<>'', company, IF( surname <> '', surname, name ) ), 1 ) = '$letter'[/code] Quote Link to comment https://forums.phpfreaks.com/topic/17686-query-help/#findComment-75732 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.