Well it's sorta' simple once you get your mind around this - you need two
tables but you've only got one.  And there's no OR, you need both to be true
for a site_id ...

The query would be easy if you actually had two tables so we'll search off
of the same table twice giving it different names each time (sc1 and sc2).

 SELECT  sc1.*
 FROM    site_category sc1, site_category sc2
 WHERE   sc1.category_id = $category_id_1
   AND   sc2.category_id = $category_id_2
   AND   sc1.site_id = sc2.site_id

DISTINCT and such are not necessary unless it's possible you'll have
duplicate rows (and you'd mind getting them back multiple times).

I know this will work in all the big ones (Oracle, MS SQL, IBM) but I'm less
sure about MySQL as I don't use it all that much.  This is standard SQL.

Good Luck,
Frank

On 3/18/02 10:53 PM, "[EMAIL PROTECTED]"
<[EMAIL PROTECTED]> wrote:

> From: Summit <[EMAIL PROTECTED]>
> Date: Mon, 18 Mar 2002 19:22:22 -0700
> To: [EMAIL PROTECTED]
> Subject: SQL statement - PHP/mySQL - brain fart
> 
> For some reason I keep thinking that this should be simple - but I just
> can't seem to figure it out.  Help???  Please???  [I've been working on
> it for two days now.]
> 
> Overview:  What I'm trying to do is query one table by passing it two
> different variables - and return only the results that are COMMON to
> both variables.  [PHP 4.1.2/mySQL 3.23.44/FreeBSD]
> 
> Assume I have a site_category table:
> 
> -------------------
> site_category
> -------------------
> site_category_id
> site_id
> category_id
> -------------------
> 
> Perhaps a dump of this looks something like this:
> 
> ----------------        -------         -----------
> site_category_id        site_id         category_id
> ----------------        -------         -----------
>       1                  2               10
>       2                  3               11
>       3                  4               12
>       4                  4               10
>       5                  5               12
>       6                  5               14
> ----------------        -------         -----------
> 
> Using values for the varibles I'm passing to the query (see below) of
> ...
> 
> $category_id_1 = 10
> $category_id_2 = 12
> 
> ... the result I'm looking for is:
> 
>       site_id = 4
> 
> ... as this is the only site_id which is common to both ...
> 
> category_id = 10
> category_id = 12
>


-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to