4 Replies - 762 Views - Last Post: 09 August 2010 - 04:36 AM Rate Topic: -----

#1 biemel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 30-July 10

Select from where problem

Posted 08 August 2010 - 01:27 AM

Hi!

I have a simple database with one table "people". Everey "people" got a name and a birthday. I m trying to find a person by his age.

Select * from people where name ='Mike' and birthday < '1962-01-01'



But there s a Mike with birthday 1962-04-02 in my result?! Why? Wham am i doing wrong?

Thanks so much!

Ah i got it:

Select * from people where birthday < '1962-01-01'and name ='Mike'



This works for some reason.. Maybe someone can tell me what s the matter?

Thanks!

This post has been edited by biemel: 08 August 2010 - 02:38 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Select from where problem

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3635
  • View blog
  • Posts: 5,756
  • Joined: 08-June 10

Re: Select from where problem

Posted 08 August 2010 - 04:14 AM

Hey.

That makes no sense. Switching the order of the conditions should make no difference.

What are the types of your columns? Is the "birthday" field a DATE field, or a string type?

This post has been edited by Atli: 08 August 2010 - 04:14 AM

Was This Post Helpful? 0
  • +
  • -

#3 biemel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 30-July 10

Re: Select from where problem

Posted 08 August 2010 - 07:06 AM

It is a DATE field.. I don t understand why the hell this works..
Was This Post Helpful? 0
  • +
  • -

#4 biemel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 30-July 10

Re: Select from where problem

Posted 09 August 2010 - 03:44 AM

Ah i got, it was my fault.. (of course)...

My first query was:
Select * From people where name = 'Mike' or name = 'Mandy' and birthday < '1962-01-01'



Now i know: because of the "and" it will search for all Mikes no matter how old they are^^
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5642
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Select from where problem

Posted 09 August 2010 - 04:36 AM

Qualify your OR:
Select * From people where (name = 'Mike' or name = 'Mandy') and birthday < '1962-01-01'



If it's all AND, you don't have to worry about parens. For AND and OR, it gets confused. Your SQL might be thinking:
Select * From people where name = 'Mike' or (name = 'Mandy' and birthday < '1962-01-01')


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1