|
-
Jun 6th, 2002, 08:25 AM
#1
Thread Starter
Hyperactive Member
SQL inside SQL
In this code :
$Sql = "select * from Subject where UserID>=1";
$Result = mysql_query($Sql);
while ($Row = mysql_fetch_array($Result))
{
$UserName="$Row[UserName]";
$Sql = "select * from Members where UserName=$UserName";
$Result = mysql_query($Sql);
while ($Row = mysql_fetch_array($Result))
{
echo "$Row[HomePage]";
}
}
I try to run SQL inside SQL But it give me an error !!!
the problem that I have 2 table (Subject and Members) and I want to get UserName(s) from
Subject table and after that get the homepage of those username(s) from Members Table and echo them.
How can I do that ???
Thanks
-
Jun 6th, 2002, 09:44 AM
#2
Fanatic Member
PHP Code:
$Sql = "select * from Subject where UserID>=1";
$Result = mysql_query($Sql);
while ($Row = mysql_fetch_array($Result))
{
$UserName="$Row[UserName]";
$Sql2 = "select * from Members where UserName=$UserName";
$Result2 = mysql_query($Sql2);
while ($Row2 = mysql_fetch_array($Result2))
{
echo "$Row2[HomePage]";
}
}
This should not create an error
-
Jun 6th, 2002, 01:29 PM
#3
PHP Code:
$Sql = "select * from Subject, Members where Subject.UserID>=1 and Member.UserName = $UserName";
$Result = mysql_query($Sql);
while ($Row = mysql_fetch_array($Result))
{
echo $Row["HomePage"];
echo $Row["UserName"];
}
I believe that should work.
-
Jun 6th, 2002, 01:30 PM
#4
-
Jun 6th, 2002, 01:33 PM
#5
thanks
-
Jun 6th, 2002, 02:38 PM
#6
Member
I really like the way u guys take time out to help others 
and btw, If I remember correctly, MySQL doesn't support nested queries. but pls check on the latest.
-
Jun 6th, 2002, 02:38 PM
#7
Member
I really like the way u guys take time out to help others 
and btw, If I remember correctly, MySQL doesn't support nested queries. but pls check on the latest.
-
Jun 6th, 2002, 02:41 PM
#8
Member
sorry abt the multiple posts..oops and this one too.
-
Jun 6th, 2002, 02:42 PM
#9
Fanatic Member
SQL will allow nested queries as long as you give it different variable names to work with. Although you cannot use the same variables over and over again in a nested query.
-Matt
-
Jun 6th, 2002, 02:43 PM
#10
Fanatic Member
Originally posted by thexchord
sorry abt the multiple posts..oops and this one too.
You can delete your own posts, but pressing the edit button and clicking the Delete Checkbox and then the Delete Button
-
Jun 6th, 2002, 03:03 PM
#11
Member
cpradio - I was talking specifically abt MySQL - I don't think u can do nested SELECTs yet in it.
-
Jun 6th, 2002, 03:05 PM
#12
I have version 3.0 and you can do them.
-
Jun 6th, 2002, 03:05 PM
#13
Fanatic Member
MySQL and SQL both allow nested queries. I have done them in both.
(sorry for just stating SQL previously I meant both)
-
Jun 7th, 2002, 04:29 PM
#14
Stuck in the 80s
I thought nesting wasn't implimented yet?
SELECT * FROM (SELECT * FROM users WHERE city='Wyoming') WHERE areacode='48444'
Okay, that's a bad example, because you could use AND. But I thought you couldn't nest like that yet?
-
Jun 7th, 2002, 04:40 PM
#15
that is actually looks bad. I never seen it like that. would that even work?? I am going to say not....
-
Jun 7th, 2002, 04:44 PM
#16
Stuck in the 80s
Originally posted by scoutt
that is actually looks bad. I never seen it like that. would that even work?? I am going to say not....
That's why I said I don't think it's implimented yet, but some website was talking about how it is going to be soon or something. I thought that's what you guys were talking about.
-
Jun 7th, 2002, 05:02 PM
#17
-
Jun 7th, 2002, 05:28 PM
#18
Stuck in the 80s
-
Jun 7th, 2002, 05:36 PM
#19
Fanatic Member
Originally posted by The Hobo
I thought nesting wasn't implimented yet?
SELECT * FROM (SELECT * FROM users WHERE city='Wyoming') WHERE areacode='48444'
Okay, that's a bad example, because you could use AND. But I thought you couldn't nest like that yet?
If that is what thexchord and you, Hobo meant by nesting then I apologize. I thought you were refering to the code I used.
In C, C++, and many other languages what I did would be considered "Nesting", so I guess I never realized that MySQL had such a weird definition for it.
-Matt
-
Jun 7th, 2002, 05:39 PM
#20
Stuck in the 80s
Actually, according to what Scoutt found, it's known as Sub-selects.
-
Jun 7th, 2002, 05:40 PM
#21
could be hobo, you never know. 
also the code that I did is called nesting. so they is many forms I guess
-
Jun 8th, 2002, 09:15 AM
#22
Stuck in the 80s
forms? What?
-
Jun 8th, 2002, 09:59 AM
#23
Fanatic Member
What scoutt is trying to say is the definition of "Nesting" in today's terminology is too broad, so many forms of it exist and everything that was posted in this thread has been considered some type of "Nesting" situation.
-Matt
-
Jun 8th, 2002, 11:30 AM
#24
Stuck in the 80s
-
Jun 8th, 2002, 11:50 AM
#25
yes that is right, many forms of nesting. crazy
-
Jun 8th, 2002, 01:25 PM
#26
Thread Starter
Hyperactive Member
Hi scoutt
Code :
$Sql = "select * from Subject, Members where Subject.UserID=100 and Member.UserName = $UserName";
$Result = mysql_query($Sql);
while ($Row = mysql_fetch_array($Result))
{
echo $Row["HomePage"];
echo $Row["UserName"];
}
it's very nice code .. but there is a problem here (and Member.UserName = $UserName) the problem that I do not have $UserName because I will get it form (Subject Table) at frist
so I neet code like that.
$Sql = "select * from Subject, Members where Subject.UserID=100 and Member.UserName = (UserName from Subject Table that it's UserID =100 )";
$Result = mysql_query($Sql);
while ($Row = mysql_fetch_array($Result))
{
echo $Row["HomePage"];
echo $Row["UserName"];
}
_______
There is something like that
Thanks
Last edited by prokhaled; Jun 8th, 2002 at 01:28 PM.
-
Jun 8th, 2002, 02:41 PM
#27
ok I see what you want. is there a category in Member that links it to the Subject table? because you could do something like
PHP Code:
$Sql = "select * from Subject, Members where Subject.UserID=100 and Member.Username = Subject.Username";
$Result = mysql_query($Sql);
while ($Row = mysql_fetch_array($Result))
{
echo $Row["HomePage"];
echo $Row["UserName"];
}
-
Jun 8th, 2002, 05:04 PM
#28
Stuck in the 80s
-
Jun 9th, 2002, 11:02 AM
#29
Thread Starter
Hyperactive Member
Code:
$Sql = "select * from Subject, Members where Subject.UserID=100 and Member.Username = Subject.Username";
$Result = mysql_query($Sql);
while ($Row = mysql_fetch_array($Result))
{
echo $Row["HomePage"];
}
if I have 2 column in the same name in Subject table and Members Table .. How can I echo HomePage from Subject table and how can I echo it from Members table
-
Jun 9th, 2002, 01:03 PM
#30
what difference would it make if they are both the same? it is the same member name in both isn't it. doing the way I have it you can echo anything from either table.
besides that is what you have in the very first post. 2 tables with the same name of category.
-
Jun 9th, 2002, 01:34 PM
#31
Stuck in the 80s
Why is this thread still going?
I'm loosing my hair!!
-
Jun 9th, 2002, 02:08 PM
#32
beats me, some poeple are hard to please
-
Jun 10th, 2002, 11:12 AM
#33
Just one question.
Is there an SQL way to return the highest value a column?
-
Jun 10th, 2002, 11:20 AM
#34
not that I'm aware of. you would have to get the distinct ones from the column and then put them in an array, then sort that array so you can echo it out like so echo $str[0];
actually you might wanna try
select max(columnname) from table
-
Jun 10th, 2002, 11:25 AM
#35
Stuck in the 80s
Originally posted by ricmitch_uk
Just one question.
Is there an SQL way to return the highest value a column?
I'd do:
PHP Code:
$sql = "SELECT cn FROM tn ORDER by cn DESC LIMIT 1";
$hv = mysql_fetch_array(mysql_query($q)) or die(mysql_error());
$hv should contain the highest value in that column (cn) of the table (tn)
-
Jun 10th, 2002, 11:35 AM
#36
Thanks I should have firgured that out myself. I guess I'm just getting dumb, having not done any SQL for a while. I just thought there might be an easy way I suppose.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|