PHP or MySQL query to combine and total 2 tables.

Hi,
I have 2 tables as shown below. One table contains sponsored link keywords clicked to reach my site.
How can I group them by keyword and add cost for a certain date period, and also include income(advert_cost) from my 2nd table when keyword match, so that I can then calculate the cost per converions.
Basically I need to be able to compare the money spent on keywords with any income earned from those keywords.

CREATE TABLE `sponsored_links` (
  `id` int(20) NOT NULL auto_increment,
  `company` varchar(20) default NULL,
  `website` varchar(4) default NULL,
  `click_cost` tinyint(4) default NULL,
  `keywords` varchar(120) default NULL,
  `ondate` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2647 ;

--
-- Dumping data for table `sponsored_links`
--

INSERT INTO `sponsored_links` VALUES (1, 'Google', 'OP', 35, '"real estate"', '2007-03-23 21:27:53');
INSERT INTO `sponsored_links` VALUES (2, 'Google', 'OP', 32, '"Sell House Privately"', '2007-03-23 22:03:34');
INSERT INTO `sponsored_links` VALUES (3, 'Google', 'OP', 32, '"how to sell real estate"', '2007-03-23 22:03:44');
INSERT INTO `sponsored_links` VALUES (4, 'Google', 'OP', 32, '"real estate"', '2007-03-23 22:03:54');
INSERT INTO `sponsored_links` VALUES (5, 'Google', 'OP', 65, '"real estate virtual tour"', '2007-03-23 22:03:57');
INSERT INTO `sponsored_links` VALUES (6, 'Google', 'OP', 65, '"real estate perth"', '2007-03-23 22:04:04');
INSERT INTO `sponsored_links` VALUES (7, 'Google', 'OP', 32, '"sell house online"', '2007-03-23 22:04:11');
INSERT INTO `sponsored_links` VALUES (8, 'Google', 'OP', 32, '"sell house trade"', '2007-03-23 22:04:15');
INSERT INTO `sponsored_links` VALUES (9, 'Google', 'OP', 65, '"need to sell my home fast"', '2007-03-23 22:04:21');
INSERT INTO `sponsored_links` VALUES (10, 'Google', 'OP', 65, '"for sale by owner realty"', '2007-03-23 22:04:35');


CREATE TABLE `sponsored_conversions` (
  `id` int(20) NOT NULL auto_increment,
  `company` varchar(20) default NULL,
  `website` varchar(4) default NULL,
  `click_cost` tinyint(4) default NULL,
  `keywords` varchar(120) default NULL,
  `advert_cost` varchar(8) default NULL,
  `ondate` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

--
-- Dumping data for table `sponsored_conversions`
--

INSERT INTO `sponsored_conversions` VALUES (1, 'Google', 'OP', 65, '"need to sell my home fast"', '66.00', '2007-03-28 10:45:42');
INSERT INTO `sponsored_conversions` VALUES (2, 'Google', 'OP', 32, '"4ealestate"', '66.00', '2007-03-28 10:45:43');
INSERT INTO `sponsored_conversions` VALUES (3, 'Yahoo', 'REW', 45, '"real estate"', '132.00', '2007-03-30 03:22:17');
INSERT INTO `sponsored_conversions` VALUES (4, 'Yahoo', 'REW', 45, '"real estate"', '132.00', '2007-03-30 03:22:20');

sabecsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JakobACommented:
I assume the fields are identical in the two tables. Then you can use a union

SELECT  u_table.id, u_table.company, ...
FROM    (                                                               -- start a sub-select
               SELECT * FROM sponsored_links
               UNION
               SELECT * from sponsored_conversions
             ) AS u_table                                            -- name the subselect table
WHERE  u_table.company = 'google'
     AND  ...

Now that is only the 'from 2 tables' part, and frankly the 'right' way of doing it is to combine those 2 tables into one. fx by adding a type-field:
     type       enum( 'link', 'conversion' )  not null

With the keywords also I would go a different route by creating a table that contain nothing but single keywords and a foreign key indicating which link that keyword belongs to.

eg the record
    1, 'Google', 'OP', 65, '"need to sell my home fast"', '66.00', '2007-03-28 10:45:42'

would becon 1 row in the sponsored_something table:
    1, 'Google', 'OP', 65, '66.00', '2007-03-28 10:45:42','conversion'
and 6 rows in the keys table: ( primary key is (fkey,wordkey) )
fkey,  wordkey
    1,    'need'
    1,    'to'
    1,    'sell'
    1,    'my'
    1,    'home'
    1,    'fast'

that wy you can join the tables on id=fkey and do a simple count of rows.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sabecsAuthor Commented:
Thanks for your suggestions JakobA, I will definitely combine both tables  with
  type       enum( 'link', 'conversion' )  not null as you suggest, this should make things a lot easier for me.

I am a bit confused over the keywords table though, "need to sell my home fast" for example is really a  phrase that needs to be entered in google and not individual keywords, that is they have to be entered in the same order for my ad to appear. What benefit would a seperate keyword table have?
Thanks again, as I am a newbie with MySQL your comments are greatly appreciated.
0
sabecsAuthor Commented:
Now that I have combined the 2 tables what query can I use to provide total costs for each keyword(or phrase) by each company and also add any advert_costs  for those keywords which can then provide a cost per conversion for those keywords ( cost per conversion would be equal to the total of advert_cost divided by the total of click_costs for a particular keyword). I hope this makes sense.

 
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

JakobACommented:
That is where splitting the keyvords-prhase into single words comes in handy. >Because even thoug the sequence is lost you also gain some considerable advantages.

1)  I want a house, a large one
2)  I want a large house
3)  I want a house
4)  I want a large airy house
5)  I want an airy large house

Now if you look for 'I want a house' you should find all of those
If you look for 'I want an airy house' you should find 4 and 5
if you look for 'I want a large house' you should find 1, 2, 4 and 5

If you just search for the full phrase you find much less, and omit all the other ways that desire could be phrased.

so to find the 'best fit' entries in the big table you use the little table

with the keyword phrase "I want an airy house" it goes something like this

$searchphrase = "I want an airy house";
$arrayWhere = split( " ", $searchphrase );    // 1 word in each cell
$phreaseWhere = "WHERE wordkey = '" .join( "' OR wordkey = '",  $arrayWhere ) ."'";
echo $phreaseWhere;   // DEBUG line so you can se what that makes. remove it later.

$sqlCommand = "
SELECT sponsored_something.*
FROM   (  SELECT id, count(*) AS c
               FROM keys
               $phreaseWhere
               GROUP BY id
               ORDER BY c DESC
            ) AS subsel
           JOIN sponsored_something ON subsel.id = sponsored_something.id
LIMIT 0,10
";

That should give you the ten rows whose keywords have the greatest nr of words in common with the searchphrase

There are several improvements possible. fx it is a good idea to remove punctuation in the phrase, and also to remove the 'dosent matter' words like 'a', 'an', 'I', 'the'.

still all that is refinement; the basic idea is abowe.

regards JakobA
0
JakobACommented:
The abow may or may not be a relevant response. I am quite confused by these costs you tak about.

cost to you ?  practically none, CPU power is cheap

cost to the sponsor ? You will need to explain how that is calculated, mostly I would go with "what the market will bear"

cost pr word ? as with CPU power, words are cheap ;-))
0
sabecsAuthor Commented:
Thanks very much for your help JakobA, you have clarified quite a few things for me, I should now be able to achieve what I require.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.