Unix Technical Forum

RE: SELECT single row from 2 tables with WHERE clause

This is a discussion on RE: SELECT single row from 2 tables with WHERE clause within the MySQL General forum forums, part of the MySQL category; --> Many thanks Peter. That's the definition I was after. Kerry -----Original Message----- From: Peter K AGANYO [mailto:kimaiga@gmail.com]On Behalf Of ...


Go Back   Unix Technical Forum > Database Server Software > MySQL > MySQL General forum

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 05:52 AM
Kerry Frater
 
Posts: n/a
Default RE: SELECT single row from 2 tables with WHERE clause

Many thanks Peter. That's the definition I was after.

Kerry
-----Original Message-----
From: Peter K AGANYO [mailto:kimaiga@gmail.com]On Behalf Of Peter K AGANYO
Sent: 19 February 2007 00:35
To: kerry@myforefathers.co.uk
Cc: mysql@lists.mysql.com
Subject: Re: SELECT single row from 2 tables with WHERE clause


Hi Kerry,

Try this:

SELECT t1.*,t2.thedescription AS t2desc FROM table1 AS t1 LEFT JOIN table2
AS t2 ON t1.linkfield=t2.linkfield WHERE t1.lookup='ZZZZZ'

Without the WHERE condition this would return all 1000 rows of table 1
since "A Left join returns all rows of the left of the conditional even if
there is no right column to match." but t1.lookup='ZZZZZ' constrains this to
only the one row of table one with lookup equal to 'ZZZZZ'.

Enjoy

Peter


On 2/17/07, Kerry Frater <kerry@myforefathers.co.uk> wrote:
I am trying to select a particular row from a table and include a column
for
aq second table but I cannot get the result I am after.

I have "table1" with 1000 rows and "table2" with 12 rows. The
relationship
between the tables is a column "linkedfield". Table1 has a unique key
called
"lookup"

If I use the code
SELECT t1.*,t2.thedescription AS t2desc FROM table1 AS t1, table2 AS t2
where (t1.lookup='ZZZZZ')
I get a result of 12 rows (as expected)

SELECT t1.*,t2.thedescription AS t2desc FROM table1 AS t1, table2 AS t2
where (t1.linkfield=t2.linkfield)
I get 1000 rows as expected

SELECT t1.*,t2.thedescription AS t2desc FROM table1 AS t1, table2 AS t2
where (t1.lookup='ZZZZZ') and (t1.linkfield=t2.linkfield)
returns 1 row if there is an active link between the two tables and

returns 0 rows if there isn't.

This is where I am stuck. In the last example I would like the 1 row
whether
there is an "active" link or not. The difference will be simply that the
"t2desc" rsulting column will be blank or contain a value.

Can anyone help me with the logic?

Kerry

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:31 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com