I have these two tables:
user{id,name,password....};
friends{id,user_id,user_id2,accept(bool)}
for instance say i wanted to fetch friends for the user_id=14 and the row accept has to be 1! how would I do the mysql query thanks 🙂
ps. when fethcing the queries it has to be a 2 to way thing, if user1 is friends with user2
also means that user2 is friends with user1, if you get what I mean! :))
I don’t mean to be deprecating, but this is very, very basic sql, and you could have easily found the answer through google or books. That being said, here is the answer.
Let me break it down for you, since you are using SQL and if you don’t learn it you will be wasting a lot of time searching for answers.
means get all rows from friends. The “*” is a replacement for column names and means “all columns”. For instance, if you had
The rows returned would only contain those columns.
Then comes the WHERE clause.
means exactly how it looks: the rows you select should have at least one user_id equal to 14 and accept should be true. MySQL uses true, false, and null (if allowed) for booleans. Some other database, like MSSQL, which uses a “BIT” type rather than a boolean uses 1, 0, or null for true, false, and null respectively.
The OR clause is because, as you stated in your edit, this friendship can be “2 way”.
So if you put it together,