Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8754475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:37:45+00:00 2026-06-13T13:37:45+00:00

I have 2 tables with the following structures: CREATE TABLE IF NOT EXISTS `campaigns`

  • 0

I have 2 tables with the following structures:

CREATE TABLE IF NOT EXISTS `campaigns` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

CREATE TABLE IF NOT EXISTS `campaign_stats` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `id_campaign` int(11) NOT NULL,
  `day` int(11) NOT NULL,
  `cost` varchar(255) NOT NULL,
  `conv` int(11) NOT NULL,
  `cost_conv` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1457 ;

Now the cost and cost_conv fields are dollar values such as 12.13 or 143.00.

The day field contains a unix timestamp stored as an INT.

What I am trying to do is get the sum of the cost, conv, and cost_conv field’s for yesterday and output them into an HTML table.

Here is my code:

<h2>Yesterday</h2>

<?php

$sql = mysql_query("
                    select c.name,
                    (select sum(conv) from campaign_stats where id_campaign=c.id and day=date(from_unixtime(day) - interval 1 day)) as num_conv,
                    (select sum(cast(cost as float)) from campaign_stats where id_campaign=c.id and day=date(from_unixtime(day) - interval 1 day)) as num_cost,
                    (select sum(cast(cost_conv as float)) from campaign_stats where id_campaign=c.id and day=date(from_unixtime(day) - interval 1 day)) as num_cost_conv
                    from campaigns as c
                    order by c.name
                    ") or die(mysql_error());

if (mysql_num_rows($sql) == 0)
{
    echo '<p>No campaign information to display.</p>';
}
else
{
    echo '<table cellpadding="10" cellspacing="1" border="0" width="100%">';
    echo '<tr>';
    echo '<th>Account</th>';
    echo '<th>Conversions</th>';
    echo '<th>Cost</th>';
    echo '<th>Cost per Conversion</th>';
    echo '</tr>';

    while ($row = mysql_fetch_assoc($sql))
    {
        foreach ($row as $k => $v)
            $$k = htmlspecialchars($v, ENT_QUOTES);

        echo '<tr class="'.(($count % 2) ? 'row1' : 'row2' ).'">';
        echo '<td>'.$name.'</td>';
        echo '<td>'.$num_conv.'</td>';
        echo '<td>'.$num_cost.'</td>';
        echo '<td>'.$num_cost_conv.'</td>';
        echo '</tr>';

        $count++;
    }

    echo '</table>';
}

?>

It is giving me the following error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'float)) from campaign_stats where id_campaign=c.id and day=date(from_unixtime(da' at line 2

Honestly I am not even sure if I wrote the query properly. Any help would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:37:47+00:00Added an answer on June 13, 2026 at 1:37 pm

    First of all, I don’t like how your query is written, you could replace subqueries with a single join. Your query could be something like this:

    select
      campaigns.name,
      sum(campaign_stats.conv) as num_conv,
      sum(cast(campaign_stats.cost as float)) as num_cost,
      sum(cast(campaign_stats.cost_conv as float)) as num_cost_conv
    from
      campaigns inner join campaign_stats on campaigns.id=campaign_stats.id_campaign
    where
      date(campaign_stats.day) = date_sub(date(now()),interval 1 day)
    group by
      campaigns.name
    order by
      campaigns.name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following table structures CREATE TABLE [dbo].[WorkItem]( [WorkItemId] [int] IDENTITY(1,1) NOT NULL,
I have the following structure: CREATE TABLE IF NOT EXISTS `myTable` ( `ID` int(11)
I have the following structure SET SQL_MODE=NO_AUTO_VALUE_ON_ZERO; CREATE TABLE IF NOT EXISTS `sis_param_tax` (
I'm using EF v1. I have following tables: CREATE TABLE category ( category_id int
Suppose we have 2 tables, a and b: CREATE TABLE a (id_a INT NOT
Given the following table structure: CREATE TABLE IF NOT EXISTS `roles` ( `id` int(11)
I have the following table structure: CREATE TABLE a ( a_id int(10) unsigned NOT
I have DB2 table having following structure CREATE TABLE DUMMY ( ID CHARACTER(10) NOT
Lets say I have the following MySQL structure: CREATE TABLE `domains` ( `id` INT(10)
I have two tables with the following (simplified) structures: table "Factors" which holds data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.