1

I'm creating a new database for a customer, and want to select the oldest group of dates based on a variable that he enters through a tkinter gui. I'm using Sqlite and I can't seem to find an answer since the date is stored as text. Is there a way of comparing dates and selecting the oldest few based on a variable. I don't need to select 2 dates and the products in between I want to select 3 products to go out and the program automatically selects the oldest 3 products in the database Thank you. :)

For example If there are 5 products in the table:

    • product one on january 1st.
    • product two on january 10th.
    • product three is january 3rd.
    • product 4 is february 5th.
    • product 5 is february 2nd.

I'd like to select the oldest 4

So the result would be:

    • the row that contains product 1
    • the row that contains product 3
    • the row that contains product 2
    • the row that contains product 5.

How can I do that?

MikeT
  • 51,415
  • 16
  • 49
  • 68

2 Answers2

1

You can use ORDER BY on the field you want to sort by and LIMIT the results:

SELECT * FROM orders ORDER BY order_date ASC LIMIT 5

This would select the lowest 5 orders according to the order date. You can change the direction of sorting by using DESC.

MikeT
  • 51,415
  • 16
  • 49
  • 68
Felicia
  • 41
  • 4
1

I believe you would use something like (assuming that the date column is named product_date)

SELECT * FROM products ORDER BY product_date ASC LIMIT 4;
  • The date being in YYYY-MM-DD format will be sorted textually but that would be fine.

e.g.

INSERT INTO products 
    VALUES
        ('Product 1','20190101'),
        ('Product2','20190110'),
        ('Product3','20190103'),
        ('Product4','20190205'),
        ('Product5','20190204');
SELECT * FROM products ORDER BY product_date ASC LIMIT 4;

Results in :-

![enter image description here

Considering the comment

what if I want to make the limit a variable

This is an example where the LIMIT is passed as a variable (limit_var) that is bound (i.e. replaces the ?) :-

def so58207795():
    import sqlite3

    db = sqlite3.Connection("mycart")
    db.execute("CREATE TABLE IF NOT EXISTS products(product TEXT, product_date TEXT)")
    db.execute("INSERT INTO products VALUES('Product 1','20190101'),('Product2','20190110'),('Product3','20190103'),('Product4','20190205'),('Product5','20190204')")
    limit_var = "4"
    print(db.execute("SELECT * FROM products ORDER BY product_date ASC LIMIT ?",(limit_var)).fetchall())

Resulting in :-

E:\PYCharmPythonProjects\venv\Scripts\python.exe E:/PYCharmPythonProjects/Test001.py
[('Product 1', '20190101'), ('Product3', '20190103'), ('Product2', '20190110'), ('Product5', '20190204')]

Process finished with exit code 0
MikeT
  • 51,415
  • 16
  • 49
  • 68
  • Thanks a lot, I'll try it and tell you . final request is , what if I want to make the limit a variable. Is that possible ? Thanks a lot for your help. :) – Mohamed Amr Oct 02 '19 at 20:23
  • Yes you just alter the SQL accordingly programatically (preferebaly binding the value so as to avoid the potential for SQL injection). – MikeT Oct 02 '19 at 20:26
  • Thanks a lot for your solution. I can't ask questions for 3 days and I have an urgent request from you. I'm sorry but I can't set my Entry() to an int. It's very important for me to do it. I've tried setting my entry to a IntVar() and use it, it gave me an error called : TypeError: 'IntVar' Object cannot be interpreted as integer. I've tried putting the entry variable in int() and setting it to float then int and nothing worked. Please help me and I'm so sorry to ask here but I can't wait any longer and my deadline is getting closer. Thank you. – Mohamed Amr Oct 05 '19 at 16:52
  • @MohamedAmr I'm sorry but python is a language that I rarely use so I'm not at all fluent in it. – MikeT Oct 05 '19 at 21:56