Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    MYSQL




Select Query Between Two Dates


select * from table where Fri, 23 May between sdate and enddate

Fri, 23 May is user given date

table likes
sno | sdate | enddate
1 | Mon, 19 May | Sat, 24 May
2 | Sat, 17 May | Mon, 19 May
3 | Fri, 23 May | Mon, 26 May


so the above queries i need the 1 and 3 results only

what is the problem in query and how to change it
give solutions.




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Dates In The Past 3 Weeks (was "SELECT Query Help")
I'm trying to select data from a table that has been submitted in the past 3 weeks. I am not having any success.

Select Dates
I need to select all dates between (and including) two given dates.
something like

SELECT DATE() BETWEEN '(dynamic date)' AND DATE_ADD('(dynamic date)', INTERVAL 90 DAY)

so that the result set would contain 90 days....
Many thanks in advance for your help.

Select Dates After A Specified Interval
The code i have come up with thus far is as follows:

SET @StartDate='2005-11-22', @StopDate1='2007-11-22', @date1= '2006-01-31'; #Note: do not set StopTime greater than 23:59:59

SELECT
Table1.StopDate As Date

FROM dbase1.Table1
where StopDate= @date1
@date1 = DATE_ADD('2006-01-31', Interval 2 DAY)
WHERE ((dbase1.Table1.StopDate)>= @StartDate) AND ((dbase1.Table1.StopDate)<= @StopDate1)
GROUP BY Table1.StopDate;

date1 is the first date in the series.

Can someone point out where i am going wrong or if there is an easier way to do this?

Select Range Of Dates
How can I select a continuous range of dates? For example If i want all of the dates between Jan 01 2007 and Apr 02 2007 to be listed as:

jan-01-2007
Jan-02-2007
jan-03-2007
..
Apr-01-2007
Apr-02-2007

What Select statement could I use? The date format is not important.

Select Fields Between 2 Dates
I am trying to develop a booking system in PHP with MySQL but I am having trouble writing some code that checks to see if a new booking clashes with another. e.g. in the database I have a booking that runs from 2008-03-10 to 2008-03-20. When I add a booking I want to check to see if a booking already exists between these dates. Is this possible?

I am not an expert with MySQL but the closest I got is to carry out 2 query's:

SELECT propertyid, arrival_date, departure_date FROM bookings WHERE arrival_date BETWEEN '$arrival_date' AND '$departure_date'

SELECT propertyid, arrival_date, departure_date FROM bookings WHERE departure_date BETWEEN '$arrival_date' AND '$departure_date'

but this obviously will not check if a booking is made in between these dates, e.g. if I try and add a booking from 2008-03-13 to 2008-03-18 it will not think that there is a collision of booking.

Retrieving Dates In SELECT
Im trying to select some results from a query and use a WHERE clause to extract ones that were only created greater than July 1st 2007.

In the table, there is a 'created' column which stores a unix timestamp. The sql im using is

"SELECT post,created FROM table1 WHERE created >= 1183348800"

It seems that the WHERE clause is just comparing the acutal number and not realizing that it is a unix date.Is there a function that I am able to use?

Who Do I Write Select With Dates And Times
i have to cols, date and time. who can i do, SELECT between (date 1,time 1) to (date 2, time 2)?

for example i want to get the recordes between 2007-01-01 23:00:00 to 2007-01-03 03:00:00 ?

SELECT Date Records Between Two Given Dates.
Let's say i have a job posting site and a user wants to see the jobs posted between dates.... how would i do that?

here's how a table would look like(sample):

table: Jobs
fields:
JobID
JobStatus
DatePosted
DateToDeactivate

and when i build the query i would make it something like this:

"Select * from Jobs Where JobID = '123' and ( DatePosted BETWEEN 2003-05-08 12:14:37 AND 2004-06-08 12:14:37)"

i know that this does not work! how can i do this.

