MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 5th, 2000, 06:31 AM
forefront forefront is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2000
Posts: 165 forefront User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 55 m 52 sec
Reputation Power: 9
Questions on SQL query construction

1. I have an SQL query (say, "SELECT * from oink WHERE blah = 'foo' AND foo = 'oink' ORDER BY lala DESC") and wish to add an additional clause of "...for all articles before 'date'"
2. Ditto if I wanted to "between 'date1' and 'date2'"
3. Is there a way to say do 2. and add a parameter where date is "2 months previous to current month" (I want to publish 1 year's articles, sorted by month, and obviously that is a rolling figure, not fixed).

How would I construct those queries, please?

Finally, if a record has "NULL" in the "expiry" (date) field would that be interpreted as "never expiring" - if not how would I make a field that defaults to never expiring?


Thank you.
David

Reply With Quote
  #2  
Old October 5th, 2000, 09:01 AM
rod k rod k is offline
Apprentice Deity
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jul 1999
Location: Niagara Falls (On the wrong side of the gorge)
Posts: 3,237 rod k User rank is Private First Class (20 - 50 Reputation Level)rod k User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 m 8 sec
Reputation Power: 13
Send a message via AIM to rod k
All of this depends on how you stored the date. If you used one of mysql's standard date fields it's fairly easy.

1. "select * from oink where blah='foo' and foo='oink' and date_field<'$date' order by lala desc" as long as $date is in an acceptable format (see the mysql manual) OR an easy way is to generate a unix timestamp for $date and replace date_field<'$date' with date_field<from_unixtime($date)

2. "select * from oink where blah='foo' and foo='oink' and date_field between '$date1' AND '$date2' order by lala desc" same comments apply.

3. "select * from oink where blah='foo' and foo='oink' and month(date_field)=month(subdate(now(), interval 2 month)) order by lala desc"

The question about never expiring is not easy. Mysql doesn't expire records, that's up to you. First, you have to determine what you mean by never expiring. Do you want those records to always be returned??? I don't see what you mean by expiring as far as the queries you've asked about since they seem more geared towards a publish date not an expiry date. If you have a seperate field for expiry and would be querying for expired in a seperate way, I would just set the date year to 9999 which mysql supports. It wouldn't require any additional logic in the query. Since an expiry type query would look like:

select * from query where expiry_date>now()

and wouldn't be subject to between, etc.

Reply With Quote
  #3  
Old October 5th, 2000, 09:23 AM
forefront forefront is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2000
Posts: 165 forefront User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 55 m 52 sec
Reputation Power: 9
RE: "never expiring"

Apologies for ambiguity!

This is an article publishing system where we have a field called "created" and one called "expiry" - I want to have records stay valid forever by default in the "expiry" field and only where I set that field with a date will the record "expire"

Thanks

Reply With Quote
  #4  
Old October 6th, 2000, 05:08 AM
Sepodati's Avatar
Sepodati Sepodati is offline
Banned
Dev Shed God 19th Plane (14000 - 14499 posts)
 
Join Date: Dec 1999
Location: Afghanistan
Posts: 14,381 Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 70626 Folding Title: Intermediate FolderFolding Points: 70626 Folding Title: Intermediate FolderFolding Points: 70626 Folding Title: Intermediate FolderFolding Points: 70626 Folding Title: Intermediate Folder
Time spent in forums: 2 Months 4 Weeks 7 h 28 m 9 sec
Reputation Power: 1784
Send a message via ICQ to Sepodati Send a message via Yahoo to Sepodati
So if it never "expires" you want it to always show up in the listing, right? No matter what month it is, this record will always show?

Just do like rod said and make the year 9999. Then it'll always a value greater than 2 months ago, and it'll always show up.

When it gets to the year 10000, you'll probably have to download a new version of MySQL. Just keep that in mind

---JH

------------------
*************************************************************
* The manual can probably answer 90% of your questions... *
* *
* PHP Manual. www.php.net/manual *
* MySQL Manual: www.mysql.com/documentation/mysql/bychapter *
*************************************************************

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Help with constructing SQL queries


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway