Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Complicated SELECT JOIN

Author  Topic 

MMAMail
Starting Member

2 Posts

Posted - 2010-07-30 : 08:41:51
The tables:

create table person (
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
title enum('Dr','Miss', 'Mr', 'Mrs', 'Ms','Prof','Rev','Ven'),
PRIMARY KEY(ID)
)ENGINE=INNODB;

create table friend (
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
fk_person_id INTEGER UNSIGNED,
family_name VARCHAR(50),
PRIMARY KEY (id),
FOREIGN KEY (fk_person_id) REFERENCES person(id) ON DELETE NO ACTION
)ENGINE=INNODB;


This is my normal join which returns multiple records.


SELECT p.id as person_id, f.family_name as friend_family_name, f.id as friend_id
FROM person AS p
JOIN friend AS f ON p.id = f.fk_person_id;


This is what I get:


person_id friend_family_name friend_id
8 dimakopoulos 15
8 sotiropoulos 16
8 papadogkonas 17
9 zoumboulakis 18
9 zambolis 19
9 koula 20


This is what I like to get instead:


person_id friend_family_name friend_id friend_family_name2 friend_id2 friend_family_name3 friend_id3
8 dimakopoulos 15 sotiropoulos 16 papadogkonas 17
9 zoumboulakis 18 zambolis 19 koula 20



--

http://mmaMail.com <============ FREE MMA Magazine



MMA Mail Magazine
http://mmaMail.com <============ FREE MMA Magazine
.

naveengopinathasari
Yak Posting Veteran

60 Posts

Posted - 2010-07-30 : 08:49:35
The Families are fixed or is it growing
as i see in the above example there are only 3 families
will it be growing???

Lets unLearn
Go to Top of Page

MMAMail
Starting Member

2 Posts

Posted - 2010-07-30 : 10:56:04
well this is the problem I am having as well. I don't for sure, but you know some time in the future the users will say they want the friends to grow :( so I am trying to protect against that

MMA Mail Magazine
http://mmaMail.com <============ FREE MMA Magazine
.
Go to Top of Page

Celko
Starting Member

23 Posts

Posted - 2010-07-31 : 21:00:36
Please a book on RDBMS and attention to part about First Normal Form (1NF). It will near the front of the book, since this is the foundation of the Relational Model.

It will tell you that what you are doing is wrong and that you do display work in the front end, never in the database.

Author of
SQL for Smarties
Thinking in Sets
SQL Puzzles and Answers
Analytics and OLAP in SQL
SQL Programming Style
Standards in SQL
Data & Databases
Trees & Hierarchies in SQL
Go to Top of Page

naveengopinathasari
Yak Posting Veteran

60 Posts

Posted - 2010-08-02 : 03:50:02
Yeah its a Interesting Requirement and its possible to acheive it.

STEP 1: design your table information with parent child relation, you need to rewrite how the data is decpicted.
STEP 2: using CTE EXPRESSION you can achieve.

The Result will look like using the above expression

8 dimakopoulos
---> 15 sotiropoulos
--> 16 papadogkonas 17
9 zoumboulakis
--> 18 zambolis
--> 19 koula 20


If you can design the table and give the details has how u want the data to be decipted, i can help you.




Lets unLearn
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2010-08-02 : 04:40:06
This is a sql server forum and you're obviously working with MySQL. Try the forums at mysql.com instead or maybe dbforums...

- Lumbago

My blog (yes, I have a blog now! just not that much content yet)
-> www.thefirstsql.com
Go to Top of Page
   

- Advertisement -