Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sql WHERE prob 1

Status
Not open for further replies.

thompom

Technical User
Dec 4, 2006
395
GB
Hi,

Normally use mysql but am having to use a 3rd parties DB with an ODBC driver that doesnt support JOINS and am having trouble getting a query to work.

sql looks like - which works fine and returns one record
Code:
SELECT MK_01_CustomerRecords.FirstName, MK_01_CustomerRecords.Surname,
MK_01_CustomerRecords.Address001, MK_01_CustomerRecords.Postcode,
MK_01_vehicleRecords.registrationnumber
FROM MK_01_VehicleRecords, MK_01_customerrecords, MK_01_vehiclelinkindex
WHERE MK_01_vehicleRecords.vehicleNumber = MK_01_vehiclelinkindex.vehiclemagicno
AND MK_01_CustomerRecords.CustomerNumber = MK_01_vehiclelinkindex.ctmagicnumber
AND MK_01_vehiclelinkindex.vehiclestatus ='C'
AND MK_01_VehicleRecords.vehiclenumber = '136225'

but if i want to query two [or more] vehicles i thought i would change the sql to

Code:
SELECT MK_01_CustomerRecords.FirstName, MK_01_CustomerRecords.Surname,
MK_01_CustomerRecords.Address001, MK_01_CustomerRecords.Postcode,
MK_01_vehicleRecords.registrationnumber
FROM MK_01_VehicleRecords, MK_01_customerrecords, MK_01_vehiclelinkindex
WHERE MK_01_vehicleRecords.vehicleNumber = MK_01_vehiclelinkindex.vehiclemagicno
AND MK_01_CustomerRecords.CustomerNumber = MK_01_vehiclelinkindex.ctmagicnumber
AND MK_01_vehiclelinkindex.vehiclestatus ='C'
AND MK_01_VehicleRecords.vehiclenumber = '136225'
OR MK_01_VehicleRecords.vehiclenumber = '196451'

but this returns loads of records - can anyone see what the prob is?
if i query each id separately they return one record each.
 
looks like the old problem of precedence;
try with brackets:
AND (MK_01_VehicleRecords.vehiclenumber = '136225'
OR MK_01_VehicleRecords.vehiclenumber = '196451')
 
amazing the difference a couple of brackets make!
looks like i need to read up on precedence
thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top