Jump to content

SQL query


warhead2020

Recommended Posts

Hi, i got one problem. Here is the problem.

 

-----------------------------

name  | date      | time  | status

----------------------------

one    | 1/1/2008 | 1.12  |  A

two    | 1/1/2008 | 1.13  |  A

one    | 1/1/2008 | 2.20  |  B

two    | 1/1/2008 | 3.00  |  B

one    | 1/1/2008 | 14.20  |  A

one    | 1/1/2008 | 17.20  |  A

 

My goal now is,how to do a query to get name with the lastest status. The result should be, if i want the name with "A" status, the answer will be " one    | 1/1/2008 | 17.20  |  A ".

I hope someone out there can help me. Thanks in advance.

 

Link to comment
Share on other sites

Date should be a date column (as in the type).  Also, date is a reserved MySQL word, so you should name it something else to avoid having to use `` all the time.

 

Anyway, once it's date type, you can just do:

 

SELECT * FROM table WHERE status = 'A' ORDER BY DateColName LIMIT 1;

 

 

You would probably want an index on DateColName if you plan on having a lot of rows.

Link to comment
Share on other sites

Date should be a date column (as in the type).  Also, date is a reserved MySQL word, so you should name it something else to avoid having to use `` all the time.

 

Thanks for ur reply. Actually that is not the real column name. I just want to give an example.

 

 

SELECT * FROM table WHERE status = 'A' ORDER BY DateColName LIMIT 1;

 

refering to ur sql, logically,the result returned is

 

one    | 1/1/2008 | 14.20  |  A

one    | 1/1/2008 | 17.20  |  A

 

my goal is, i want

one    | 1/1/2008 | 17.20  |  A

coz  that is the latest status for ONE. I hope my question is clear. Thanks in advance.

Link to comment
Share on other sites

It's not?  Hrmm, could've sworn it was x.x.  Hehe ;p.

 

 

 

Anyway, sorry, I did not see the time column.  I suggest changing your date and time columns to be 1 column of the datetime type.  That's much more efficient for MySQL and much easier to manage.

 

 

Anyway, this should work:

 

 

SELECT * FROM table ORDER BY date DESC, time DESC LIMIT 1;

 

 

 

If you merged to 1 column:

 

SELECT * FROM table ORDER BY column DESC LIMIT 1;

Link to comment
Share on other sites

Actually date is a data type and since it is only used when creating or altering columns it is permitted to be used for table and column names without any special treatment (tested.) It does not appear in the reserved words table, but it is listed in the following -

MySQL allows some keywords to be used as unquoted identifiers because many people previously used them. Examples are those in the following list:

 

    *      ACTION

    *      BIT

    *      DATE

... 

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.