Help writing SQL statement in PHP script

This is a discussion on Help writing SQL statement in PHP script within the PHP Language forums, part of the PHP Programming Forums category; This might be in the wrong group, but... Here is an example of my data: entry_id cat_id 1 20 2 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-11-2008
Chuck Cheeze
 
Posts: n/a
Default Help writing SQL statement in PHP script

This might be in the wrong group, but...

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
CJ Willcock
 
Posts: n/a
Default Re: Help writing SQL statement in PHP script

Chuck Cheeze wrote:
> This might be in the wrong group, but...
>
> 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...


There's probably a nicer way but you can do something like this to get
through:

$q = "SELECT `entry_id`, `cat_id` FROM `myTable` WHERE `cat_id` = '30'
OR `cat_id` = > '35';
$res = $mysql->getRes();
while( $row = $res->fetch() ) {
if(30 == $row['cat_id']) $myBase[$row['entry_id']] &= 0x01;
elseif(35 == $row['cat_id']) $myBase[$row['entry_id']] &= 0x02;
}

foreach( $myBase as $key => $flags ) if( 0x03 == $flags ) $myEntries[] =
$key;


Cheers,
CJW
Reply With Quote
  #3 (permalink)  
Old 05-11-2008
CJ Willcock
 
Posts: n/a
Default Re: Help writing SQL statement in PHP script

CJ Willcock wrote:
> Chuck Cheeze wrote:
>> This might be in the wrong group, but...
>>
>> 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...

>
> There's probably a nicer way but you can do something like this to get
> through:
>
> $q = "SELECT `entry_id`, `cat_id` FROM `myTable` WHERE `cat_id` = '30'
> OR `cat_id` = > '35';
> $res = $mysql->getRes();
> while( $row = $res->fetch() ) {
> if(30 == $row['cat_id']) $myBase[$row['entry_id']] &= 0x01;
> elseif(35 == $row['cat_id']) $myBase[$row['entry_id']] &= 0x02;
> }
>
> foreach( $myBase as $key => $flags ) if( 0x03 == $flags ) $myEntries[] =
> $key;
>
>
> Cheers,
> CJW


Whoops!

Read SQL statement:


$q = "SELECT `entry_id`, `cat_id` FROM `myTable` WHERE `cat_id` = '30'
OR `cat_id` = '35';
Reply With Quote
  #4 (permalink)  
Old 05-11-2008
petersprc
 
Posts: n/a
Default Re: Help writing SQL statement in PHP script

Hi,

You could also do:

select entry_id from tbl where cat_id
in (30, 35) group by entry_id having
count(entry_id) = 2;

Assuming the table is duplicate-free:

create unique index ent_cat on tbl
(entry_id, cat_id)

Regards,

John Peters

On May 11, 2:06 am, Chuck Cheeze <chadcrow...@gmail.com> wrote:
> This might be in the wrong group, but...
>
> 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
  #5 (permalink)  
Old 05-11-2008
Jerry Stuckle
 
Posts: n/a
Default Re: Help writing SQL statement in PHP script

Chuck Cheeze wrote:
> This might be in the wrong group, but...
>
> 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...
>


This isn't a PHP question - it's a database question. You need a group
for your database (i.e. if it's MySQL, comp.databases.mysql).

You'll get good answers there because that's where the SQL experts hang
out. The answers posted here so far are incorrect.

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

Reply With Quote
  #6 (permalink)  
Old 05-12-2008
Mike Lahey
 
Posts: n/a
Default Re: Help writing SQL statement in PHP script

Jerry Stuckle wrote:
> You'll get good answers there because that's where the SQL experts hang
> out. The answers posted here so far are incorrect.


Pay attention to the posts. Peter's solution will work.

You can learn about grouping queries at this tutorial page:
http://www.w3schools.com/sql/sql_groupby.asp
Reply With Quote
  #7 (permalink)  
Old 05-12-2008
NC
 
Posts: n/a
Default Re: Help writing SQL statement in PHP script

On May 10, 11:06 pm, Chuck Cheeze <chadcrow...@gmail.com> wrote:
>
> 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.


SELECT t1.entry_id
FROM theTable AS t1 LEFT JOIN theTable AS t2
ON t1.entry_id = t2.entry_id
WHERE t1.cat_id = 30 AND t2.cat_id = 35

Cheers,
NC
Reply With Quote
  #8 (permalink)  
Old 05-12-2008
Captain Paralytic
 
Posts: n/a
Default Re: Help writing SQL statement in PHP script

On 12 May, 02:36, Mike Lahey <mikey6...@yahoo.com> wrote:
> Jerry Stuckle wrote:
> > You'll get good answers there because that's where the SQL experts hang
> > out. The answers posted here so far are incorrect.

>
> Pay attention to the posts. Peter's solution will work.


It may work, but that does not make it the "correct" way to do it.
Reply With Quote
  #9 (permalink)  
Old 05-12-2008
aguillacutty@gmail.com
 
Posts: n/a
Default Re: Help writing SQL statement in PHP script

On May 12, 9:20 am, Captain Paralytic <paul_laut...@yahoo.com> wrote:
> On 12 May, 02:36, Mike Lahey <mikey6...@yahoo.com> wrote:
>
> > Pay attention to the posts. Peter's solution will work.

>
> It may work, but that does not make it the "correct" way to do it.


Oh no, the answer is correct, but there are alternatives. What you
must to realize is if the OP decides to match not 2, but 30 or 500
category IDs, you must use group by. This is more universal solution.

--
"Man who lives in glass house should change clothes in basement."
Reply With Quote
  #10 (permalink)  
Old 05-12-2008
Jerry Stuckle
 
Posts: n/a
Default Re: Help writing SQL statement in PHP script

Mike Lahey wrote:
> Jerry Stuckle wrote:
>> You'll get good answers there because that's where the SQL experts
>> hang out. The answers posted here so far are incorrect.

>
> Pay attention to the posts. Peter's solution will work.
>
> You can learn about grouping queries at this tutorial page:
> http://www.w3schools.com/sql/sql_groupby.asp
>


Not necessarily. What happens if he has two entries with (2,30)? It
will fail.

Nothing in the description of the problem prohibits such an occurrence.

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

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB 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 09:56 PM.


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