Solved

PHP or MySQL query to combine and total 2 tables.

Posted on 2007-04-01
6
202 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
Webinar: MariaDB® Server 10.2: The Complete Guide

Join Percona’s Chief Evangelist, Colin Charles as he presents MariaDB Server 10.2: The Complete Guide on Tuesday, June 27, 2017 at 7:00 am PDT / 10:00 am EDT (UTC-7).

 
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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

690 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