Select All Dates Since First Of Current Month
I'd like to write a query to select all records entered since the first of the current month (also, a query for all records entered during the previous month). A "date_in" field is being populated using the Now() function.

Using Dates To Query
I have a table with a field for each record with a date in it. I want to get all records before September 12,2000 but I don't know what the query should be.

Use Between In Dates Query
can I safely use between with dates as in:

select * from table where CURRENT_DATE() BETWEEN LASTMONTHDATE AND NEXTMONTHDATE

Query Dates
I am trying to find records where my approved date is not NULL and my shipped date is NULL. Using phpMyAdmin the records show cells with italic NULL and others with dates ie. 2006-06-11, so I assume this is formated right.
However using this query I get no records.
SELECT * FROM records WHERE approved_date != NULL AND shipped_date = NULL
I have used phpMyAdmin's search section with every conceivable way and I can't get any proper results.

Query For Dates
Table Data:
-----------

id__name___from_________to_______
-------------------------------------
1___ABC___2006-10-01___2006-10-31
2___CDE___2006-11-01___2006-11-31


is there a syntax to select both rows at once?

i.e. if I query for dates from 2006-10-15 to 2006-11-15, is there a possibility to have the two rows as a result of a single SQL Statement query?

Query Using Dates
I'm having trouble figuring out how to get two time periods in one query. Basically it's either one month at a time or a full year...So I need to get everything that starts and stops this month, or cases where this month falls between the start and stop dates. I couldn't figure out how to do the between bit, so I settled for calling an expiration date that is not older than this month.I get partially correct results. It's calling the five ads that start on 2006-12-01 and end 2006-12-31, and ignoring the ones that start 2007-01-01 and end 2007-01-31. However, I've got a test ad that starts 2006-01-01 and ends 2006-12-31 that it's ignoring...I think my timeframes are cancelling each other out or something.

$sql = "SELECT ad_id, ad_filename,rally,affiliates,past,upcoming,start_date FROM ads WHERE Month(start_date)='$curmonth' AND Year(start_date)='$curyear' OR Month(end_date)>='$curmonth' AND Year(end_date)>='$curyear'ORDER BY rally='y' DESC";

Comparing Dates Y, M, D , Time In Sql Query
ok.. ive finally got my nice little calender setup to output my dates

now

how do i query my database

for example ive got 2 variables
$startdate = 2006-1-1 00:00:01
$enddate = 2006-1-1 23:59:59

these represent the 1st and last second of the first of january 2006

i have a cell in my table (table called challenges) ( cell called time) that contains a date in the same format for each entry

how do i get all the entries between the start time and the end time..

can i use less than < and more than > as these are not really integar values

the column type is datetime but there is also another column of type datetime

Query Dates Within 1 Week From A Date
I'm fairly new to PHP and have done some basic work with mysql but am not sure how to approach this. I'm trying to figure out how to create a query with PHP to get all rows with a datetime from 1 week ago to the current timedate.

Note: I'm also not sure how to find a date for 1 week ago with PHP. Any help would be great.

Creating A Query Based On Dates
I am trying to write a query (in PHP) which selects from a database all of the items which are in the future. My query is as follows

