473,486 Members | 2,401 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

WHERE clause

155 New Member
I'm running a small classified ads site and I display a list of categories with the number of ads listed under it.

The ads stay in the database for 40 days and then they are deleted using a cron script. However, they're only displayed for 30 days. The extra 10 days are to give the owner time to renew the ad whereby it'll be displayed for another 30 days. This part I have no trouble with because I have this in the query:

Expand|Select|Wrap|Line Numbers
  1. FROM ads WHERE cat_name='$cat_name' &&
  2. submitted > SUBDATE(NOW(), INTERVAL 30 DAY)
The problem I'm having is with the part I mention first, displaying the number of ads next to the category name that the ad is filed under. I'm using this:

Expand|Select|Wrap|Line Numbers
  1. $query = "SELECT c.cat_id, c.cat_name, COUNT(d.cat_name)
  2. as theCount FROM class_categories AS c LEFT OUTER JOIN ads AS
  3. d ON c.cat_name = d.cat_name GROUP BY c.cat_name";
However, this number includes the ads that aren't currently displayed since they're over 30 days old. I've tried using this:

Expand|Select|Wrap|Line Numbers
  1. $query = "SELECT c.cat_id, c.cat_name, COUNT(d.cat_name)
  2. as theCount FROM class_categories AS c LEFT OUTER JOIN ads AS
  3. d ON c.cat_name = d.cat_name WHERE cat_name='$cat_name' &&
  4. submitted > SUBDATE(NOW(), INTERVAL 30 DAY)
  5. GROUP BY c.cat_name";
But, it doesn't work. It only displays the first category name that has an ad under it.

Any idea how I can get it to display the number of ads (that are within the 30 day display time limit) that each category has?

Thanks
David
Jan 7 '09 #1
2 1382
Atli
5,058 Recognized Expert Expert
Hi.

You can specify more than one condition for joins, just as you would with a WITH clause.

So, you could do:
Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. FROM a
  3. INNER JOIN b
  4.   ON a.id = b.a_id
  5.   AND b.a_id Mod 2 = 0
  6.  
And the table would only display rows where the two tables are linked together AND where the id in table a is an even number.

You should be able to add your date limitation to your join in the same way.
Jan 7 '09 #2
DavidPr
155 New Member
Atli,

Thanks, works great!

Expand|Select|Wrap|Line Numbers
  1. $query = "SELECT c.cat_id, c.cat_name, COUNT(d.cat_name)
  2. as theCount FROM class_categories AS c LEFT OUTER JOIN ads AS
  3. d ON c.cat_name = d.cat_name
  4. AND submitted > SUBDATE(NOW(), INTERVAL 30 DAY)
  5. GROUP BY c.cat_name";
Jan 7 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

47
3572
by: Andrey Tatarinov | last post by:
Hi. It would be great to be able to reverse usage/definition parts in haskell-way with "where" keyword. Since Python 3 would miss lambda, that would be extremly useful for creating readable...
2
2123
by: steve | last post by:
To gain performance, do I need to index ALL the fields in the where clause. Say we have a query like: select stuff from table where field1=.. and field2=... If field1 selection substantially...
3
21977
by: A.V.C. | last post by:
Hello, I found members of this group very helpful for my last queries. Have one problem with CASE. I can use the column name alias in Order By Clause but unable to use it in WHERE CLAUSE. PLS...
0
1610
by: jobs | last post by:
I have a gridview that times when selecting from specific table when I try to when I add a where clause. Even when I'm only seleting Top 1 which comes right back at the command line. I test the...
2
10725
by: Jim.Mueksch | last post by:
I am having a problem with using calculated values in a WHERE clause. My query is below. DB2 gives me this error message: Error: SQL0206N "APPRAISAL_LESS_PRICE" is not valid in the context where...
9
19113
by: Emin | last post by:
Dear Experts, I have a fairly simple query in which adding a where clause slows things down by at least a factor of 100. The following is the slow version of the query ...
8
3462
by: chrisdavis | last post by:
I'm trying to filter by query or put those values in a distinct query in a where clause in some sort of list that it goes through but NOT at the same time. Example: ROW1 ROW2 ROW3 ROW4 ,...
5
2646
by: pwiegers | last post by:
Hi, I'm trying to use the result of a conditional statement in a where clause, but i'm getting 1)nowhere 2) desperate :-) The query is simple: -------- SELECT idUser,...
4
14237
by: Jane T | last post by:
I appreciate how difficult it is to resolve a problem without all the information but maybe someone has come across a similar problem. I have an 'extract' table which has 1853 rows when I ask for...
12
25066
by: =?ISO-8859-1?Q?Ren=E9?= | last post by:
Hi, is there a rule of thumb what is better/faster/more performant in SQL Server 2005? a) SELECT * FROM A INNER JOIN B ON B.ID = A.ID AND B.Cond1 = 1 AND B.Cond2 = 2 b) SELECT * FROM A INNER...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7132
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7180
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5439
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4870
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1381
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
266
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.