Jump to content

[SOLVED] SELECT LIKE


5kyy8lu3

Recommended Posts

 

I have PHP 5.0.45.

 

Hi.  I'm fairly new to MySQL.  I'm self taught from the php/mysql for dummies book.  Though it covered php pretty well, I felt it lacked in the mysql department a bit.

 

I've never used LIKE or the wildcard in a mysql query before.  I searched and I believe I got this right but I want to make sure I did this query correctly.  I don't have any dates in my table from last year so it's hard for me to test this.

 

What I want: to select entries in the table where the timestamp is from this year.  I use YYYYMMDDHHMMSS for my timestamp format.

 

Is my query correct for pulling ALL entries from the table that are from this year?

 

<?php
// my php line:
$q = 'SELECT miles FROM ' . $username . ' WHERE dt_added LIKE ' . date("Y") . '%';

//which should result in a query like this:
SELECT miles FROM admin WHERE dt_added LIKE 2008%

 

would this query accomplish what I'm wanting it to?  I've read online and bought the o'reilly's mysql book today and I believe this should work but when I add php into the mix I'd just like to be 100% sure.  Thanks!

 

Link to comment
Share on other sites

$q = 'SELECT miles FROM ' . $username . ' WHERE YEAR(dt_added) = YEAR(CURDATE())';

lol man I was way off!

 

so out of curiousity, would my method even work?  i knew mysql had some built in functions for handling dates and times but I didn't know I could use it with my weird timestamp format ;D

 

appreciate the help and fast responce, cheers

Link to comment
Share on other sites

You're not storing this timestamp as a string, do you?

Date functions will work on MySQLs DATE, TIME, DATETIME and TIMESTAMP fields.

 

If any book tells you to store date, timestamp or anything like that as a string - burn it, as it is source of evil.

Link to comment
Share on other sites

You're not storing this timestamp as a string, do you?

Date functions will work on MySQLs DATE, TIME, DATETIME and TIMESTAMP fields.

 

If any book tells you to store date, timestamp or anything like that as a string - burn it, as it is source of evil.

just bought the book today so I haven't had time to read it front to back yet, but yes, I've been storing it as VARCHAR, oops =) but I'm guessing I should ALTER TABLE and change it to DATETIME, yea?

 

I wrote functions to format my timestamps.  I'm guessing I would've saved myself a TON of time had I just used DATETIME format and the built in date/time mysql functions lol :o

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.