SELECT * FROM news WHERE ((news.date)>$today ORDER BY date

where news is my database, news.date is the the field which holds the date for the item and $today will be replaced my current date. At the moment it seems to display all values, which suggest its not functioning properly.

Creating A Query Based On Dates
I am trying to write a query (in PHP) which selects from a database all of the items which are in the future. My query is as follows

SELECT * FROM news WHERE ((news.date)>$today ORDER BY date

where news is my database, news.date is the the field which holds the date for the item and $today will be replaced my current date. At the moment it seems to display all values, which suggest its not functioning properly.

Creating A Query Based On Dates
I am trying to write a query (in PHP) which selects from a database all of
the items which are in the future. My query is as follows

SELECT * FROM news WHERE ((news.date)>$today ORDER BY date

where news is my database, news.date is the the field which holds the date
for the item and $today will be replaced my current date.
At the moment it seems to display all values, which suggest its not
functioning properly.

Query By Dates To Find History Records
what is the correct way to query on a date field in a table to pull a value that is older than 3 days?

Date field format is 2007-04-04

I only want records where the date is 2007-04-01 or earlier.

Converting MS Access Dates To MySQL Dates During LOAD DATA INPUT ?
My insert code is below. The MySQL server is on my local machine and is version 4.1.22. I am exporting from an access table with 6 fields to a mysql table with those 6 plus 5 more fields. The main problem I am having SO FAR, is converting dates. The data file dates are formated like:
2/2/2006 0:00:00, 12/20/2006 9:22:05

Any ideas how to format those into mysql friendly dates?

Also, how do I convert currency fields in the load data process? I know you use SET by what kind of formula?

===========================================

LOAD DATA INFILE 'C:Documents and Settingspath_to_filedata.txt'
INTO TABLE auto
(field2, field3, field4, field5, currency_field6, field7, date_field8, date_field9)
SET id = MD5(UUID())
FIELDS TERMINATED BY ','
LINES TERMINATED BY '
'

How To List All Missing Dates Between 2 Dates
I have a database which stored financial data daily exclude weekend, however sometimes i tend to forget to upload data into the database. How do i write a sql query that would detect the missing dates between from last updated date until the current date.

Stroring Dates Perior To 1 AD, BC Dates
I need to store some dates that can be BC, However I received a problem indicating Incorrect Datetime.

I have read the documentation, and it says that Date&DateTime types can store from 1000-9999 AD, and Timestamp stores less year ranges. What other options do I have to store dates before 1000 AD !

List All Dates Between 2 Diff Dates
I have a database which stored financial data daily exclude weekend, however sometimes i tend to forget to upload data into the database. How do i write a sql query that would detect the missing dates between from last updated date until the current date.

Dates, Dates, Dates - Syntax Help Please!
OK, I have a MySQL table which contains, among others, a field called date which stores dates in the format YYYY-MM-DD HH:MM:SS.

What I'd like to do is select a list of all the months present (without repetition), preferably in the format YYYY-mm

Select Query With Sub-query For Count And Max
I have a table "test"... as below... I want to have a result set of all the parents with the count of their children and the lastest created date of their child ...

Select Query Help
Okay, so I'm no SQL guru and I've run into a problem while working on building a query for one of my PHP applications. Here is the query in question.

PHP

SELECT
    details.salute_url,
    details.about_me,
    details.html_header,
    details.html_footer,
    details.ads,
    details.publisher_id,
    details.channel,
    details.border_colour,
    details.title_colour,
    details.background_colour,
    details.text_colour,
    details.url_colour,
    main.profile_perm,
    friends.ref_id AS friends_check
FROM
    isps_details AS details,
    isps_main AS main,
    isps_friends AS friends
WHERE
    details.uid=&#391;' AND
    main.uid=&#391;' AND
    friends.uid=&#391;' AND
    friends.friend_id=?' AND
    friends.pending=&#390;' AND
    friends.pending_3pt=&#390;'
LIMIT 0, 1


What this query is supposed to do is pull some information on a user from "isps_details" and some more information from "isps_main". Then it is supposed to pull up some info from the "isps_friends" table which is used to determine whether the current user(friends.friend_id) is "friends" with the owner of the current page(friends.uid).

The thing is, sometimes the user may not be friends with the current page owner and as a result the friends part of the query will be false and as a result of that the entire query returns nothing. Is there anyway to modify my query in order for it to still pull up the records for the other part of the query even if the friend's WHERE statment is false?

Again, I'm not too good with mySQL so any help is appreciated!

NB: I'm on mySQL 4.0.27-standard

Need Help With A SELECT Query
I have two tables.

Table 1 = ContentWords
Table 2 = TitleWords

The tables have both each two fields named wordId and articleId. The wordId field references a wordId in a table named Words which contains the fields wordId and word.

As you might understand I use these tables (ContentWords and TitleWords) to index words that appear in articles to use with a search function.

It's easy to construct a search query to have it select a articleId from either ContentWords or TitleWord (SELECT articleId FROM ContentWords/TitleWords WHERE wordId = $var).

But if I want to search if a word exists in both the Content and Title? I've constructed a SELECT query that kind off works:



SELECT ContentWords.articleId AS ContentArticleId, TitleWords.articleId AS TitleArticleId
FROM `ContentWords` , `TitleWords`
WHERE ContentWords.wordId = TitleWords.wordId AND ContentWords.wordId = $var AND TitleWords.wordId = $var
This will output something like:



ContentArticleId TitleArticleId
53 76
77 76
That means the word exists in the content in article 53 and 77. The word also exists in the Title in article 76.

What I actually want is only ONE field named ArticleId with the result of 3 rows (53,76 and 77).

Is this possible?

Thanks in advance!

Help With SELECT Query
I am having trouble coming up with the proper SQL that will get me a certain result.

i have a table that looks something like this:

request_id | member_id | Club_id

1 | 10 | 12
2 | 10 | 12
3 | 10 | 142
4 | 10 | 142
5 | 10 | 72

I am trying to come up with a query that will give me the number of times that member #10 chose each different club.

ex:
club #12 - 2 times
club #142 - 2 times
club #72 - 1 time

i have tried a few things but everything i have tried with DISTINCT, or COUNT gives me 5 (total requests). And it is late in the day and my brain is a bit fried.

SELECT Query
Is it possible to SELECT from a table but only retrieve part of a field value?

I have a table that contains stock data, the field in question is the description.
I would like to only return the first 100 characters of the field rather than it's full contents.

Using LIKE In A Select Query -
I am trying to query 2 tables. One has a field, County_name which includes a list of counties and the state they are associated with ie. MT-Missoula

The other table is a list of sites and has a State field. I would like to bring up a list from the County_names field that begin with a State that matches the value in the Site State field. I can't figure this out, can anyone help a new mysql user?

$countyresults=mysql_query("SELECT ct.*, s.State, s.Site_ID
FROM county_tbl AS ct, site_tbl AS s
WHERE s.Site_ID='$ID'
AND ct.County_name LIKE 's.State%'
ORDER BY County_name");

Select Query Help Please
The below query works well for returning 1 result however I now need to expand it to return multiple results. At the moment I feed it 1 car type and 1 company id and it spits out one result, I then loop the query to get the number of results I need - but this is very inefficiant so I want to change it so I pass all my variables at once.

So where I have cars.type =&#391;' I want to have car.type =&#391;' or cars.type=&#392;' which in itself works but how to I extend the company section as when I add the extra 'Or's' in there the query goes wacky and spits out hundreds of results which are all the same - I think its something to do with the inner join but im not sure.

MySQL
SELECT prices.single_f,
prices.return_f,
prices.vehicle,
resort.company_id,
resort.resort,
cars.type,
cars.description,
cars.long_description,
cars.image,
surcharges.type,
surcharges.adult,
surcharges.kids,
prices.timefrom,
prices.timeto,
prices.resort_id,
cars.min,
cars.max,
company.is_enabled
FROM prices
INNER JOIN resort ON resort.id = prices.resort_id
AND resort.company_id = &#391;'
AND resort.resort = 'Acoteias'
INNER JOIN company ON company.id = &#391;'
INNER JOIN cars ON cars.type = prices.vehicle
AND cars.type = &#392;'
LEFT JOIN surcharges ON surcharges.resort_id = prices.resort_id
WHERE prices.break <= ïb'
AND company.is_enabled = 'Yes'
AND (
(
prices.timefrom <= prices.timeto
AND prices.timefrom <= &#3905;:00:00'
AND prices.timeto >= &#3905;:00:00'
)
OR (
prices.timefrom >= prices.timeto
AND prices.timefrom <= &#3905;:00:00'
AND &#3923;:59:59' >= &#3905;:00:00'
)
OR (
prices.timefrom >= prices.timeto
AND &#3900;:00:00' <= &#3905;:00:00'
AND prices.timeto >= &#3905;:00:00'
)
)
LIMIT 0 , 30

Select Query Help
How do i query something like:

select id from table-name where number=1234 , 123123 , 32123 , 123123 , 123123 , 123123

id & number is int variable.

it works if i do it one at a time.

eg: select id from table-name where number=1234;

and so on ....

Can't Use Select...IN Inner Query
I'm trying to make the following SQL query and keep getting errors:

select * from mytbl where reportid in (select reportid from mytbl where level>60)

I get this 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 'select reportid from mytbl )' at line 1

When I'm running the inner query (select reportid from mytbl where level>60) it is running OK. Also, when I'm running without inner query (select * from mytbl where reportid in (1,2,3)) itis running Ok.

But Somehow I can't run it together.

I'm using MySQL 4.0.14.

Using IF In A Select Query
I want to say in mysql that if a field is NULL, then ignore it in a select query statement like this -

$result = mysql_query("SELECT
                id,
                head,
                IF(blurb IS NULL, SUBSTRING(story, 1, (locate( ',', story, 50)))) AS blurb, etc etc

Is there a way?

Select Query
use a select statement in a update query but it doesnt like the select bit... here is what i have

$query = "UPDATE virtual_bc SET assigned = 'p' WHERE virtual_id IN ('SELECT * FROM virtual_bc WHERE assigned = 'n' ORDER BY virtual_id LIMIT 1')";
$result = mysql_query($query) or die(mysql_error());

Select Query
I'm trying to get a select query working but am having problems. I have a table that has survey anwer results. The two items each entry has that I need to pay attention to are the user who entered the info and the survey they were taking. How can I write a select statement that will check if a user answered more than one survey? One survey is easy:

SELECT distinct userID from surveyResults where surveyID='11'

But what do I do if I want to see who took survey 11, 12, and 13?

SELECT Query
When using a simple SELECT query, how to make all the rows in the resultset to be numbered?

Let's assume that a query resulted in 1000 rows - I want all of them to have a number (1 to 1000) in front of them (in a special column) to be able to read what item is on row 354 and what is on row 767 (for example). With another sorting (using ORDER BY) the sorted data should be again numbered from 1 to 1000 in an ascending way (the number is not firmly assorted to a special row, it changes depending on the sorting order).

Select Query
The below query is pulling all records no matter what value 'LOCATION' holds. LOCATION holds the users dept. and is fine so I think it is a problem with the conditions. I'm trying to selct all records "where nsi.orderid= '$orderid' || nsi.recipient= '$orderid'" but nsi.location=LOCATION.

SELECT * FROM nsi
where nsi.orderid= '$orderid'
|| nsi.recipient= '$orderid'
AND nsi.location='".LOCATION."'
ORDER BY orderid"

Select Query
I have a table that has a column for surnames and another with firstnames, among others. What I am trying to do is a "select query" to drag out all the unique people sharing the same surname. For Eg, if I have Jo Blogs, Jim Blogs and Edna Blogs each with 50 rows of data, I only want to display the people that have the lastname "blogs", like Jo,Jim and Edna with no other data, just the three lines. The closest I have come is as follows:

 SELECT        
m1.firstname,m2.firstname        
FROM        
  Member m1        
INNER        
  JOIN        
    Member m2        
    ON ( m1.firstname != m2.firstname )        
        
WHERE        
  m1.lastname LIKE 'Blogs'
  and m2.lastname LIKE 'Blogs' 

Select Query
lets say i have a poll where users can vote on the following
1. Good
2. Bad
3. Ugly

i want to write a query (from PHP) that will let me select and order based on how many each has...so if 2.Bad has the most votes, display it first...if 3.Ugly has the second most votes, display it second.my sql query skills...

Select Query
I have a mysql table with about 2,000,000 rows. Whenever I run this query

select * from table limit 1000

it takes less than 1 second to return. But when I run this query

select * from table where Destination like '%talk%' limit 1000

it takes about 70 seconds to return the result. Destination column has an index. Is this a normal response from this size of a table?

SELECT Query
I use php and MySQL.

User fills out search form.
There can be possiblities:
1) user knows the full name
2) user knows first letter(1 or 2)
3) user leaves the field empty meaning - look for any name

How do formalte my query?

SELECT * FROM table WHERE AGE>20 AND NAME='';

Select Query
I need to construct a mysql select statement that will rank the rows returned such that the more hits per keyword the high rank within the results.
For example, lets say I have the keyword phrase:
"keyword1 keyword2 keyword3"

I need mysql to search the text field in such a way that it will rank multiple matches on any of the keywords such that the more a keyword is found within the text, the higher it will go within the returned rows.
So far I have been trying:

SELECT * FROM articles WHERE MATCH (title,body) AGAINST ('keyword1 keyword2 keyword3');

...this has had limited results.

SELECT Query
I got 3 relevant tables:

'links': ID, Link, UserID, SiteID
'sites': ID, Name
'user': ID, Flag, Name

Now I would like to SELECT all Links from users with the Flag "Premium".
Simple example would be, I got 10 users, 4 of them with the 'Premium' Flag. All of those 10 users got about 4 links in the link table. Now I'd only like to select the links which belong to the Premium Users(=flag field in table user).

SELECT Query
I have a table called thingamajigs which i keep tabs of product colors.
In thingamajigs there are id, productid and productcolor fields. There could be dozens of productcolors for a product id - resulting in multiple entries of the same productid in this table.I need to find out all productid's where there is availability in both Red and Blue.

I thought my query should look something like this:

SELECT productid FROM thingamajigs WHERE productcolor='Red' AND productcolor='Blue'

But, that returns zero matches -- even though I know there is at least one productid which is available in both Red and Blue.

SELECT Query.
Table

1 3 #1,#2
2 5 #2,#3
3 3 #1,#2,#3
4 4 #4
5 5 #1,#2,#3
6 5 #1,#2,#3
7 4 All
8 3 #1,#2,#3

Need to compare the value of field2 with the occurrences of "#" in field3.
Tried this:
$query_plcResultsSet = "SELECT * FROM Table WHERE RteType IN ('Mainline', 'Connector') AND StartDateD = '$strchvarval' AND (field2 = 'All' OR " . substr_count(field3, '#') . " = field2)";

Select Query
premise:running mysql 4.0.20
i got a table with following columns

Code:
win1 win2 lose1 lose2
a b c d
a d
b c a e
a d
d a c e



so basically its like this..i got teams a-e
win1 and win2 are the 2 teams that wins (if theres an teamate then there is win2,otherwise, he won it alone) and same principle goes for the losing teams..
so how does the select query goes if i want to select all teams and order em by the amount of wins they have (win1 + win2)

Select Query
Ok, I've a db that looks like this, lets call it 'menu'

menu_id | menu_value | menu_parent
1 | Boats | 0
2 | Cars | 0
3 | Modells | 1
4 | Colors | 1
5 | Red | 4
6 | Blue | 4

How do I list Boat, Cars, Modells and Colors... The menu is really dynamic so I could not set limit 4...
I wrote this, it feels close but it doesn't show 'Boats' and 'Cars' and it do show 'Red' and 'Blue' which I don't want to show...
SELECT c.menu_id, c.menu_value, p.menu_value AS menu_parent, p.menu_id AS parent_id FROM menu AS c INNER JOIN menu AS p ON p.menu_id = c.menu_parent ORDER BY c.menu_value ASC


Copyright © 2005-08 www.BigResource.com, All rights reserved