Eggzorcist Posted August 23, 2012 Share Posted August 23, 2012 Hi everyone. My question does it become too much for a social media site to have too many queries to the database. Maybe my database retrieval ability is very weak though I find myself having trouble trying to get lots of information from my database in only in a few queries. For example. I have a database that looks like this: id1, id2, var1. the Id's are user IDs. Though I also need both user's names which is from the users db. THough I'm having trouble obtaining both their names. SELECT * FROM users, owes WHERE (owes.id1 = 1 OR owes.id2 = 1) AND owes.active = 1 AND owes.confirmed = 1 AND owes.id1 = users.id AND owes.id2 = users.id Though my last SQL line does not select both, instead it selects nothing because users.id seem to already have a user taken and thus since the first users.id is not = var2 nothing is selected. I want to select the data which id = 1 is at id1 or id2, while selecting the profile info of id = 1 and id = 2. is this possible all in one query? Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 24, 2012 Share Posted August 24, 2012 From what I can understand of your code, and the little information you've posted about your tables, it seems the second table holds a list over whom owes whom what. If that is the case, then you really need to take a second look at exactly what you're asking from the database with the last condition. owes.id1 = users.id // The person owed is the person who's detail you're retrieving in the current row. AND owes.id2 = users.id // The person with the debt is the person who's detail you're retrieving in the current row. See something that doesn't quite make sense there? Quote Link to comment Share on other sites More sharing options...
Barand Posted August 24, 2012 Share Posted August 24, 2012 You need to join twice to the users table using different table aliases SELECT u1.name as name1, u2.name as name2, owes.* FROM owes INNER JOIN users u1 ON owes.id1 = u1.id INNER JOIN users u2 ON owes.id2 = u2.id WHERE (owes.id1 = 1 OR owes.id2 = 1) AND owes.active = 1 AND owes.confirmed = 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.