Results 1 to 36 of 36

Thread: SQL inside SQL

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259

    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

  2. #2
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    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
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  3. #3
    scoutt
    Guest
    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.

  4. #4
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    show off
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  5. #5
    scoutt
    Guest


    thanks

  6. #6
    Member
    Join Date
    Apr 2002
    Posts
    52
    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.

  7. #7
    Member
    Join Date
    Apr 2002
    Posts
    52
    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.

  8. #8
    Member
    Join Date
    Apr 2002
    Posts
    52
    sorry abt the multiple posts..oops and this one too.

  9. #9
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    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
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  10. #10
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    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
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  11. #11
    Member
    Join Date
    Apr 2002
    Posts
    52
    cpradio - I was talking specifically abt MySQL - I don't think u can do nested SELECTs yet in it.

  12. #12
    scoutt
    Guest
    I have version 3.0 and you can do them.

  13. #13
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    MySQL and SQL both allow nested queries. I have done them in both.

    (sorry for just stating SQL previously I meant both)
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  14. #14
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    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?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  15. #15
    scoutt
    Guest
    that is actually looks bad. I never seen it like that. would that even work?? I am going to say not....

  16. #16
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    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.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  17. #17
    scoutt
    Guest
    here you go, that code is actually valid, scary isn't

    http://www.mysql.com/doc/A/N/ANSI_diff_Sub-selects.html

  18. #18
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    So I'm not crazy, eh?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  19. #19
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    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
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  20. #20
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Actually, according to what Scoutt found, it's known as Sub-selects.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  21. #21
    scoutt
    Guest
    could be hobo, you never know.

    also the code that I did is called nesting. so they is many forms I guess

  22. #22
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    forms? What?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  23. #23
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    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
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  24. #24
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Life is crazy, yo!
    My evil laugh has a squeak in it.

    kristopherwilson.com

  25. #25
    scoutt
    Guest
    yes that is right, many forms of nesting. crazy

  26. #26

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259

    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.

  27. #27
    scoutt
    Guest
    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"]; 


  28. #28
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    My evil laugh has a squeak in it.

    kristopherwilson.com

  29. #29

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259
    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

  30. #30
    scoutt
    Guest
    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.

  31. #31
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Why is this thread still going?





    I'm loosing my hair!!
    My evil laugh has a squeak in it.

    kristopherwilson.com

  32. #32
    scoutt
    Guest
    beats me, some poeple are hard to please

  33. #33
    ricmitch_uk
    Guest
    Just one question.

    Is there an SQL way to return the highest value a column?

  34. #34
    scoutt
    Guest
    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

  35. #35
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    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)
    My evil laugh has a squeak in it.

    kristopherwilson.com

  36. #36
    ricmitch_uk
    Guest
    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
  •  



Click Here to Expand Forum to Full Width