I’m pretty new to MYSQL. I’m trying to write a query which will search across multiple tables, but it appears to only evaluate the first condition of the query. The query I have developed is:
SELECT DISTINCT user.user_id, user_netid, user_firstname, user_lastname,
user_dmid, user_insnum, instype_id, user_birthday, user_insapproved
FROM user, signup, event
WHERE (user.instype_id = 3 OR user.instype_id = 4)
AND signup.signup_attended =1 AND signup.signup_timestamp >= 1325376000
AND (event.activity_id=37 OR event.activity_id=40 OR event.activity_id=5);
I know it’s pretty long, but like I said, I’m new to this and it was the best I could come up with. In any case, It appears to only evaluate user.instype_id because it gives me records in which signup.signup_attended !=1 or any of the other conditions which appear after user.instype_id. Can someone please show me what I’m doing wrong?
My guess is that this is happening due to your cross join (list of tables with commas…but no joining statements). Basically, you may filter out users with an inst type, but you will still show users with a
signup_attendedbecause theusertable and thesignuptable are not linked so it just starts mashing the tables together. Maybe wikipedia can explain a cross join better than I can 🙂You need to rewrite your code using
JOINHere is an example of what it would look like afterwards (with guesses on the join columns)
Perform a google on SQL JOIN and you should get a good bit of info explaining this. Here is one tutorial