473,385 Members | 1,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Finetuning mysql query!

I already got some good help earlier, but still need some..:-)

I have tables like
ADVERTISERS:
advertiser_id
advertiser_name etc...

ADS:
ad_id
ad_advertiser_id
ad_status (0 or 1 where 1 means visible)
ad_duration (usually 6 months expressed in seconds = 15811200)
ad_start (php function time() set during first publishment)
ad_text etc...

In same query i want to get
- all information about advertisers and
- number of visible, no-outdated ads that they have.

It would be a lot easier if everybody had at least one ad and active state
of ads wouldn't matter. But that's not the case and now I have to do JOIN
and this and that.

************************************************** **
This ALMOST works but counting only active, no-outdated ads doesn't work:

$now=time();
$query =" SELECT
IF ((ads.ad_id <> NULL AND ads.ad_status=1 AND (ad_duration + ad_start) >
'$now'), count( * ), 0) AS nr_of ads,
advertisers.*
FROM advertisers
LEFT OUTER JOIN ads ON ad_id=ad_advertised_id
GROUP BY advertiser_id
ORDER BY advertiser_name
");
Jul 20 '05 #1
3 1247
Actually current test is, but doesnt' work right:

$now=time();
$query =" SELECT
IF ((ads.ad_id <> NULL AND ads.ad_status=1 AND (ad_duration + ad_start) >
'$now'), count( * ), 0) AS nr_of ads,
advertisers.*
FROM advertisers
LEFT OUTER JOIN ads ON advertiser_id=ad_advertised_id
GROUP BY advertiser_id
ORDER BY advertiser_name
");
Jul 20 '05 #2
Ok, I found the solution already.
Jul 20 '05 #3
Perttu Pulkkinen wrote:
$now=time();
$query =" SELECT
IF ((ads.ad_id <> NULL AND ads.ad_status=1 AND (ad_duration + ad_start) >
'$now'), count( * ), 0) AS nr_of ads,


Not relevant to your problem but...

You do know that MySQL has now() function, which return current date and
time, and also curdate() and curtime() which return only the current
date or time.

So instead using $now() = time(); You could just say
"select * from tablename where timecolumn=curtime()";
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Lenz Grimmer | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, MySQL 4.0.14, a new version of the popular Open Source/Free Software Database, has been released. It is now available in source and binary...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
1
by: Saqib Ali | last post by:
I have created 2 tables in my MySQL database. A_TAB and B_TAB. They have auto-incrementing integer primary keys respectively named A_ID & B_ID. When I created B_TAB, I declared a field named A_ID...
1
by: jlee | last post by:
I'm pretty much a newbie on mysql, and I need some help. I am running mysql Ver 12.22 Distrib 4.0.24, for portbld-freebsd5.4 (i386) on a server hosting an active website. The site's developer...
1
by: Good Man | last post by:
Hi there I've noticed some very weird things happening with my current MySQL setup on my XP Laptop, a development machine. For a while, I have been trying to get the MySQL cache to work....
3
by: Juan Antonio Villa | last post by:
Hello, I'm having a problem replicating a simple database using the binary log replication, here is the problem: When the master sends an update to the slave, an example update reads as follows:...
1
by: Ike | last post by:
Recently, I began using a different MySQL verver (i.e. different machine as well as different version#, going from 4.12a to 4.1.9 max). The following query used to work: select firstname,...
3
by: Me Alone | last post by:
Hello: I am trying to edit some C code I found in "The definitive guide to using, programming, and administering MySQL" by Paul DuBois. This C client program connects and then segfaults when...
221
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.