Page 1 of 1

SQL help!

Posted: 24 Dec 2016, 02:37
by Dibbles
I asked this in discord, but alas got no replies :( So I thought I might ask in here, as there's a better majority of you guys here than discord :)

I am trying to get a result from a table for usergroups... but the secondary_group could have multiple groups in it (2,3,6,8)

Code: Select all

SELECT username FROM users WHERE primary_group = 3 OR secondary_group = 3
anyone got any suggestions?

Re: SQL help!

Posted: 24 Dec 2016, 08:03
by Sathias
Is this what you are trying to do??

Code: Select all

SELECT username FROM users WHERE primary_group = 3 OR secondary_group IN (2,3,6,8)

Re: SQL help!

Posted: 24 Dec 2016, 13:08
by Mini
Sorry, didn't see the post on Discord until this morning Dibbles, but agree with what Sathias has put above

Re: SQL help!

Posted: 24 Dec 2016, 18:44
by Dibbles
Sathias wrote:Is this what you are trying to do??

Code: Select all

SELECT username FROM users WHERE primary_group = 3 OR secondary_group IN (2,3,6,8)
That works, but displays multiple of the same username (if they are in multiple of the groups).

ie,

User 1 is in Group 1,3,4
User 2 is in Group 2,3,6

the query above will display

username - User 1
username - User 2
username - User 2
username - User 2

Is there a way to limit to 1 per username? (I probably know it, but I just can't put my head to it right now -_-)

Re: SQL help!

Posted: 24 Dec 2016, 18:49
by Mini
Group By?

So...

Code: Select all

SELECT username FROM users WHERE primary_group = 3 OR secondary_group IN (2,3,6,8) Group By username

Re: SQL help!

Posted: 24 Dec 2016, 19:13
by Dibbles
Mini wrote:Group By?

So...

Code: Select all

SELECT username FROM users WHERE primary_group = 3 OR secondary_group IN (2,3,6,8) Group By username
yep! that works wonders! cheers! Turns out I was thinking of a different way, which may not actually have worked ;)