Solved

PHP or MySQL query to combine and total 2 tables.

Posted on 2007-04-01
6
201 Views
Last Modified: 2008-02-01
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');

0
Comment
Question by:sabecs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 15

Accepted Solution

by:
JakobA earned 500 total points
ID: 18832510
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
 

Author Comment

by:sabecs
ID: 18835578
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
 

Author Comment

by:sabecs
ID: 18839449
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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 15

Expert Comment

by:JakobA
ID: 18839780
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
 
LVL 15

Expert Comment

by:JakobA
ID: 18839811
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
 

Author Comment

by:sabecs
ID: 18843365
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question