Can someone help with my SQl statement?

This is a discussion on Can someone help with my SQl statement? within the MySQL Database forums, part of the Database Forums category; Here is an example of my data: entry_id cat_id 1 20 2 25 3 30 4 25 5 35 6 ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-11-2008
Chuck Cheeze
 
Posts: n/a
Default Can someone help with my SQl statement?

Here is an example of my data:

entry_id cat_id
1 20
2 25
3 30
4 25
5 35
6 25
2 30
2 35
3 35

As you can see, entry_id's 2 and 3 both belong to cat_id 30 and 35

I have captured the cat_id's 30 and 35 with my script, so I need all
entry_id's that belong to BOTH cat_id 30 and 35.

I tried "Select entry_id from myTable where cat_id = '30' and cat_id
=
'35' but obviously that is incorrect.

Can someone help? Thanks...
Reply With Quote
  #2 (permalink)  
Old 05-11-2008
PleegWat
 
Posts: n/a
Default Re: Can someone help with my SQl statement?

On Sun, 11 May 2008 08:42:49 -0700, Chuck Cheeze wrote:
> I have captured the cat_id's 30 and 35 with my script, so I need all
> entry_id's that belong to BOTH cat_id 30 and 35.
>
> I tried "Select entry_id from myTable where cat_id = '30' and cat_id =
> '35' but obviously that is incorrect.


SELECT a.entry_id
FROM myTable a
INNER JOIN myTable b
ON a.entry_id = b.entry_id
WHERE a.cat_id = 30
AND b.cat_id = 35



--
Remove caps to reply
Reply With Quote
  #3 (permalink)  
Old 05-11-2008
Chuck Cheeze
 
Posts: n/a
Default Re: Can someone help with my SQl statement?

On May 11, 10:40*am, PleegWat
<pleegwat.REM...@CAPS.telfort.nl.INVALID> wrote:
> On Sun, 11 May 2008 08:42:49 -0700, Chuck Cheeze wrote:
> > I have captured the cat_id's 30 and 35 with my script, so I need all
> > entry_id's that belong to BOTH cat_id 30 and 35.

>
> > I tried "Select entry_id from myTable where cat_id = '30' and cat_id =
> > '35' but obviously that is incorrect.

>
> SELECT a.entry_id
> FROM myTable a
> INNER JOIN myTable b
> * * * * ON a.entry_id = b.entry_id
> WHERE a.cat_id = 30
> * * * * AND b.cat_id = 35
>
> --
> Remove caps to reply


Perfect thanks!
Reply With Quote
  #4 (permalink)  
Old 05-11-2008
Luuk
 
Posts: n/a
Default Re: Can someone help with my SQl statement?

Chuck Cheeze schreef:
> On May 11, 10:40 am, PleegWat
> <pleegwat.REM...@CAPS.telfort.nl.INVALID> wrote:
>> On Sun, 11 May 2008 08:42:49 -0700, Chuck Cheeze wrote:
>>> I have captured the cat_id's 30 and 35 with my script, so I need all
>>> entry_id's that belong to BOTH cat_id 30 and 35.
>>> I tried "Select entry_id from myTable where cat_id = '30' and cat_id =
>>> '35' but obviously that is incorrect.

>> SELECT a.entry_id
>> FROM myTable a
>> INNER JOIN myTable b
>> ON a.entry_id = b.entry_id
>> WHERE a.cat_id = 30
>> AND b.cat_id = 35
>>
>> --
>> Remove caps to reply

>
> Perfect thanks!


Select DISTINCT entry_id from myTable where cat_id = '30' OR cat_id = '35'

will also work....

--
Luuk
Reply With Quote
  #5 (permalink)  
Old 05-11-2008
Jerry Stuckle
 
Posts: n/a
Default Re: Can someone help with my SQl statement?

Luuk wrote:
> Chuck Cheeze schreef:
>> On May 11, 10:40 am, PleegWat
>> <pleegwat.REM...@CAPS.telfort.nl.INVALID> wrote:
>>> On Sun, 11 May 2008 08:42:49 -0700, Chuck Cheeze wrote:
>>>> I have captured the cat_id's 30 and 35 with my script, so I need all
>>>> entry_id's that belong to BOTH cat_id 30 and 35.
>>>> I tried "Select entry_id from myTable where cat_id = '30' and cat_id =
>>>> '35' but obviously that is incorrect.
>>> SELECT a.entry_id
>>> FROM myTable a
>>> INNER JOIN myTable b
>>> ON a.entry_id = b.entry_id
>>> WHERE a.cat_id = 30
>>> AND b.cat_id = 35
>>>
>>> --
>>> Remove caps to reply

>>
>> Perfect thanks!

>
> Select DISTINCT entry_id from myTable where cat_id = '30' OR cat_id = '35'
>
> will also work....
>


Nope, that will get it if EITHER is true. He needs BOTH to be true.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 10:43 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0