If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Please help me on this mysql

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-23-10, 09:10
nsatheesh87 nsatheesh87 is offline
Registered User
 
Join Date: Mar 2010
Posts: 5
Red face 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
Reply With Quote
  #2 (permalink)  
Old 03-23-10, 09:58
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
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

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 03-24-10, 01:15
nsatheesh87 nsatheesh87 is offline
Registered User
 
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
Reply With Quote
  #4 (permalink)  
Old 03-24-10, 05:06
nsatheesh87 nsatheesh87 is offline
Registered User
 
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
Reply With Quote
  #5 (permalink)  
Old 03-24-10, 06:16
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
i already showed you how to do it

and i say again, you really should change your table design
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 03-24-10, 07:15
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
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.
__________________
Mike
Reply With Quote
  #7 (permalink)  
Old 03-24-10, 07:26
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
mike, i had already reported this thread and asked that it be deleted because it's a re-post of this one -- Please help me on this mysql

perhaps they should simply be merged now that this one has a reply
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 03-24-10, 07:53
nsatheesh87 nsatheesh87 is offline
Registered User
 
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
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On