Simple SQL Help

They have: 82 posts

Joined: Oct 2001

I need to select t1.field1, t1.field2. t1.field3,
even if t2.field1 = t1.field2 does not match
how can I do this?

I need t2.field1 only of it matches it needs to be optional.

select t1.field1, t1.field2. t1.field3,
t2.field1 from table1 t1, table2 t2
where t1.field=1
and t2.field1 = t1.field2

Thanks,

Mike

Blessed is the man who fears the LORD, who delights greatly in his commandments. Psalms 112:1

They have: 11 posts

Joined: May 2003

I need to select t1.field1, t1.field2. t1.field3,
even if t2.field1 = t1.field2 does not match
how can I do this?

I need t2.field1 only of it matches it needs to be optional.

select t1.field1, t1.field2. t1.field3,
t2.field1 from table1 t1, table2 t2
where t1.field=1
and t2.field1 = t1.field2

You need at least one field between the two related tables to be a relationship.

If what i think you are trying to do, is a left join. This works in sql server, not sure about mysql:

select t1.field1, t1.field2. t1.field3,
t2.field1 from table1 t1
left join table2 t2 on
t1.field2=t2.field1
where t1.field=1

What you will get is all records from table one, and all matching records from table2. However, you will get NULL fields in t2.field1 where no match occurred on the join expression.

http://www.mysql.com/doc/en/JOIN.html

They have: 82 posts

Joined: Oct 2001

Thank you very much SQLGuru!

Mike

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.