+ Reply to Thread
Results 1 to 9 of 9

Thread: MysQL Query for Master table & child table

  1. #1
    phpasks is offline x10 Sophmore phpasks is an unknown quantity at this point
    Join Date
    Apr 2008
    Posts
    145

    MysQL Query for Master table & child table

    I have two table One is master table & second is secondary table.

    Master Table
    CREATE TABLE `tbltest` (
    `test_id` int(5) NOT NULL auto_increment,
    `name` varchar(50) NOT NULL default '',
    `lname` varchar(50) NOT NULL default '',
    PRIMARY KEY (`test_id`)
    )
    INSERT INTO `tbltest` (`test_id`, `name`, `lname`) VALUES
    (1, 'malav', 'shah'),
    (2, 'rahul', 'shah'),
    (3, 'kannan', 'Mopnnar'),
    (4, 'Milan', 'Shah'),
    (5, 'Kahan', 'Shah'),
    (6, 'Kaushal', 'Patel'),
    (7, 'Sweta', 'Shah'),
    (8, 'Dipak', 'Jani');

    COMMIT;


    Secondary Table
    CREATE TABLE `tbltest_hst` (
    `test_hst_id` int(10) NOT NULL auto_increment,
    `test_id` int(10) NOT NULL default '0',
    `name` varchar(50) NOT NULL default '',
    `lname` varchar(50) NOT NULL default '',
    PRIMARY KEY (`test_hst_id`)
    )

    INSERT INTO `tbltest_hst` (`test_hst_id`, `test_id`, `name`, `lname`) VALUES
    (1, 10, 'rupal', 'patel'),
    (2, 1, 'malav', 'shah'),
    (3, 2, 'rahul', 'shah'),
    (4, 13, 'rina', 'mehta'),
    (5, 11, 'viral', 'patel'),
    (6, 11, 'viral', 'patel'),
    (7, 17, 'i****a', 'shah'),
    (8, 17, 'i****a', 'shah'),
    (9, 20, 'Pooja', 'Mehta'),
    (10, 23, 'Kavita', 'Patel'),
    (11, 3, 'Kannan', 'Mopnnar'),
    (12, 4, 'Milan', 'Shah'),
    (13, 5, 'Kahan', 'Shah'),
    (14, 5, 'Kahan', 'Shah'),
    (15, 25, 'Chintan', 'Shah'),
    (16, 26, 'Neha', 'Shah'),
    (17, 25, 'Chintan', 'Shah'),
    (18, 26, 'Neha', 'Shah');

    COMMIT;

    I have first master table tbltest & Second child table tbltest_hst

    If i have 1 to 8 unique id record in master table.
    I have child table 1 to 8 unique id and also deleted some id record in my child table.

    How find out this not in master table record,
    but my child record?????

    I have used below query
    SELECT tbltest.test_id
    FROM tbltest, tbltest_hst
    where (tbltest_hst.test_id NOT IN (tbltest.test_id))


    This query does not work properly.

    How to solve this problem??????????
    Asif Khalyani
    http://www.phpasks.com

  2. #2
    woiwky is offline x10 Lieutenant woiwky is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    390

    Re: MysQL Query for Master table & child table

    I would personally use:

    Code:
    SELECT tbltest.test_id
    FROM tbltest, tbltest_hst
    WHERE tbltest_hst.test_id = tbltest.test_id
    But your query should work too if you add DISTINCT:

    Code:
    SELECT DISTINCT tbltest.test_id
    FROM tbltest, tbltest_hst
    WHERE (tbltest_hst.test_id NOT IN (tbltest.test_id))
    "But you have access to the greatest source of knowledge in the universe."
    "Well I do talk to myself sometimes, yes."

    "I'm back, and I'm bad! Obviously within certain, sensible, preset parameters"

  3. #3
    phpasks is offline x10 Sophmore phpasks is an unknown quantity at this point
    Join Date
    Apr 2008
    Posts
    145

    Re: MysQL Query for Master table & child table

    Quote Originally Posted by woiwky View Post
    I would personally use:

    Code:
    SELECT tbltest.test_id
    FROM tbltest, tbltest_hst
    WHERE tbltest_hst.test_id = tbltest.test_id
    But your query should work too if you add DISTINCT:

    Code:
    SELECT DISTINCT tbltest.test_id
    FROM tbltest, tbltest_hst
    WHERE (tbltest_hst.test_id NOT IN (tbltest.test_id))
    This is not right query.
    [IMG]file:///C:/DOCUME%7E1/JIMIT%7E1.BAG/LOCALS%7E1/Temp/moz-screenshot.jpg[/IMG][IMG]file:///C:/DOCUME%7E1/JIMIT%7E1.BAG/LOCALS%7E1/Temp/moz-screenshot-1.jpg[/IMG]This out put is unique value but this value come in master table as well as same as child table.

    I have needed out put this way.
    Child table value come but not in master table this id used.
    Asif Khalyani
    http://www.phpasks.com

  4. #4
    woiwky is offline x10 Lieutenant woiwky is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    390

    Re: MysQL Query for Master table & child table

    Oh, sorry. I misunderstood you. This should get you the records in the child table which aren't in the master table:

    Code:
    SELECT test_id
    FROM tbltest_hst
    WHERE (test_id NOT IN (SELECT test_id FROM tbltest))
    "But you have access to the greatest source of knowledge in the universe."
    "Well I do talk to myself sometimes, yes."

    "I'm back, and I'm bad! Obviously within certain, sensible, preset parameters"

  5. #5
    phpasks is offline x10 Sophmore phpasks is an unknown quantity at this point
    Join Date
    Apr 2008
    Posts
    145

    Re: MysQL Query for Master table & child table

    Quote Originally Posted by woiwky View Post
    Oh, sorry. I misunderstood you. This should get you the records in the child table which aren't in the master table:

    Code:
    SELECT test_id
    FROM tbltest_hst
    WHERE (test_id NOT IN (SELECT test_id FROM tbltest))
    Thanks woiwky,

    But i know inner query using this solution.

    With out inner join possible or not.
    Asif Khalyani
    http://www.phpasks.com

  6. #6
    woiwky is offline x10 Lieutenant woiwky is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    390

    Re: MysQL Query for Master table & child table

    Without a JOIN or subquery, I don't think it can be done.
    "But you have access to the greatest source of knowledge in the universe."
    "Well I do talk to myself sometimes, yes."

    "I'm back, and I'm bad! Obviously within certain, sensible, preset parameters"

  7. #7
    phpasks is offline x10 Sophmore phpasks is an unknown quantity at this point
    Join Date
    Apr 2008
    Posts
    145

    Re: MysQL Query for Master table & child table

    Quote Originally Posted by woiwky View Post
    Without a JOIN or subquery, I don't think it can be done.
    Without inner query, because inner query very heavy query.

    How much load for that inner query?
    Do u know?
    Asif Khalyani
    http://www.phpasks.com

  8. #8
    woiwky is offline x10 Lieutenant woiwky is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    390

    Re: MysQL Query for Master table & child table

    You'll probably be better off using an left join actually. Joins are usually faster than a subquery. But you may not notice much of a difference unless you have a lot of records. I can't say what the load would be though. Check the execution time on this query:

    Code:
    SELECT tbltest_hst.test_id
    FROM tbltest_hst
    LEFT JOIN tbltest ON tbltest_hst.id = tbltest.id
    WHERE ISNULL(tbltest.id)
    If the result set is very large, then you may be able to increase the performance with a temporary table.
    "But you have access to the greatest source of knowledge in the universe."
    "Well I do talk to myself sometimes, yes."

    "I'm back, and I'm bad! Obviously within certain, sensible, preset parameters"

  9. #9
    phpasks is offline x10 Sophmore phpasks is an unknown quantity at this point
    Join Date
    Apr 2008
    Posts
    145

    Re: MysQL Query for Master table & child table

    Quote Originally Posted by woiwky View Post
    You'll probably be better off using an left join actually. Joins are usually faster than a subquery. But you may not notice much of a difference unless you have a lot of records. I can't say what the load would be though. Check the execution time on this query:

    Code:
    SELECT tbltest_hst.test_id
    FROM tbltest_hst
    LEFT JOIN tbltest ON tbltest_hst.id = tbltest.id
    WHERE ISNULL(tbltest.id)
    If the result set is very large, then you may be able to increase the performance with a temporary table.
    thanks buddy.

    thanks lot
    Asif Khalyani
    http://www.phpasks.com

+ Reply to Thread

Similar Threads

  1. [PHP] MySQL and PHP
    By Bryon in forum Tutorials
    Replies: 43
    Last Post: 03-24-2011, 07:27 AM
  2. New Site-Suggestions?
    By mnoutside in forum Review My Site
    Replies: 9
    Last Post: 08-27-2008, 07:01 AM
  3. Mysql help needed: getting number of items in table column
    By xprtweb in forum Programming Help
    Replies: 1
    Last Post: 02-23-2008, 01:17 AM
  4. mysql - db table size smaller then other hosting? how
    By ootyg in forum Scripts & 3rd Party Apps
    Replies: 0
    Last Post: 11-26-2007, 04:50 PM
  5. Table inside table
    By wizeman in forum Tutorials
    Replies: 4
    Last Post: 07-11-2005, 05:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers