MrClamps
MrClamps

Reputation: 31

sqlite3 max function

I have a simple database of two tables, User,Customer. Both these have a smallint id. I ran this command

 select max(u.id,c.id) as maxthing from Users u join Customers c;

I got pretty much all the id's in both tables. I tried looking at the docs for max but it dosent say I can pass a second parameter. Can someone explain what is happening here, what happens when I pass in a second parameter to max()?

Upvotes: 0

Views: 150

Answers (1)

sagi
sagi

Reputation: 40471

That happens because you are making a Cartesian join , each record joins to each records so got are multiplying the results. You need to add a join relation :

 select max(u.id,c.id) as maxthing
 from Users u
 join Customers c
  ON(u.ID = c.User_ID)

Upvotes: 1

Related Questions