select from one, depending on the other

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
ed209
Forum Contributor
Posts: 153
Joined: Thu May 12, 2005 5:06 am
Location: UK

select from one, depending on the other

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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'
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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
Post Reply