Jump to content

sql


jonniejoejonson

Recommended Posts

Hey guys,

You're help would be much appreciated...

If I have the following table and sample data... myGroupTable

 

group_Id : user_Id

1 : 3

1 : 7

1 : 100

4 : 42

4 : 98

4 : 13

 

1. I would like a sql statement that would...

Return a group_Id that has exactly the specified user_Id's in them.

eg... is there a group_Id that has User_Id's 3, 7 and 100

answer: group_id 1.

 

2. If I were to have a table like the above one... would I just index group_id

 

Kind regards J

Link to comment
Share on other sites

First, use IN -- WHERE user_Id IN '#value1',''#value2',''#value3')

 

Second, index choice is highly dependent on multiple usage scenarios -- in this case, user_Id would be a good choice, assuming this style represents a major percentage of the queries you're running on this table.

Link to comment
Share on other sites

  • 1 month later...

Hey guys,

thanks for your responses, but neither is doing what I had hoped...

@fenway If I use your method it selects all rows that the the user_Id's are in... but I only want them to be selected if they are an exact match....

 

 

eg: If I have the following table and sample data... myGroupTable

Hey guys,

You're help would be much appreciated...

If I have the following table and sample data... myGroupTable

 

group_Id : user_Id

1 : 3

1 : 7

1 : 100

2 : 3

2 : 7

2 : 100

2 : 104

4 : 42

4 : 98

4 : 13

 

I would like a sql statement that would...

Return a group_Id that has exactly the specified user_Id's in them.

eg... is there a group_Id that has User_Id's 3, 7 and 100

answer: group_id 1.

Please note that I dont want it to return a group_Id 2, as that also has a user_Id of 104 in it...

Kind regards J

Link to comment
Share on other sites

Surely the first solution worked as needed? Even if it does have an unnecessary amount of parentheses.

 

SELECT `group_Id`
FROM `myGroupTable`
WHERE `user_Id` = 3
    AND `user_Id` = 7
    AND `user_Id` = 100
GROUP BY `group_Id`
ORDER BY `group_Id` DESC

 

Or did you just plug in what Muddy provided without actually putting in the proper variables?

Link to comment
Share on other sites

Surely the first solution worked as needed? Even if it does have an unnecessary amount of parentheses.

 

SELECT `group_Id`
FROM `myGroupTable`
WHERE `user_Id` = 3
    AND `user_Id` = 7
    AND `user_Id` = 100
GROUP BY `group_Id`
ORDER BY `group_Id` DESC

 

Or did you just plug in what Muddy provided without actually putting in the proper variables?

This will NOT work at all.  userID can't be 3 things at once.  I didn't really Muddy suggested that -- it's a common misconception.

 

This, OTOH, will work:

SELECT t1.`group_Id`
FROM `myGroupTable` AS t1
CROSS JOIN `myGroupTable` AS t2
CROSS JOIN `myGroupTable` AS t3
WHERE t1.`user_Id` = 3
    AND t2.`user_Id` = 7
    AND t3.`user_Id` = 100

It's more efficient, too.

Link to comment
Share on other sites

Surely the first solution worked as needed? Even if it does have an unnecessary amount of parentheses.

 

SELECT `group_Id`
FROM `myGroupTable`
WHERE `user_Id` = 3
    AND `user_Id` = 7
    AND `user_Id` = 100
GROUP BY `group_Id`
ORDER BY `group_Id` DESC

 

Or did you just plug in what Muddy provided without actually putting in the proper variables?

This will NOT work at all.  userID can't be 3 things at once.  I didn't really Muddy suggested that -- it's a common misconception.

 

This, OTOH, will work:

SELECT t1.`group_Id`
FROM `myGroupTable` AS t1
CROSS JOIN `myGroupTable` AS t2
CROSS JOIN `myGroupTable` AS t3
WHERE t1.`user_Id` = 3
    AND t2.`user_Id` = 7
    AND t3.`user_Id` = 100

It's more efficient, too.

 

I suppose it couldn't. Good solution.

Link to comment
Share on other sites

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.