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
https://forums.phpfreaks.com/topic/141021-sql-query/
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
https://forums.phpfreaks.com/topic/141021-sql-query/#findComment-738101
Share on other sites

  Quote

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.

 

 

  Quote

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
https://forums.phpfreaks.com/topic/141021-sql-query/#findComment-738109
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
https://forums.phpfreaks.com/topic/141021-sql-query/#findComment-738141
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 -

  Quote
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
https://forums.phpfreaks.com/topic/141021-sql-query/#findComment-739803
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.