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




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?




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
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.

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.

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.

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 '
'

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.

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

Retrieving MAX(col)
I have a query that returns 25 rows. How would I get the MAX value of a column from this query? I don't want to group the results because then my query will get grouped and I don't want that.

Random Retrieving
data in myTable

(id) name
(1) Tom
(4) Jane
(8) John
I have data in myTable like the above.

Can I retrieve one of names in name column randomly?

Retrieving Pictures
im in the process of adding path a name of a picture into a field so that i see it in my web browser. it isnt working! ive got car_make and images fields. the image field is varchar. is this correct?

basically need these pictures to upload into my browser but im struggling to insert paths into the database in the first place. any help is appreciated. thanks

p.s. when i change the field type to BLOB in phpmyadmin it says binary, do not edit. it wont let me edit the field any more.

Retrieving LAST_INSERT_ID
"SELECT LAST_INSERT_ID()" returns the last inserted value of an AUTO_INCREMENT column.
But can only be used after an insert (as far as i know).
What should i do to retrieve this value from a specific table?

I've tried:
SELECT LAST_INSERT_ID(table.column)
SELECT LAST_INSERT_ID(table)
SELECT LAST_INSERT_ID(column)

...but didnt work. I dont know if MySQL doesnt support these statements or if the version im using is old (4.0.15). Ill upgrade as soon as i get the time to download the newest version (35mb)

Retrieving From 2 Tables
I have 2 tables, Male and Female.

