I am new to programming and this is my first question, so I apologise if I make a mistake.
I have written this query:
SELECT U.Id, UWH.Role, USI.Title, CAST(USI.StartDate AS DATE)
FROM UserWorkHistory UWH
JOIN Users U ON UWH.UserId=U.Id
JOIN UserStoryItems USI ON U.Id=USI.UserId
JOIN UserWorkHistoryTypes UWHT ON UWH.UserWorkHistoryTypeId=UWHT.Id
WHERE U.Location LIKE '%Great Britain%'
OR U.Location LIKE '%United Kingdom%'
OR U.Location LIKE '%England%'
OR U.Location LIKE '%UK%'
OR U.Location LIKE '%U.K.%'
AND UWHT.Id = 1
AND USI.Id = 1
AND CAST(USI.StartDate AS DATE) > DATEADD(YEAR,-5,GETDATE())
AND UWH.Role LIKE '%Contract%'
OR UWH.Role LIKE '%Contractor%'
OR UWH.Role LIKE '%Freelance%'
OR UWH.Role LIKE '%Non-perm%'
OR UWH.Role LIKE '%non-permanent%'
OR USI.Title LIKE '%Contractor%'
OR USI.Title LIKE '%Contractor%'
OR USI.Title LIKE '%Freelance%'
OR USI.Title LIKE '%Non-perm%'
OR USI.Title LIKE '%non-permanent%'
OR USI.Title LIKE '%self-made%'
I am trying to specify four things:
1) That the results I get back are from the UK
2) That the results I get back contain any of the words I have specified in the 'LIKE' arguments.
3) That the results adhere to the rules (UWHT.Id = 1, USI.Id = 1).
4) That only the results with a StartDate from the last 5 years are returned to me.
Apart from the locations, nothing else is returning as I would like. I would imagine that it's because of an incorrect AND/OR syntax, but can't find a previous SO question that explains how to do this (if there is one and I've missed it, I apologise).