Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2010
    Posts
    5

    Red face Unanswered: Please help me on this mysql

    Table1:
    ad_id ad_data
    1 hello
    2 hi
    3 hru
    4 test

    table2:
    id column_ad1 column_ad2 column_ad3 column_ad4 column_ad5 column_ad6
    1 1 2 3 1 4 4

    here table1.ad_id = table2.column_id(n)

    here i need to extract ad_data from table1 through its corresponding ad_id .
    how do get the data from table1 using table2

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Code:
    SELECT table2.id
         , t1_1.ad_data AS ad_data_1
         , t1_2.ad_data AS ad_data_2
         , t1_3.ad_data AS ad_data_3
         , t1_4.ad_data AS ad_data_4
         , t1_5.ad_data AS ad_data_5
         , t1_6.ad_data AS ad_data_6
      FROM table2
    INNER
      JOIN table1 AS t1_1
        ON t1_1.ad_id = table2.column_ad1
    INNER
      JOIN table1 AS t1_2
        ON t1_2.ad_id = table2.column_ad2
    INNER
      JOIN table1 AS t1_3
        ON t1_3.ad_id = table2.column_ad3
    INNER
      JOIN table1 AS t1_4
        ON t1_4.ad_id = table2.column_ad4
    INNER
      JOIN table1 AS t1_5
        ON t1_5.ad_id = table2.column_ad5
    INNER
      JOIN table1 AS t1_6
        ON t1_6.ad_id = table2.column_ad6
    it doesn't have to be this ugly, if table2 were normalized

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Mar 2010
    Posts
    5

    help me on this phpmysql

    --
    -- Table structure for table `column_ads`
    --

    CREATE TABLE IF NOT EXISTS `column_ads` (
    `id` int(5) NOT NULL AUTO_INCREMENT,
    `column_id_1` int(10) NOT NULL,
    `column_id_2` int(10) NOT NULL,
    `column_id_3` int(11) NOT NULL,
    `column_id_4` int(11) NOT NULL,
    `column_id_5` int(11) NOT NULL,
    `column_id_6` int(11) NOT NULL,
    `column_id_7` int(11) NOT NULL,
    `column_id_8` int(11) NOT NULL,
    `column_id_9` int(11) NOT NULL,
    `page_id` int(5) NOT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;

    --
    -- Dumping data for table `column_ads`
    --

    INSERT INTO `column_ads` (`id`, `column_id_1`, `column_id_2`, `column_id_3`, `column_id_4`, `column_id_5`, `column_id_6`, `column_id_7`, `column_id_8`, `column_id_9`, `page_id`) VALUES
    (1, 6, 2, 3, 2, 3, 3, 3, 3, 3, 1),
    (8, 2, 2, 3, 2, 3, 3, 3, 3, 3, 34);

    --
    -- Table structure for table `advertisement`
    --

    CREATE TABLE IF NOT EXISTS `advertisement` (
    `ad_id` int(11) NOT NULL AUTO_INCREMENT,
    `title` varchar(60) NOT NULL,
    `script` text NOT NULL,
    `created_on` varchar(30) NOT NULL,
    PRIMARY KEY (`ad_id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

    --
    -- Dumping data for table `advertisement`
    --

    INSERT INTO `advertisement` (`ad_id`, `title`, `script`, `created_on`) VALUES
    (3, 'gghf', '<html><title>hello</title></html>', 'March 22, 2010, 6:56 am'),
    (2, 'sofent', '<script typefghf="text/javascript>\r\nhi</script>', 'March 22, 2010, 6:51 am'),
    (5, 'vcbcvb', '<html><title>hello</title></html>', 'March 23, 2010, 8:37 am'),
    (6, 'fdg', '<html><title>hello</title></html>', 'March 23, 2010, 9:47 am');

    Actually
    i need to extract the data from script column from advertisement table..

    notes: column_id_1,....column_id_9 from column_ads table holds advertisement table ad_id data.
    i need to extract the script from advertisement table by column_id_1---column_id_9 where page_id = (example:1)

    o/p

    column_id_1-->2 means i want to extract <script typefghf="text/javascript>\r\nhi</script> bec ad_id is 2

    likewise i have to get upto column_id_9

  4. #4
    Join Date
    Mar 2010
    Posts
    5

    how can i build this one?

    --
    -- Table structure for table `column_ads`
    --

    CREATE TABLE IF NOT EXISTS `column_ads` (
    `id` int(5) NOT NULL AUTO_INCREMENT,
    `column_id_1` int(10) NOT NULL,
    `column_id_2` int(10) NOT NULL,
    `column_id_3` int(11) NOT NULL,
    `column_id_4` int(11) NOT NULL,
    `column_id_5` int(11) NOT NULL,
    `column_id_6` int(11) NOT NULL,
    `column_id_7` int(11) NOT NULL,
    `column_id_8` int(11) NOT NULL,
    `column_id_9` int(11) NOT NULL,
    `page_id` int(5) NOT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;

    --
    -- Dumping data for table `column_ads`
    --

    INSERT INTO `column_ads` (`id`, `column_id_1`, `column_id_2`, `column_id_3`, `column_id_4`, `column_id_5`, `column_id_6`, `column_id_7`, `column_id_8`, `column_id_9`, `page_id`) VALUES
    (1, 6, 2, 3, 2, 3, 3, 3, 3, 3, 1),
    (8, 2, 2, 3, 2, 3, 3, 3, 3, 3, 34);

    --
    -- Table structure for table `advertisement`
    --

    CREATE TABLE IF NOT EXISTS `advertisement` (
    `ad_id` int(11) NOT NULL AUTO_INCREMENT,
    `title` varchar(60) NOT NULL,
    `script` text NOT NULL,
    `created_on` varchar(30) NOT NULL,
    PRIMARY KEY (`ad_id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

    --
    -- Dumping data for table `advertisement`
    --

    INSERT INTO `advertisement` (`ad_id`, `title`, `script`, `created_on`) VALUES
    (3, 'gghf', '<html><title>hello</title></html>', 'March 22, 2010, 6:56 am'),
    (2, 'sofent', '<script typefghf="text/javascript>\r\nhi</script>', 'March 22, 2010, 6:51 am'),
    (5, 'vcbcvb', '<html><title>hello</title></html>', 'March 23, 2010, 8:37 am'),
    (6, 'fdg', '<html><title>hello</title></html>', 'March 23, 2010, 9:47 am');

    Actually
    i need to extract the data from script column from advertisement table..

    notes: column_id_1,....column_id_9 from column_ads table holds advertisement table ad_id data.
    i need to extract the script from advertisement table by column_id_1---column_id_9 where page_id = (example:1)

    o/p

    column_id_1-->2 means i want to extract <script typefghf="text/javascript>\r\nhi</script> bec ad_id is 2

    likewise i have to get upto column_id_9

  5. #5
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    i already showed you how to do it

    and i say again, you really should change your table design
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  6. #6
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    I am sure there is a question in there somewhere but I can't find it. So instead of answering I'll just comment that your column_ads table looks wrong to me - would you do better with :

    Code:
    CREATE TABLE IF NOT EXISTS column_ads (
       id int NOT NULL AUTO_INCREMENT,
       page_id int NOT NULL,
       column_no tinyint NOT NULL,
       add_id int NOT NULL,
       PRIMARY KEY (id)
    );
    Not sure how useful the id column is either - I would just use page_id, column_no and add_id but it depends on your application.

  7. #7
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    mike, i had already reported this thread and asked that it be deleted because it's a re-post of this one -- http://www.dbforums.com/mysql/165479...-me-mysql.html

    perhaps they should simply be merged now that this one has a reply
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  8. #8
    Join Date
    Mar 2010
    Posts
    5

    Thank you r937 and mike.

    Now i changed my table design as per you suggestion, Now its working fine. Thank you very much for both, you two helps me a lot, i have to learn more on normalization as well designing table

Posting Permissions

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