Male has a field guyName and contains: ['John', 'Jack'].
Female has a field girlName and contains: ['Mary,'Emily'].

What query should I use to get an array like this:

[[0]=>Array(guyName=>'John'),[1]=>Array(guyName=>'Jack'),
[2]=>Array(girlName=>'Mary'), [3]=>Array(girlName=>'Emily')]?

I used
SELECT guyName FROM Male UNION SELECT girlName FROM Female and I get this:

Array ( [0] => Array ( [guyName] => John) [1] => Array ( [guyName] => Jack ) [2] => Array ( [guyName] => Mary ) [3] => Array ( [guyName] => Emily ) )

I want the key to match with the table field correctly.

Retrieving Data
i am having 3 tables in my db.how can i display all d product ids in "tbprod" table that are under a category id which is in d `tbcat` as `catid`.

CREATE TABLE `tbcat` (
`catid` int(10) unsigned NOT NULL auto_increment
}

CREATE TABLE `tbsubcat` (
`scatid` int(10) unsigned NOT NULL auto_increment,
`catid` int(10) unsigned NOT NULL default '0'
}

CREATE TABLE `tbprod` (
`pdid` int(10) unsigned NOT NULL auto_increment,
`scatid` int(10) unsigned NOT NULL default '0'
}

Retrieving Values From Url
i want to put the value in a variable but dont know how to retrieve it:

i want to use record_num = 3 as a variable:

URL: .../review.php?,record_num=3

EXAMPLE OF VAR

$link_id ='[record_num]';

SOURCE
<a href='review.php?,record_num={$row['link_id']}'>LINK</a>

Retrieving Information As It Is
I have a mulitline input field, going to a database, when i view the inputted information in the database "browse" i looks exactly like it does when i typed it in for example

Line one
Line Two

When i get the information back out using a SQL statement it come out in one lone line thus Line one Line two, with two boxes in between where the line break should be.

I need to to come out as it went in

Is this possible?

Retrieving Top 100 Results
I am trying to get an average value from my database but I only want to retrieve the average out of the top 100 results. The column I want to average contains values from 0.1 to 10.0.

So I want to get the average from the top 100 values in this column. The average syntax is clear but what do I have to add?

This is what I have now:

$query="select AVG(M_RATING) as rating FROM rate_members WHERE M_COUNTRY='SE'";
But this gives me an average based on all values in the column.

Retrieving News
[I]I am doing on my final year project. One part is mySql. how come my client's application ( Java) retrieve news from my data base. Cos i faced some problems linking the codes to the time stamp of the data base.

Retrieving New Record ID
I'm making a web-site thats backended by php-2-mysql. A common problem seems
to be when processing form data, I use information early in the form to
create a new record in a main table. I then immediately need the
auto_incremented Key-ID of the new record to put other information in
related tables.

In the past i've gotten that ID by just doing a SELECT with a WHERE that
matches all the values i've just INSERTED, but it seems a cumbersome
method.

Retrieving New Record ID
I'm making a web-site thats backended by php-2-mysql. A common problem seems
to be when processing form data, I use information early in the form to
create a new record in a main table. I then immediately need the
auto_incremented Key-ID of the new record to put other information in
related tables.

In the past i've gotten that ID by just doing a SELECT with a WHERE that
matches all the values i've just INSERTED, but it seems a cumbersome
method.

Retrieving Records
hi,
I have the following table:

ID Type TimeStampe

10 1 2005-04-25 16:37:58
10 2 2005-04-25 16:39:58
10 3 2005-04-25 16:38:58
10 4 2005-04-25 16:38:58

I need to create a query that will get me 2 types (limit 2) that have the oldest timestamp (1 and 3).

Retrieving All Records
I have a table with a field called "posted" that contains a UNIX timestamp of the time it was posted. I would like to set up a query to get all of the posts that occurred for the current month. What I have is..
Code:
SELECT * FROM posts WHERE FROM_UNIXTIME(posted,%m) = DATE_FORMAT(NOW(),%m)

Is this correct, or is there a more optimized way to do it?

Retrieving First Sentence
Does anyone know how I can extract the first sentence from a text field? I've got it right up to a point with SUBSTRING_INDEX but it's not quite good enough. The problem is that I want to stop capturing the substring at the first occurence of one of three possible characters: ! . ?

SUBSTRING_INDEX doesn't seem to allow multiple delimiter characters so I guess I'm looking at regexp but I'm not sure how to go about it. Here's my current query:


Code:


SELECT SUBSTRING_INDEX(item, '.', 1) AS preview FROM entries;

Retrieving Data
Came to ask you gurus to help me out with the below error:
1064 - 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 'on o.blacklisted = c.blacklisted where c.customers_id = '0' , o

select count(*) as total from orders o left join orders_total ot on (o.orders_id = ot.orders_id), customers c on o.blacklisted = c.blacklisted where c.customers_id = '0' , orders_status s where o.orders_status = s.orders_status_id and s.language_id = '1' and ot.class = 'ot_total'

Since I could not understand very well where I made mistake, I like to ask you guys to help me. If the above error does not give you a right picture, I can post the php codes where from this error came.

Retrieving Unicode Data
I have an existing JSP web application which retrieved data from a
SQLServer database and displayed it. The data contained Unicode chars. This
worked fine. I changed databases/JDBC drivers over to MySQL. I've set the
database to use utf8 and the tables as well. I've viewed the data in the
table and it contains unicode just fine, however, the query returns data
with all the unicode chars converted to question marks. I have searched for
a solution to this for hours and no luck so far.

I've tried passing in useUnicode=true and characterEncoding=UTF8 in the URL
connection string and this has done nothing. Then I read that by default
the driver has the useUnicode set to true. I tried setting it to false to
see if it would do anything. Nothing. Code:

Retrieving Last Auto-increment
I've got a project where I'm adding records to a database, and the primary key is autogenerated (pretty standard). What I want to do is retrieve the id of the last record added to the database, so I can use it in another part of the script.

I'm currently using a select statement, but there has to be a way of doing it without another query.

Retrieving Data Through A Form.
I'm setting up a database and I want to retrieve data from the db through a form.

I currently have:

$sql = "SELECT * FROM Tasks WHERE t_id = '$_POST[t_id]'";

$result = mysql_query($sql, $conn) or die(mysql_error());

while ($newArray = mysql_fetch_array($result)){
$id = $newArray['t_id'];

echo "ID: $id <br>";

I don't think that the 'WHERE t_id = '$_POST...' part is correct, as the code seems to do what it is supposed to when I take that part out, it just doesn't do what I want it to do.

So.. to clarify; I insert a task id (t_id) into the form, hit the submit button, and it is supposed to show the results (echo "ID: $id <br>";). I hope this makes sense to someone. Code:

RETRIEVING DATA FROM FOUR TABLES
I need to retrieve data from four tables into one php "$result" with one MySQL query.  The tables are configured as follows:

Images
   ImageID (primary key & the only unique field)
   Serial
   Photographer (all photographers are listed once in the Posters.Photographer field, ie many to one)
   Operator     (only some operators are listed in the Operators.Operator field, not all, ie many to the occasional one)
   Subject (all subjects are listed once in the Subject.Subject field, ie many to one)

Operators
   Operator (primary key)   
   OpField1
   OpField2
   etc

Photographers
   Photographer  (primary key)
   PhoField1
   PhoField2
   etc

Subjects
   Subjects      (primary key)
   SubField1
   SubField2
   etc

The task:  I need to select ALL images from the Images table meeting criteria set by conditionals on fields in that table, eg 'where Serial = "ABCDE"'.  For each hit I need to fetch all the relevant fields from all four tables, relating to the Image, the Operator (where they exist), Photographer and Subject. Obviously I don't need to retrieve the linking fields twice.

"The answer's going to be a left join" I hear you thinking - well, something like that.  Because a straight join between Images.Operator and Operators.Operator results in the correct image data not being retrieved when the particular operator does not appear in the Operators table (there are good reasons for this, the details for many operators simply don't exist or can't be traced).

So, either I add all the Operators that occur in the Images table to the Operators table, even though there is nothing to put in the detail fields, or I need a query that successfully combines all the correct kinds of joins.  I've found a few likely-looking examples to follow on the internet and in books, but all sorts of unexpected things happened when I tried them.  I've read so much about joins that my head's ing.  And I don't learn so fast these days (no hair left to keep the few remaining brain cells warmed up).  I have a suspicion that it's actually less complicated than the guidance makes it out to be...

Retrieving Order Number
is there a way to retrieve the order number along with the other fields in a select statement ?

for example if I have a query that selects the following results:

ID Value
-----------
1 1345
3 8275
5 2384
7 2345

I want a way to retrieve the following results:

Recno ID Value
--------------------
1 1 1345
2 3 8275
3 5 2384
4 7 2345

Retrieving User Password?
I created a mysql DB and I can't recall the password that I used for the main user. Something has come up and I need to retrieve the PW I created for the user, is this possible?

If so, how do I do this from cpanel?

Retrieving Mysql Into Web Table
OK, I’m trying to simply retrieve records from a table in Mysql. Say I have around 60 to 110 rows in a table of different members each with a (FirstName, LastName and Contact) field, and I would like to show them all on a web page.

As it is the first time I’m trying myself with table in PhP my question is, am I’m doing it right ??? Do I use the markers around the <br>, <div> and other tags in the right way ???

My code I have shown below includes several different code’s which looked to me promising, well if somebody could help here so I can actually retrieve information from a specific table. Code:

Retrieving The Last Value Of A Specific Column
I need help in retrieving the last value that is entered in my table for a specific column

An example to clarify what I want to do:

I am collecting data automatically and the values of which are being inserted into a database. I want to retrieve for example the last row entered for a column named e.g. current_number. I am not sure how this can be done in SQL.

One idea I had is to use the last_insert_id(), but I am not so sure how to use it as an sql query. Is there another way to get the the last value of a specific column in a table?


Retrieving Last Inserted Record
After a use the INSERT command to enter something into the database I seem to remember a way to get the record that was just created using some kind of SELECT command. I seem to remember "SELECT @" or something similiar.

how I can go about getting the record just inserted with the INSERT command?

Retrieving Information From MySQL
I have created a an online shop and it works fine on localhost and on the test server, however we want to intregate it with an online bank and its not working properly.

When purchasing a single product the product can be purchased successfully however multiple products cannot purchased successfully.

For some reason the page on the online banks server cannot access one our database that stores the information in the customers shopping cart but it can retrieve the information for a single product order. Im not sure if its some kind of issue on there server?

SQL For Retrieving 10 Highest Values
So basically I need the SQL query that retrieves from a table 'users' the 10 highest values from the field 'points'.

Retrieving Data From Server
I was able to query the local Access database for all kinds of information but now because I am uploading the information directly to the website I do not have this information available on my local machine.

Is there a way that I could download the data file as and when required from my server and insert it into Access or something similar that would allow me to run queries?

I thought I was doing well up until this stage but my decision to make the process more efficient seems to have caused more problems.

Retrieving Specific Records
I am trying to establish (using a straight MySQL query in version 4.0.18)
the level of qualifications someone has, i.e. degree, postgraduate degree or
other.

To simplify things, I need to use only two tables, namely:

master table, containing:
staffno (Primary Key);
forename;
surname;

psnquals table, containing:
psnqualid (Primary Key);
staffno (Foreign Key);
levelid (where 1 = degree, 2 = postgraduate degree, 3 = other)

In the followning query, staffno 1 has three qualification levels, 1, 2, 3.
The query finds the first matching record (where cdelevelid = 1) and then
stops, making it appear as if the employee had no postgraduate
qualification.

One person can have one or all of the above qualifications. I need to write
a query that shows if a person has one, two or all of the above. I tried
using the following query, but it seems only to look at the first psnqualid
record it finds and then stops:

SELECT m1.staffno, m1.forename, m1.surname,
CASE WHEN p1.cdelevelid = 1 THEN "D" WHEN p1.cdelevelid = 2 THEN "P" ELSE
"O"
FROM `master` m1
INNER JOIN psnquals p1 ON m1.staffno = p1.staffno

I also tried using an IF statement, but this also seemed to stop after
finding the first matching record in the psnquals table.

Retrieving Elapsed Time
I have a table storing logs, and i would like to retrieve the elapsed time from the date in the logs till Now! I need to return something like 3 days 2 hours 14 min.

Retrieving Unicode Data
I have an existing JSP web application which retrieved data from a
SQLServer database and displayed it. The data contained Unicode chars. This
worked fine. I changed databases/JDBC drivers over to MySQL. I've set the
database to use utf8 and the tables as well. I've viewed the data in the
table and it contains unicode just fine, however, the query returns data
with all the unicode chars converted to question marks. I have searched for
a solution to this for hours and no luck so far.

I've tried passing in useUnicode=true and characterEncoding=UTF8 in the URL
connection string and this has done nothing. Then I read that by default
the driver has the useUnicode set to true. I tried setting it to false to
see if it would do anything. Nothing.

Then I came across this:

http://lists.mysql.com/java/7098

I don't know anywhere else to ask this. Even though it may not be MySQL's
problem but the driver I don't see any forums for the MySQL driver.

Retrieving Fields With A Null Value
i'm trying to write this query which is looking for fields that are NULL, but it doesn't seem to work. What's wrong with this code snippet.

I checked my database and the fields are in fact NULL so i know the problem is within this code.


$query = "SELECT `jid`, `jobnumber`, `jobname`, `install_date` FROM `jobs` WHERE `engineering_received` = 'NULL' OR `engineering_received` = '' ORDER BY install_date";

Retrieving Most Recent Datestamp Row
If you remember my post from a few days ago, this is the table structure that I'm now using (thanks CYoung and TonyGroves):

stamp DATETIME not null primary key,
year int(4) not null,
month int(2) not null,
day int(2) not null,
hour int(2) not null,
minute int(2) not null,
reading float(10,6) not null



How would I go about retrieving the results that have the most recent and most distant DATETIME  from the above table? These are not necessarily the first and last rows entered in the database table, as the order in which files are parsed and entered into the database may not be chronological.

Apologies if this is remedial but I'm fairly new to sql.

I also need to fetch results by descending DATETIME. I'm guessing that this will do it:

"select * from siteTable
 where stamp between $datetimeA
 and $datetimeB
 order by stamp desc"

I won't be able to test it out until tomorrow night. Can I do the 'order by desc' thing with datestamps?

Retrieving Unique Records
Hi the subject matter pretty much sums up what i need, a piece of sql code that queries 3 or more tables and selects only those records that have a date field that only occurs in one of the tables. So for example if one table has 20 records with a date of 12/12/04 and the other 2 tables do not have any records with a date of 12/12/04 then i only want to select those records with a date of 12/12/04.

Retrieving The Last Record In A Table
What is the best way to retrieve the last record entered into a table?


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