Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2014
    Posts
    7

    Unanswered: Obtain the most recent records using IN operator

    Hi Folks,

    I need to pull out the most recent records from a simple table based on the date added field. My search form contains a text box where the user put several records to search and I use the IN operator to query each record.

    Table:

    Click image for larger version. 

Name:	data.JPG 
Views:	7 
Size:	31.4 KB 
ID:	16456

    The query used to pull data from the text field is:

    Code:
    SELECT ID, material, price, date_added WHERE material In ("[records in the textbox separated by comma]")
    How can I pull only the most recent records based on the date_added field? Ex. If the user insert the records "123" and "345", the result needs to be:

    Click image for larger version. 

Name:	Result.JPG 
Views:	6 
Size:	22.8 KB 
ID:	16457

    Thank you very much in advance for your help!
    Last edited by gustavoavila; 07-15-15 at 19:37.

  2. #2
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    495
    Provided Answers: 24
    I don't see the need for the IN operator.

  3. #3
    Join Date
    Feb 2014
    Posts
    7
    Quote Originally Posted by ranman256 View Post
    I don't see the need for the IN operator.
    Well... the IN operator is used to search for each record in the textbox. If there is a best way to search several records inserted in a multiline textbox, please share it to improve my application

  4. #4
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    try something like:-
    Code:
    SELECT  top  1 material, price, date_added
    FROM mytable
    where material in (123,234,765)
    order by date_added DESC
    ;
    change 'mytable' to whatever your table is called
    select top 1, means get the first row row for each match
    the order by clause tells the sql engine to work in reverse (thats the DESC bit) date order


    if the materials are coming form a list/combo box, then you coudl build the query oin the fly (using the material id(s) from the list/combo box. or supply the values as parameters
    I'd rather be riding on the Tiger 800 or the Norton

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •