Be Excellent To Each Other

And, you know, party on. Dude.

All times are UTC [ DST ]




Reply to topic  [ 7 posts ] 
Author Message
 Post subject: SQL Stuff
PostPosted: Tue May 24, 2011 21:08 
User avatar

Joined: 31st Mar, 2008
Posts: 8658
It's late and my head hurts, do my work for me please beex.

Let's say I have a table that looks like this:

A | B
------
1 | True
1 | False
1 | False
2 | True
2 | True

I want a query that will return field A, where every result in B is true (so in this case I want it to return 2).
I'm a bit rusty, so I'm probably missing something easy.


Top
 Profile  
 
 Post subject: Re: SQL Stuff
PostPosted: Tue May 24, 2011 21:40 
SupaMod
User avatar
Est. 1978

Joined: 27th Mar, 2008
Posts: 69783
Location: Your Mum
Er...
Code:
SELECT `A` FROM `tablename` WHERE `B` = 'True'

?

[edit]Oh right, hang on.

_________________
Grim... wrote:
I wish Craster had left some girls for the rest of us.


Top
 Profile  
 
 Post subject: Re: SQL Stuff
PostPosted: Tue May 24, 2011 21:42 
User avatar

Joined: 31st Mar, 2008
Posts: 8658
Grim... wrote:
Er...
Code:
SELECT `A` FROM `tablename` WHERE `B` = 'True'

?

Er...
No


Top
 Profile  
 
 Post subject: Re: SQL Stuff
PostPosted: Tue May 24, 2011 21:43 
SupaMod
User avatar
Est. 1978

Joined: 27th Mar, 2008
Posts: 69783
Location: Your Mum
Here's one way:
Code:
SELECT `A` FROM `tablename` AS tablename1
LEFT JOIN `tablename` AS tablename2 ON tablename1.A = tablename2.A AND tablename2.B = 'False'
WHERE tablename2.A IS NULL


Better column names next time, please ;)

[edit]If the table is huge and the join is too slow, you can do it with a GROUP BY and an IF.
[edit2]Capital 'F' on False.

_________________
Grim... wrote:
I wish Craster had left some girls for the rest of us.


Top
 Profile  
 
 Post subject: Re: SQL Stuff
PostPosted: Tue May 24, 2011 21:49 
User avatar
ugvm'er at heart...

Joined: 4th Mar, 2010
Posts: 22406
Grim... wrote:
Er...
Code:
SELECT `A` FROM `tablename` WHERE `B` = 'True'

?

[edit]Oh right, hang on.


Heh, I wrote exactly that! Then re-read the question and thought, hang on... :D


Top
 Profile  
 
 Post subject: Re: SQL Stuff
PostPosted: Tue May 24, 2011 21:51 
User avatar

Joined: 31st Mar, 2008
Posts: 8658
I know I said I was rusty, but give me some credit. Cheers Grim... I shall test that later.


Top
 Profile  
 
 Post subject: Re: SQL Stuff
PostPosted: Wed May 25, 2011 9:58 
User avatar

Joined: 30th Mar, 2008
Posts: 14427
Location: Shropshire, UK
Could you not do
Code:
SELECT DISTINCT(A) FROM tablename WHERE B='True';


//edit: No, I've just re-read it. Haha.


Top
 Profile  
 
Display posts from previous:  Sort by  
Reply to topic  [ 7 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Columbo, The Greys and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search within this thread:
You are using the 'Ted' forum. Bill doesn't really exist any more. Bogus!
Want to help out with the hosting / advertising costs? That's very nice of you.
Are you on a mobile phone? Try http://beex.co.uk/m/
RIP, Owen. RIP, MrC. RIP, Dimmers.

Powered by a very Grim... version of phpBB © 2000, 2002, 2005, 2007 phpBB Group.