Solved

PHP or MySQL query to combine and total 2 tables.

Posted on 2007-04-01
6
193 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
  • 3
  • 3
6 Comments
 
LVL 15

Accepted Solution

by:
JakobA earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 15

Expert Comment

by:JakobA
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now