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
 combining columns in output

Author  Topic 

barleytwist
Starting Member

2 Posts

Posted - 2006-02-23 : 13:30:16
Using MySQL 5, MySQLQuery (latest). Complete Newbie,

I have the following query:

SELECT i.IndivId, i.Surname, i.First_Names, i.Parents,
(SELECT i.Surname FROM individuals i, families f
where (i.IndivId = f.father_ID) and (i.indivId = f.Family_ID))
as "father"
FROM individuals i
where i.Parents>0
order by i.Parents;

It returns nulls for the subselect. There are 2 tables, individuals and Parents. I am trying to use a father id. in the PArents table to cause the individual id. (being the father of the individual(s)) to be named in the father column. I have a feeling I'm going wrong here.

I think the query above is pretty self explanatory!

Many thanks.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-02-23 : 13:47:11
Please provide a data example of what you want.

BTW, you have posted in a SQL Server forum, so the queries that we provide may not work for you since you are using mySql. For mySql specific queries, you might try posting in a mySql forum such as the one at dbforums.com.

Tara Kizer
aka tduggan
Go to Top of Page

barleytwist
Starting Member

2 Posts

Posted - 2006-02-24 : 05:28:44
Sorry! What's a data sample? (I told you I was new!).
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-02-24 : 13:06:42
A data sample is you showing us a few rows of data of all of the tables involved in the query, plus what the data should look like when the query is run. Your query is not self-explanatory to people who have not worked on your system, so we need to see data to understand. It can be made up data. It just needs to illustrate your problem.

Tara Kizer
aka tduggan
Go to Top of Page

sal
Starting Member

6 Posts

Posted - 2006-02-24 : 13:26:38
I wouldn't use i as a table alias in the subquery and the main query.
Anyway, in the subquery, you're matching the child ID to both the father ID and the family ID. They're (father ID & family ID) probably not the same, so you don't get any matches. If they are the same, one is redundant.
Like Tara wrote, it would be helpful to see a sample of your data. Intuitively, it seems either the tables are poorly designed or that your comparisons will never match. Why would the individual id match either the father or family ids, not to mention both.
Go to Top of Page
   

- Advertisement -