i have a table with about 100 rows , and i want every time to get rows between number and number , like this
if `i=1` i want to get the rows `0 1 2 3 4`
if `i=2` i want to get the rows `5 6 7 8 9`
if `i=3` i want to get the rows `10 11 12 13 14`
and maybe the last value of i will just take 3 or 4 rows not 5
i think the solution will be something like this
select * from question limit (i-1)*5 , i*5-1
but doesn't work , cos i don't know how to use variable in a query , and when i tried it for i=1 it doesn't work and i got a syntax error
The first parameter to LIMIT is the zero-based starting row index. The second one is the number of rows. So, if you're always looking for five rows:
SELECT * FROM question LIMIT (i-1)*5 , 5
(You'll have to calculate (i-1)*5 with whatever language you're using to build the query, and pass an actual number to MySQL).
Related
select * from "Test"."EMP"
id
1
2
3
4
5
Select SUM(1) FROM "Test"."EMP";
Select SUM(2) FROM "Test"."EMP";
Select SUM(3) FROM "Test"."EMP";
why the output of these queries is?
5
10
15
And
I don't understand why they write table name like this "Test"."EMP"
your table has 5 records. the statement select 1 from test.emp returns 5 records with values as 1 for all 5 records.
id
1
1
1
1
1
This is because db engine simply returns 1 for each existing record without reading the contents of the cell. and same happens for select <any static value> from test.emp
same happens for 2 and 3
id
2
2
2
2
2
hence there are 5 records returned with the static values and sum of those values will be the product of static number passed in the select statement and total records in the table
additional fact: It is always recommended to perform count(1) than count(*) as it consumes less resource and hence less load on the server
I don't think it's "Test"."EMP" with double quotes.. it's probably `Test`.`EMP` with backticks instead. The definition means its database_name.table_name. This is the recommended format to get the correct table_name from database_name; in this case, you're specifically making the syntax to query from `Test`.`EMP`. Read more about identifier qualifiers.
As for SUM(x), the x get's repeated according to the rows present in the table. So SUM(1) on 5 rows is 1+1+1+1+1, SUM(2) on 5 rows is 2+2+2+2+2, and so on.
I'm trying to write a query that will only return a recordset between 2 positions in a table
For example, using Excel to demonstrate, populate a recordset between positions 3 and 7
I thought about using BETWEEN with the ID field, but there may be times that a record has been deleted and the ID field auto-incrementation will be out.
Ultimately, I want to use the query to populate a list for reporting:
Page 1 shows Records 1 to 10
Page 2 shows Records 11 to 20
Page 3 shows Records 21 to 30 etc
Why not use limit and offset?
select t.*
from t
order by id
limit 10 offset 0;
Then for subsequent pages, you would have:
limit 10 offset 9;
limit 10 offset 19;
SELECT id,name,info FROM table LIMIT 5
the result Set should be contains 5 rows wich is The first 5 rows of the table,but is any exception about this usage? .the table like this :
SELECT * FROM table limit 10;
1. company_id company_name tel
1 TCL集团股份有限公司 0752-2288333
2 UNITEDSTACK(北京)科技有限公司 15727325616
3 《市政技术》杂志社有限公司 13401070358
4 《网络安全技术与应用》杂志社有限公司 010-62765013
5 《艺术市场》杂志社股份有限公司 64271947
7 一呼医知己健康咨询(北京)有限公司 010-62957992
8 一呼(北京)电子商务有限公司 62957992
9 一汽轿车股份有限公司 0431-85782608
10 一通万通商务服务(北京)有限公司 010-68061805
I use the first sql the result is normal:like this
SELECT company_id,company_name,tel FROM table LIMIT 5;
1. 1 TCL集团股份有限公司 0752-2288333
2 UNITEDSTACK(北京)科技有限公司 15727325616
3 《市政技术》杂志社有限公司 13401070358
4 《网络安全技术与应用》杂志社有限公司 010-62765013
5 《艺术市场》杂志社股份有限公司 64271947
However I use the second sql like this :
SELECT comapny_id,company_name FROM table LIMIT 5;
1275992
1758051
2990914
5241776
5344925
We are seeing the result is not the 5 rows of the table obviously,the difference of these fileds is that company_id is a primary key,company_name is a type of MUL.can you help me?thank you very much!
the result Set should be contains 5 rows wich is The first 5 rows of
the table,but is any exception about this usage?
Only the first part of this statement is correct. Your query returns 5 rows from the query. However, those are 5 indeterminate rows.
SQL tables represent unordered sets. Hence, there is no first five rows in a table. If you want your result set ordered, then you need to include an order by clause. Often, an auto-incremented id is used for this purpose, because such an id captures the order that rows are inserted into the table.
I'm trying to find an elegant way to write a query that only returns enough rows for a certain column to add up to at least n.
For example, let's say n is 50, and the table rows look like this:
id count
1 12
2 13
3 5
4 18
5 14
6 21
7 13
Then the query should return:
id count
1 12
2 13
3 5
4 18
5 14
Because the counts column adds up to n > 50. (62, to be exact)
It must return the results consecutively starting with the smallest id.
I've looked a bit into accumulators, like in this one: MySQL select "accumulated" column
But AFAIK, there is no way to have the LIMIT clause in an SQL query limit on an SUM instead of a row count.
I wish I could say something like this, but alas, this is not valid SQL:
SELECT *
FROM elements
LIMIT sum(count) > 50
Also, please keep in my the goal here is to insert the result of this query into another table atomically in an automated, performance efficient fashion, so please no suggestions to use a spreadsheet or anything that's not SQL compatible.
Thanks
There are many ways to do this. One is by using Correlated Subquery
SELECT id,
count
FROM (SELECT *,
(SELECT Isnull(Sum(count), 0)
FROM yourtable b
WHERE b.id < a.id) AS Run_tot
FROM yourtable a) ou
WHERE Run_tot < 50
I have 1 MYSQL table with 8000 records. I would like to query First name, Middle name, and Last name by the first characters of their full name. For instance, i have 40 rows of records (First name, Middle name, and Last name) which their first character of their name is SSS. My question is, how do i query "SSS" to only return 6 results and when i query "SSS" again i need another 6 results and so on until all the 40 records query ends.
I used the following query but only return the first 6 results and when i query "SSS" the next time, it returns the same results over and over. Please i need your help.
SELECT *
FROM Table
WHERE (UPPER(FName) LIKE UPPER('S%')
AND UPPER(MName) LIKE UPPER('S%')
AND UPPER(LName) LIKE UPPER('S%')
ORDER BY ID
LIMIT 6 OFFSET 0
Thanks in advance.
You're ordering by ID - good. Remember max ID of the last returned data set and when you query next time add condition ID > your_last_max_id
Second time, change offset:
LIMIT 6 OFFSET 6
and then
LIMIT 6 OFFSET 12
etc.