Page 1 of 1

select from one, depending on the other

Posted: Fri Feb 10, 2006 12:01 pm
by ed209
I would like to select information from Table1 where Table2.field1 is not empty.

There is a link between them Table1.filed1 is the same as Table2.field2...

This is what I have in head although it doesn't work, is it possible to do this without doing 2 queries?

Code: Select all

SELECT *
FROM Table1
WHERE 
Table1.Field1=theID AND (Table2.Field1<>"" AND Table2.Field2=theID)
Is this possible?

Posted: Fri Feb 10, 2006 12:06 pm
by feyd

Code: Select all

SELECT
  *
FROM
  `Table1`
INNER JOIN
  `Table2`
  ON (
    `Table2`.`Field2` = `Table1`.`Field1`
  )
WHERE
  `Table2`.`Field1` IS NOT NULL
  AND
  `Table2`.`Field1` <> ''
  AND
  `Table1`.`Field1` = 'theID'

Posted: Fri Feb 10, 2006 12:13 pm
by raghavan20

Code: Select all

select a.* from `table1` a
join table2 b on b.field2 = a.field1
where a.field1 = $theId
and
b.field2 is not null