Help wanted - Struggling with 'updated'-NOW() field

I try to learn SQL by figuring out things.

I want to make a listing of all records that were changed in the
last... 1, 6, 12 hours/days.

I have a field called 'updated' managed like: UPDATE tablename SET
updated = NOW(), .... WHERE....

I created a query like this: "SELECT * FROM tablename WHERE 'updated'
> '$startstring' ORDER BY 'updated'"

// $startstring holds the date-time string exactly like the 'updated'
field should look.

When in php i do: if($record[updated] > $startstring) ..show record..;
it works, but then I must pull the entire database.

In the much more efficient WHERE clause it does not work.

Where am i going wrong?
--
/Kees
Hans-Peter Sauer [ Fr, 17 November 2006 16:19 ] [ ID #1539268 ]

Re: Help wanted - Struggling with 'updated'-NOW() field

Keith wrote:

> I try to learn SQL by figuring out things.
>
> I want to make a listing of all records that were changed in the
> last... 1, 6, 12 hours/days.
>
> I have a field called 'updated' managed like: UPDATE tablename SET
> updated = NOW(), .... WHERE....
>
> I created a query like this: "SELECT * FROM tablename WHERE 'updated'
> > '$startstring' ORDER BY 'updated'"
>
> // $startstring holds the date-time string exactly like the 'updated'
> field should look.
>
> When in php i do: if($record[updated] > $startstring) ..show record..;
> it works, but then I must pull the entire database.
>
> In the much more efficient WHERE clause it does not work.
>
> Where am i going wrong?
> --
> /Kees

try echoing the query
zac.carey [ Fr, 17 November 2006 21:15 ] [ ID #1539269 ]

Re: Help wanted - Struggling with 'updated'-NOW() field

"strawberry" <zac.carey [at] gmail.com> wrote:

>
> Keith wrote:
>
> > I try to learn SQL by figuring out things.
> >
> > I want to make a listing of all records that were changed in the
> > last... 1, 6, 12 hours/days.
> >
> > I have a field called 'updated' managed like: UPDATE tablename SET
> > updated = NOW(), .... WHERE....
> >
> > I created a query like this: "SELECT * FROM tablename WHERE 'updated'
> > > '$startstring' ORDER BY 'updated'"
> >
> > // $startstring holds the date-time string exactly like the 'updated'
> > field should look.
> >
> > When in php i do: if($record[updated] > $startstring) ..show record..;
> > it works, but then I must pull the entire database.
> >
> > In the much more efficient WHERE clause it does not work.
> >
> > Where am i going wrong?
> > --
> > /Kees
>
> try echoing the query

Sorry its standard testing for me, but that shows exactly what i want
it to show:

SELECT * FROM tablename HAVING 'updated' > '2006-11-16 23:25:22' ORDER
by 'updated'

--
/Keith
Hans-Peter Sauer [ Fr, 17 November 2006 23:29 ] [ ID #1539271 ]

Re: Help wanted - Struggling with 'updated'-NOW() field

"strawberry" <zac.carey [at] gmail.com> wrote:

>
> Keith wrote:
>
> > I try to learn SQL by figuring out things.
> >
> > I want to make a listing of all records that were changed in the
> > last... 1, 6, 12 hours/days.
> >
> > I have a field called 'updated' managed like: UPDATE tablename SET
> > updated = NOW(), .... WHERE....
> >
> > I created a query like this: "SELECT * FROM tablename WHERE 'updated'
> > > '$startstring' ORDER BY 'updated'"
> >
> > // $startstring holds the date-time string exactly like the 'updated'
> > field should look.
> >
> > When in php i do: if($record[updated] > $startstring) ..show record..;
> > it works, but then I must pull the entire database.
> >
> > In the much more efficient WHERE clause it does not work.
> >
> > Where am i going wrong?
> > --
> > /Kees
>
> try echoing the query

Just realise: it does work, but it does not select so te whole
database is pulled, so the WHERE clause does not do it's job.
--
/Keith
Hans-Peter Sauer [ Fr, 17 November 2006 23:35 ] [ ID #1539272 ]

Re: Help wanted - Struggling with 'updated'-NOW() field

Keith wrote:
> "strawberry" <zac.carey [at] gmail.com> wrote:
>
> >
> > Keith wrote:
> >
> > > I try to learn SQL by figuring out things.
> > >
> > > I want to make a listing of all records that were changed in the
> > > last... 1, 6, 12 hours/days.
> > >
> > > I have a field called 'updated' managed like: UPDATE tablename SET
> > > updated = NOW(), .... WHERE....
> > >
> > > I created a query like this: "SELECT * FROM tablename WHERE 'updated'
> > > > '$startstring' ORDER BY 'updated'"
> > >
> > > // $startstring holds the date-time string exactly like the 'updated'
> > > field should look.
> > >
> > > When in php i do: if($record[updated] > $startstring) ..show record..;
> > > it works, but then I must pull the entire database.
> > >
> > > In the much more efficient WHERE clause it does not work.
> > >
> > > Where am i going wrong?
> > > --
> > > /Kees
> >
> > try echoing the query
>
> Just realise: it does work, but it does not select so te whole
> database is pulled, so the WHERE clause does not do it's job.
> --
> /Keith

eh?
zac.carey [ Sa, 18 November 2006 01:13 ] [ ID #1540313 ]

Re: Help wanted - Struggling with 'updated'-NOW() field

> I created a query like this: "SELECT * FROM tablename WHERE 'updated'
> > '$startstring' ORDER BY 'updated'"

Note that this query does not reference the updated field at all,
except for the "select *" part.

>// $startstring holds the date-time string exactly like the 'updated'
>field should look.

'updated' is a string, not a field name.

>In the much more efficient WHERE clause it does not work.
>
>Where am i going wrong?

`updated` is a field name. 'updated' is a string.
gordonb.6ap7w [ Sa, 18 November 2006 01:25 ] [ ID #1540314 ]

Re: Help wanted - Struggling with 'updated'-NOW() field

"strawberry" <zac.carey [at] gmail.com> wrote:

>
> Keith wrote:
> > "strawberry" <zac.carey [at] gmail.com> wrote:
> >
> > >
> > > Keith wrote:
> > >
> > > > I try to learn SQL by figuring out things.
> > > >
> > > > I want to make a listing of all records that were changed in the
> > > > last... 1, 6, 12 hours/days.
> > > >
> > > > I have a field called 'updated' managed like: UPDATE tablename SET
> > > > updated = NOW(), .... WHERE....
> > > >
> > > > I created a query like this: "SELECT * FROM tablename WHERE 'updated'
> > > > > '$startstring' ORDER BY 'updated'"
> > > >
> > > > // $startstring holds the date-time string exactly like the 'updated'
> > > > field should look.
> > > >
> > > > When in php i do: if($record[updated] > $startstring) ..show record..;
> > > > it works, but then I must pull the entire database.
> > > >
> > > > In the much more efficient WHERE clause it does not work.
> > > >
> > > > Where am i going wrong?
> > > > --
> > > > /Kees
> > >
> > > try echoing the query
> >
> > Just realise: it does work, but it does not select so te whole
> > database is pulled, so the WHERE clause does not do it's job.
> > --
> > /Keith
>
> eh?

SELECT * FROM tablename HAVING 'updated' > '2006-11-16 23:25:22' ORDER
by 'updated'
returns all records from the database not only those updated after
2006-11-16 23:25:22
Hans-Peter Sauer [ Sa, 18 November 2006 10:38 ] [ ID #1540315 ]

Re: Help wanted - Struggling with 'updated'-NOW() field

gordonb.6ap7w [at] burditt.org (Gordon Burditt) wrote:

> > I created a query like this: "SELECT * FROM tablename WHERE 'updated'
> > > '$startstring' ORDER BY 'updated'"
>
> Note that this query does not reference the updated field at all,
> except for the "select *" part.
>
> >// $startstring holds the date-time string exactly like the 'updated'
> >field should look.
>
> 'updated' is a string, not a field name.
>
> >In the much more efficient WHERE clause it does not work.
> >
> >Where am i going wrong?
>
> `updated` is a field name. 'updated' is a string.
>
Wow, never saw that those back-ticks were so important, I always used
normal ticks and it worked, but not now.

THANK YOU
Hans-Peter Sauer [ Sa, 18 November 2006 12:50 ] [ ID #1540316 ]

Re: Help wanted - Struggling with 'updated'-NOW() field

Keith wrote:

> gordonb.6ap7w [at] burditt.org (Gordon Burditt) wrote:
>
>
>>> I created a query like this: "SELECT * FROM tablename WHERE 'updated'
>>> > '$startstring' ORDER BY 'updated'"
>>
>>Note that this query does not reference the updated field at all,
>>except for the "select *" part.
>>
>>
>>>// $startstring holds the date-time string exactly like the 'updated'
>>>field should look.
>>
>>'updated' is a string, not a field name.
>>
>>
>>>In the much more efficient WHERE clause it does not work.
>>>
>>>Where am i going wrong?
>>
>>`updated` is a field name. 'updated' is a string.
>>
>
> Wow, never saw that those back-ticks were so important, I always used
> normal ticks and it worked, but not now.
>
> THANK YOU

if updated is a column name then the query should look like:

SELECT * FROM tablename HAVING updated > '2006-11-16 23:25:22' ORDER
by updated;

Depending on your datefield you may need to "convert" the time to something the
database can actually use.

It is always a good idea to test your select statements interactively before
moving it to your applciation.


--
Michael Austin.
DBA Consultant
Michael Austin [ So, 19 November 2006 21:19 ] [ ID #1540907 ]
Datenbanken » mailing.database.mysql » Help wanted - Struggling with 'updated'-NOW() field

Vorheriges Thema: many to many problem
Nächstes Thema: partial schema retrieval