coder_1432 coder_1432 - 3 months ago 5
MySQL Question

Why does using the "AND" operator works with a JOIN, even without a "WHERE" clause

Good afternoon, I just noticed something. If I write a mysql query with a JOIN in it, for example:

Select Car, Vendor from Cars JOIN Vendor On Vendor.IdCar=Car.IdCar WHERE Car="Brand"


It will return that car that is equal to "Brand".

But I noticed that if I structure the query this way:

Select Car, Vendor from Cars JOIN Vendor On Vendor.IdCar=Car.IdCar AND Car="Brand"


It will return the same thing as the previous query. Noticed that I used AND instead of WHERE. I also tested in PHP queries it worked. So for example:

Select Car, Vendor from Cars JOIN Vendor On Vendor.IdCar=Car.IdCar AND Car=$brand


instead of

Select Car, Vendor from Cars JOIN Vendor On Vendor.IdCar=Car.IdCar WHERE Car=$brand


So the question I'm asking is: Why does the second query work? Is it because JOIN was used, because I was pretty sure that WHERE clause was needed before AND?

Answer

Both WHERE and ON require a (boolean) condition. And a boolean condition can be constructed from boolean terms by combining them with AND, OR and NOT(and possibly ( and ) )

  • WHEREcondition is an (optional) clause in a query
  • table-expression JOIN table-expression ON condition is an operator that combines two table-expressions, based on the condition.
Comments