I am trying to search for questions on Puzzling Stack Exchange that contain certain tags so that I can find puzzles that I might be able to solve.
I have been able to write a simple script that conforms to my other restriction of having a post score of between 0 and 10, however when I try to implement the restriction of having certain tags by including an inner loop, it gives me the error
Line 5: Incorrect syntax near the keyword "FROM"
for some reason.
Here is my code that I have so far:
SELECT p.Title, p.Id, p.Score, p.ViewCount, u.DisplayName, q.Tags
FROM Posts q INNER JOIN PostTags pt ON q.Id=pt.PostId
INNER JOIN Tags t ON t.id = pt.TagId
WHERE t.TagName=##tagName:string?logical-deduction##
FROM Posts p
JOIN Users u ON p.OwnerUserId = u.Id
WHERE p.PostTypeId = 1
And p.Score <= 10
And p.Score >= 0
And p.AcceptedAnswerId is null
ORDER BY p.Score DESC
My question is: Why is this "incorrect syntax" error being caused? I feel like it may be a problem with either of the INNER JOIN
s, but I'm not really that sure.
The result I would like here is for the code to return the same table of questions as I have already done, along with the added restriction of only returning questions with the logical-deduction
tag.
I don't exactly have a lot of prior experience with SEDE/SQL, so any help would be appreciated.
I managed to figure out what my problem was:
JOIN Users u ON p.OwnerUserId = u.Id
) elsewhere, however no matter what, there would be an error caused by invalid syntax on that line. So, I decided to scrap it for now.p.PostTypeId
, WHERE
should actually be And
because p.PostTypeId
isn't a value that needs to be restricted. (which is also another error I encountered when fixing this)FROM
should only be in the code once, as mentioned by @nbk), and realizing that I need to put JOIN Users u ON p.OwnerUserId = u.Id
on Line 3 because the code ends up breaking if the JOIN
function doesn't come before the INNER JOIN
s.So, in conclusion, the reason there was an error was because my code was a dumpster fire and would probably have been fixed sooner if I had just tidied up my code a little.
The reworked code:
SELECT p.Title, p.Id, p.Score, p.ViewCount, u.DisplayName, p.Tags
FROM Posts p
JOIN Users u ON p.OwnerUserId = u.Id
INNER JOIN PostTags pt ON p.Id=pt.PostId
INNER JOIN Tags t ON t.Id = pt.TagId
WHERE t.TagName=##tagName:string?logical-deduction##
And p.PostTypeId = 1
And p.Score <=10
And p.Score >=0
And p.AcceptedAnswerId is null
ORDER BY p.Score DESC