marcick Well-Known Member Licensed User Longtime User May 6, 2015 #1 Hi, I need to extract the most recent record of a certain Id, is this way correct or there is a more quick and elegant way using the Max function ? Code: Dim Cursor1 As Cursor Dim query As String="SELECT * FROM records WHERE ID = John ORDER BY InsDate DESC LIMIT 1" Cursor1 = MySub.SQL_Bls1.ExecQuery(query) Cursor1.Position=0 Log(Cursor1.GetString("InsDate") & " - " & Cursor1.GetInt("Size")) Thanks
Hi, I need to extract the most recent record of a certain Id, is this way correct or there is a more quick and elegant way using the Max function ? Code: Dim Cursor1 As Cursor Dim query As String="SELECT * FROM records WHERE ID = John ORDER BY InsDate DESC LIMIT 1" Cursor1 = MySub.SQL_Bls1.ExecQuery(query) Cursor1.Position=0 Log(Cursor1.GetString("InsDate") & " - " & Cursor1.GetInt("Size")) Thanks
CanguroCode Active Member Licensed User Longtime User May 6, 2015 #2 I use this query: Code: "SELECT MAX(id) FROM table1" Upvote 0
marcick Well-Known Member Licensed User Longtime User May 6, 2015 #3 CanguroCode said: I use this query: Code: "SELECT MAX(id) FROM table1" Click to expand... Yes, but this way I just get the max value, how to get the entire record that has the max value of that column ? Upvote 0
CanguroCode said: I use this query: Code: "SELECT MAX(id) FROM table1" Click to expand... Yes, but this way I just get the max value, how to get the entire record that has the max value of that column ?
DonManfred Expert Licensed User Longtime User May 6, 2015 #4 Code: SELECT * FROM table1 ORDER BY id DESC LIMIT 0,1; Upvote 0
marcick Well-Known Member Licensed User Longtime User May 6, 2015 #5 This is how I do. I was asking if, instead to use cursor, was possible to retrieve an entire record using the max function. Upvote 0
This is how I do. I was asking if, instead to use cursor, was possible to retrieve an entire record using the max function.
DonManfred Expert Licensed User Longtime User May 6, 2015 #6 marcick said: I was asking if, instead to use cursor, was possible to retrieve an entire record using the max function. Click to expand... You can select the hole record and do a subselect to get the max value too Like this Code: select *,(select max(id) from table) as maxid from table where ... ORDER BY field ASC LIMIT 0,x But i DONT know whether it works with sqlite like in mysql too give it a try Upvote 0
marcick said: I was asking if, instead to use cursor, was possible to retrieve an entire record using the max function. Click to expand... You can select the hole record and do a subselect to get the max value too Like this Code: select *,(select max(id) from table) as maxid from table where ... ORDER BY field ASC LIMIT 0,x But i DONT know whether it works with sqlite like in mysql too give it a try
E edgar_ortiz Active Member Licensed User Longtime User May 7, 2015 #7 marcick said: Hi, I need to extract the most recent record of a certain Id, is this way correct or there is a more quick and elegant way using the Max function ? Code: Dim Cursor1 As Cursor Dim query As String="SELECT * FROM records WHERE ID = John ORDER BY InsDate DESC LIMIT 1" Cursor1 = MySub.SQL_Bls1.ExecQuery(query) Cursor1.Position=0 Log(Cursor1.GetString("InsDate") & " - " & Cursor1.GetInt("Size")) Thanks Click to expand... In fact that's the best way. Upvote 0
marcick said: Hi, I need to extract the most recent record of a certain Id, is this way correct or there is a more quick and elegant way using the Max function ? Code: Dim Cursor1 As Cursor Dim query As String="SELECT * FROM records WHERE ID = John ORDER BY InsDate DESC LIMIT 1" Cursor1 = MySub.SQL_Bls1.ExecQuery(query) Cursor1.Position=0 Log(Cursor1.GetString("InsDate") & " - " & Cursor1.GetInt("Size")) Thanks Click to expand... In fact that's the best way.
S sorex Expert Licensed User Longtime User May 7, 2015 #8 here's another method... Code: SELECT * FROM records WHERE ID = (select max(ID) from records) no sorting involved so it might be faster. Upvote 0
here's another method... Code: SELECT * FROM records WHERE ID = (select max(ID) from records) no sorting involved so it might be faster.
marcick Well-Known Member Licensed User Longtime User May 7, 2015 #9 Thanks everybody, but anyway we are talking of using then the cursor to extract the columns. The result of the query is just one record, so I have to set the cursor to 0 and scan the columns. I was asking myself if, with ExecQuerySingleResult instead of ExecQuery, was possible to extract a whole record and not a single field. Upvote 0
Thanks everybody, but anyway we are talking of using then the cursor to extract the columns. The result of the query is just one record, so I have to set the cursor to 0 and scan the columns. I was asking myself if, with ExecQuerySingleResult instead of ExecQuery, was possible to extract a whole record and not a single field.
S sorex Expert Licensed User Longtime User May 7, 2015 #10 why do you need to scan the columns? * returns all record fields so you can just access them Upvote 0
marcick Well-Known Member Licensed User Longtime User May 7, 2015 #11 look to my first post. I need to extract the most recent record of a certain Id, then scan the various column. I did it with a cursor, how would you do in a single line ? Upvote 0
look to my first post. I need to extract the most recent record of a certain Id, then scan the various column. I did it with a cursor, how would you do in a single line ?
S sorex Expert Licensed User Longtime User May 7, 2015 #12 somthing like Code: records=SQL.querySQLite("SELECT * FROM records WHERE ID = (select max(ID) from records)") records.Position=0 log(records.GetString("ID")) Upvote 0
somthing like Code: records=SQL.querySQLite("SELECT * FROM records WHERE ID = (select max(ID) from records)") records.Position=0 log(records.GetString("ID"))
S sorex Expert Licensed User Longtime User May 7, 2015 #13 ok, I misinterpret your initial post, your query was already good. I thought you wanted the last ID as single recordset. Upvote 0
ok, I misinterpret your initial post, your query was already good. I thought you wanted the last ID as single recordset.
marcick Well-Known Member Licensed User Longtime User May 7, 2015 #14 ok, thanks again anyway ... Bye Upvote 0