daniio
daniio

Reputation: 61

How to specify query in sqlite with min() function

I have table defined by code below

CREATE TABLE Products
(
P_Id INTEGER PRIMARY KEY,
name TEXT,
price REAL,
sellPrice REAL,
plu INTEGER,
codeBar TEXT,
tax INTEGER,
amount INTEGER,
date TEXT
);

and when I try to execute query like that I get a syntax error (it's a prepared statement)

select * 
from Products 
where P_Id = min(select P_Id from Products where codeBar=?);

Can somebody please help? What is wrong with this query?

The exact error message I get is:

java.sql.SQLException: near "select": syntax error

Thx for help.

Upvotes: 0

Views: 230

Answers (2)

John Woo
John Woo

Reputation: 263723

MIN() should be inside the subquery.

select * 
from Products 
where P_Id = (select min(P_ID) from Products where codeBar=?);

Upvotes: 5

Ilion
Ilion

Reputation: 6872

I believe what you want is this:

select * from Products 
where P_Id = (select min(P_Id) from Products where codeBar=?);

Upvotes: 1

Related Questions