Re: SQL Syntax
| From: | Simcha Younger | Date: | Wed, 16 Jun 2010 08:57:15 +0000 |
| Subject: | Re: SQL Syntax | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-306189@lists.php.net to get a copy of this message | ||
On Wed, 16 Jun 2010 02:58:31 +0200
"Jan Reiter" <the-fallen@gmx.net> wrote:
>
> I have 2 tables. Table A containing 2 fields. A user ID and a picture ID =>
> A(uid,pid) and another table B, containing 3 fields. The picture ID, an
> attribute ID and a value for that attribute => B(pid,aid,value).
>
>
>
> Table B contains several rows for a single PID with various AIDs and values.
> Each AID is unique to a PID. (e.g. AID = 1 always holding the value for the
> image size and AID = 3 always holding a value for the image type)
>
>
>
> The goal is now to join table A on table B using pid, and selecting the rows
> based on MULTIPLE attributes.
>
>
>
> So the result should only contain rows for images, that relate to an
> attribute ID = 1 (size) that is bigger than 100 AND!!!!!!! an attribute ID =
> 5 that equals 'jpg'.
>
>
>
> I appreciate your thoughts on this.
>
>
>
> Regards,
>
> Jan
>
something like this (untested)
$sql = 'select * from a ';
$join = '';
$where = array();
//each condition 'column = X'
foreach($condition as $count=>$cond){
$join .= ' left join b as b'.$count' on a.pid=b'.$count.'.pid ';
$where[] = 'b'.$count'.'.'.$cond;
}
$sql .= $join.implode(' AND ', $where);
--
Simcha Younger <simcha@syounger.com>