Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Sep 2004
    Posts
    713
    Thanks
    6
    Thanked 2 Times in 2 Posts

    How to do this effect

    $sql=mysql_query("select * from abc WHERE name='laal'");

    $r=mysql_fetch_array($sql);

    $id=$r['id'];

    mysql_query("SELECT * from cde WHERE id='$id'");

    how to combine these two query together to become one query
    flying dagger

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    No idea what your tables look like... whether `name` is unique, etc. There are multiple options.

    http://dev.mysql.com/doc/refman/5.1/en/subqueries.html
    Code:
    SELECT *
    FROM `cde`
    WHERE `id` = (
    	SELECT `id`
    	FROM `abc`
    	WHERE `name` = 'laal'
    )
    http://dev.mysql.com/doc/refman/5.1/en/join.html
    Code:
    SELECT `cde`.*
    FROM `cde`
    LEFT JOIN `abc`
    	ON `abc`.`id` = `cde`.`id`
    WHERE `abc`.`name` = 'laal'
    Last edited by kbluhm; 01-21-2010 at 06:55 PM.

  • #3
    Regular Coder
    Join Date
    Sep 2004
    Posts
    713
    Thanks
    6
    Thanked 2 Times in 2 Posts
    left join? table cde have 2 records which are same id, which means the id from abc will be used to select two recrods from table cde, i think left join cannot produce this outcome right?
    flying dagger

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    I think it would.

    The sub query won't work for you? It is technically still two queries but will result in less code.

    The left join is what you're looking for. It will return all matching rows from `cde` where `cde`.`id` = `abc`.`id` and `abc`.`name` = laal
    Last edited by kbluhm; 01-21-2010 at 06:57 PM.

  • #5
    Regular Coder
    Join Date
    Sep 2004
    Posts
    713
    Thanks
    6
    Thanked 2 Times in 2 Posts
    please help me again. Let me give you the table structure

    table abc

    cid name quality
    1 laal 3
    2 gaga 4

    table cde
    id cid subname
    1 1 baba
    2 1 sasa
    3 2 tata

    the cid in table cde is same as cid in table abc

    now i want to ge4t something like structure below
    cid name subname quality
    1 laal (baba,sasa) 3
    2 gaga (tata) 4


    how can i do this?
    flying dagger


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •