Jump to content


Photo

query help


  • Please log in to reply
3 replies to this topic

#1 esport

esport
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts

Posted 15 August 2006 - 10:13 PM

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.

SELECT * FROM contacts					
WHERE  left(company,1)='$letter'

How do i do it if compnay is null so then search in surname.

Thanks

Daniel

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 15 August 2006 - 10:57 PM

WHERE LEFT( IF(company<>'',company,surname), 1 ) = '$letter'
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 esport

esport
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts

Posted 15 August 2006 - 11:21 PM

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.

WHERE LEFT( IF(company<>'',company,surname,name), 1 ) = '$letter'

Thanks

Daniel

#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 16 August 2006 - 03:53 PM

No, you'd need another nested IF:

WHERE LEFT( IF( company<>'', company, IF( surname <> '', surname, name ) ), 1 ) = '$letter'

Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users