Unix Technical Forum

joins(?)

This is a discussion on joins(?) within the MySQL forums, part of the Database Server Software category; --> Hi, I have a question about joining tables and i cannot figure it out. Hope somebody can help me: ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 10:43 AM
eHannes
 
Posts: n/a
Default joins(?)

Hi,

I have a question about joining tables and i cannot figure it out.
Hope somebody can help me:

I have two tables

table1
-------------------
fieldid | shortlist
-------------------
name | y
adress | n
city | n
email | y


table2
----------------------
docid | fieldid | data
----------------------
1 | name | hans
2 | name | piet
2 | email | piet@where.nl


Is it possible te create the following result:

result
---------------
id | fieldid | data
1 | name | hans
1 | email | NULL
2 | name | piet
2 | email | piet@where.nl


so, for every field where table1.shortlist = 'y' want to have a
corresponding table2.data. if it isn't there then i want a NULL value

Thanks,
eHannes

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 10:43 AM
lark
 
Posts: n/a
Default Re: joins(?)

eHannes wrote:
> Hi,
>
> I have a question about joining tables and i cannot figure it out.
> Hope somebody can help me:
>
> I have two tables
>
> table1
> -------------------
> fieldid | shortlist
> -------------------
> name | y
> adress | n
> city | n
> email | y
>
>
> table2
> ----------------------
> docid | fieldid | data
> ----------------------
> 1 | name | hans
> 2 | name | piet
> 2 | email | piet@where.nl
>
>
> Is it possible te create the following result:
>
> result
> ---------------
> id | fieldid | data
> 1 | name | hans
> 1 | email | NULL
> 2 | name | piet
> 2 | email | piet@where.nl
>
>
> so, for every field where table1.shortlist = 'y' want to have a
> corresponding table2.data. if it isn't there then i want a NULL value
>
> Thanks,
> eHannes
>



i think this'll do it. run this baby like this

select
docid,
fieldid,
data

from table2,
table1

where title = fieldid
and shortlist = 'y'


--
lark -- hamzee@sbcdeglobalspam.net
To reply to me directly, delete "despam".
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 10:43 AM
Good Man
 
Posts: n/a
Default Re: joins(?)

eHannes <hscholte@xs4all.nl> wrote in news:1178028671.060641.174050
@y80g2000hsf.googlegroups.com:

> so, for every field where table1.shortlist = 'y' want to have a
> corresponding table2.data. if it isn't there then i want a NULL value


look into "left join"s.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 10:43 AM
eHannes
 
Posts: n/a
Default Re: joins(?)

On 1 mei, 16:48, Good Man <h...@letsgo.com> wrote:
> eHannes <hscho...@xs4all.nl> wrote in news:1178028671.060641.174050
> @y80g2000hsf.googlegroups.com:
>
> > so, for every field where table1.shortlist = 'y' want to have a
> > corresponding table2.data. if it isn't there then i want a NULL value

>
> look into "left join"s.


That's exactly what i tried (and a right one too) but with no results.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 10:43 AM
Good Man
 
Posts: n/a
Default Re: joins(?)

eHannes <hscholte@xs4all.nl> wrote in news:1178086607.011057.169060
@u30g2000hsc.googlegroups.com:

> On 1 mei, 16:48, Good Man <h...@letsgo.com> wrote:
>> eHannes <hscho...@xs4all.nl> wrote in news:1178028671.060641.174050
>> @y80g2000hsf.googlegroups.com:
>>
>> > so, for every field where table1.shortlist = 'y' want to have a
>> > corresponding table2.data. if it isn't there then i want a NULL value

>>
>> look into "left join"s.

>
> That's exactly what i tried (and a right one too) but with no results.


then you should look at how you've designed your tables. i understood the
result you *wanted* to get, but your table design made no sense to me. why
is there a column called "data" that is holding a first name, a last name,
and an email address in different rows???

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-28-2008, 10:43 AM
Peter H. Coffin
 
Posts: n/a
Default Re: joins(?)

On Wed, 02 May 2007 10:17:44 -0500, Good Man wrote:
> eHannes <hscholte@xs4all.nl> wrote in news:1178086607.011057.169060
> @u30g2000hsc.googlegroups.com:
>
>> On 1 mei, 16:48, Good Man <h...@letsgo.com> wrote:
>>> eHannes <hscho...@xs4all.nl> wrote in news:1178028671.060641.174050
>>> @y80g2000hsf.googlegroups.com:
>>>
>>> > so, for every field where table1.shortlist = 'y' want to have a
>>> > corresponding table2.data. if it isn't there then i want a NULL value
>>>
>>> look into "left join"s.

>>
>> That's exactly what i tried (and a right one too) but with no results.

>
> then you should look at how you've designed your tables. i understood the
> result you *wanted* to get, but your table design made no sense to me. why
> is there a column called "data" that is holding a first name, a last name,
> and an email address in different rows???


It may or may not be a good design for this particular thing, but as a
general case, this kind of technique serves a very real purpose: It
accomodates not knowing completely what the database will be used for at
the time that the design and programming are finalized. Changes to
what data are stored don't require code changes. Plus, it's amazingly
useful for storing dynamic structured objects, so long as the actual
structure somewhat resembles

CREATE TABLE `trashheap` (
`rowid` int(11) NOT NULL auto_increment,
`parent_rowid` int(11) default NULL,
`label` varchar(250) NOT NULL,
`data` varchar(250) default NULL,
PRIMARY KEY (`rowid`),
KEY `FK_trashheap` (`parent_rowid`),
CONSTRAINT `trashheap_ibfk_1` FOREIGN KEY (`parent_rowid`)
REFERENCES `trashheap` (`rowid`)
) ENGINE=InnoDB

--
12. One of my advisors will be an average five-year-old child. Any flaws in my
plan that he is able to spot will be corrected before implementation.
--Peter Anspach's list of things to do as an Evil Overlord
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-28-2008, 10:43 AM
Good Man
 
Posts: n/a
Default Re: joins(?)

"Peter H. Coffin" <hellsop@ninehells.com> wrote in
news:slrnf3he8i.fum.hellsop@abyss.ninehells.com:

>> Then you should look at how you've designed your tables. i
>> understood the result you *wanted* to get, but your table design made
>> no sense to me. why is there a column called "data" that is holding
>> a first name, a last name, and an email address in different rows???

>
> It may or may not be a good design for this particular thing, but as a
> general case, this kind of technique serves a very real purpose: It
> accomodates not knowing completely what the database will be used for
> at the time that the design and programming are finalized.


Thanks for the insight Peter, I can see how that would be useful!
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 05:30 PM.


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