O
O
Oleksandr2015-02-16 18:53:27
SQL
Oleksandr, 2015-02-16 18:53:27

What is the error in this SQL query?

When executing a request in PHPMyAdmin, a hang occurs.
PHPMyAdmin doesn't give any messages, it just shows a "Loading" message, you have to kill the process.

SELECT download_count.id,
date_format(start_date, '%D %b %Y') as 'start',
date_format(max(wp1.tracking_date),'%M') as 'mouth', 
date_format(max(wp1.tracking_date),'%D %b %Y') as 'last',
COUNT(wp1.download_id) as 'last_mouth_count',
COUNT(wp2.download_id) as 'today_count'
FROM download_count
 LEFT JOIN download_info as wp1 on download_count.id=wp1.download_id AND NOT ISNULL(wp1.tracking_date) AND date_format(wp1.tracking_date, '%Y-%m')=date_format(now(), '%Y-%m')
 LEFT  JOIN download_info as wp2 on download_count.id=wp2.download_id AND NOT ISNULL(wp2.tracking_date) AND  date_format(wp2.tracking_date, '%Y-%m-%d')=date_format(now(), '%Y-%m-%d')
WHERE download_count.id=wp2.download_id AND download_count.id=wp1.download_id

download_count table
CREATE TABLE `download_count` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(150) NOT NULL,
  `url` varchar(150) NOT NULL,
  `full` int(1) NOT NULL DEFAULT '0',
  `plugin_id` int(4) unsigned NOT NULL,
  `start_date` datetime NOT NULL,
  `used_date` datetime DEFAULT NULL,
  `count` bigint(20) NOT NULL DEFAULT '0',
  `cat_id` int(11) NOT NULL DEFAULT '1',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=87 DEFAULT CHARSET=utf8;

download_info table
CREATE TABLE `download_info` (
  `tracking_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `download_id` bigint(20) unsigned NOT NULL,
  `tracking_referer` varchar(355) DEFAULT NULL,
  `tracking_date` datetime NOT NULL,
  `user_id` bigint(20) unsigned NOT NULL DEFAULT '0',
  `user_refferal` varchar(350) DEFAULT NULL,
  `additional_info` varchar(32) DEFAULT NULL,
  `version_id` bigint(20) unsigned NOT NULL,
  PRIMARY KEY (`tracking_id`),
  KEY `download_id` (`download_id`)
) ENGINE=InnoDB AUTO_INCREMENT=50243 DEFAULT CHARSET=utf8;

Thank you.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Aksentiev, 2015-02-16
@Sanasol

What is the error in this SQL query?

You tell us this.
Does an error occur?
So far, I see an error only in the fact that the month was called a mouth.

A
Alexey Yakhnenko, 2015-02-16
@ayahnenko

WHERE download_count.id=wp2.download_id AND download_count.id=wp1.download_id
why are join conditions duplicated here?

O
Oleksandr, 2015-02-16
@Track77

Thanks to all.
So the problem turned out to be either in exceeding the memory limit, or some other hoster restrictions.
Decided by replacing the second JOIN with SELECT.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question