Jump to content

Recommended Posts

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?

 

 

 

 

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?

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.