Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Having Trouble with query

First off, I'd like to point out that I'm totally new to Mysql - a couple weeks. It just stopped making my head hurt Monday when I started understanding the simple strings and the dynamic site I'm building was going fairly smooth for a total amateur data base person. Well, today my head started to hurt again, lol.

I'm trying to show a Consultant's information on the same page as their sponsor. I initially had all of the data in 1 table, since all sponsors are Consultants. I played with numerous variations, but either got nothing for the sponsor or all the same person as sponsor. After doing a bunch of reading and studying today, I decided to split the table down with the Cons_id and Sponsor_id in a seperate data base, but still in the first so that I could try to relate them. It's not working and I'm pleading for help. I'm using GoDaddy's Mysql data base and Dreamweaver MX (and they don't seem to like each other :p ). Here is what I have:

mysql_select_db($database_ConsAcct, $ConsAcct);
$query_Sponsor = "SELECT * FROM Consultant WHERE Consultant.cons_id =
(SELECT Sponsor.Cons_id FROM Sponsor WHERE Consultant.SponsorID = 'Sponsor.Spons_id')", $colname_Acct, $colname_Sponsor);
$Sponsor = mysql_query($query_Sponsor, $ConsAcct) or die(mysql_error());
$row_Sponsor = mysql_fetch_assoc($Sponsor);
$totalRows_Sponsor = mysql_num_rows($Sponsor);

And this is the error I'm getting:

Parse error: parse error, unexpected ',' in [the URL] on line 15

Line 15 is the (SELECT Sponso.Cons_id

I have beat my head over this all day and hope somebody can please help.
[1720 byte] By [Ralyn] at [2007-11-11 8:19:07]
# 1 Re: Having Trouble with query
Ralyn,

you might get into trouble if the subquery returns more than one value. Try to replace the subquery by a join. I'm not a mySql expert (use mostly MS SQL) but it would look something like

select a.*, b.cons_id from consultant a inner join Sponsor b on a.SponsorId = b.Spons_id
CedricB at 2007-11-11 23:47:09 >
# 2 Re: Having Trouble with query
Cedric,

Thank you sooooo much for the assist. I had to play with the string a little before it would go without an error, but it's still giving me the owner's information (who we do have in as a Sponsor for some of them) instead of the Sponsoring Consultant. Here is what I have now:

$query_Sponsor = "SELECT * FROM Consultant INNER JOIN Sponsor ON Consultant.SponsorId = 'Sponsor.Spons_id'";
Ralyn at 2007-11-11 23:48:09 >
# 3 Re: Having Trouble with query
Aha! I got it! Here is what ended up working, just in case anybody else has the same situation:

$query_Sponsor = "SELECT * FROM Consultant INNER JOIN Sponsor WHERE Consultant.cons_id = Sponsor.Spons_id OR Consultant.cons_id = Sponsor.Cons_id";
Ralyn at 2007-11-11 23:49:13 >
# 4 Re: Having Trouble with query
whoops, I got all excited too soon. It put in the one for all now, just a different consultant.
Ralyn at 2007-11-11 23:50:15 >
# 5 Re: Having Trouble with query
If you have the information in the second table only for some of them, you need a left outer join. It will return you the corresponding value from the sponsor table, if there is any, or a NULL value otherwise.
CedricB at 2007-11-11 23:51:14 >