SQL - How to select discontinuous rows from a table with one select?

Tiange Luo
 `SELECT * FROM Post
  WHERE Tid = Id
  ORDER BY Time
  LIMIT 0,1`

 `SELECT * FROM Post
  WHERE Tid = Id
  ORDER BY Time
  LIMIT start,offset;`

Can I use only one SELECT to complete this? Just like

  `SELECT * FROM Post
  WHERE Tid = Id
  ORDER BY Time
  LIMIT 0,1 and start,offset;`
Shadow

In this case combine the 2 sql statements in a union, since you cannot provide multiple limit clauses in a single select:

SELECT * FROM Post LIMIT 1,2
UNION ALL
SELECT * FROM Post LIMIT 5,6;

However, I would add an order by clause to the 2 select statements just to make 100% sure you know which records will be selected.

UPDATE: Technically you could do this in a single statement using a running counter and filtering on the counter in where. However, that would not really be a good idea from performance wise, since mysql would have to loop through all records within the table. It cannot know which records would satisfy the criteria. Limit clauses are better optmised.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

select from one table, insert into another table oracle sql query

From Dev

Select unique pairs of rows from one table that are associated to the same value in another table in SQL

From Dev

SQL Select to return 2 rows one for each column in table

From Dev

SQL select one row from table 1 joining on multiple rows in table 2

From Dev

select multiple column from one table and insert into another as rows

From Dev

Select percentage of rows from SQL table?

From Dev

How can i use SQL Select Insert to copy rows from one table to another

From Dev

select a random rows from table

From Dev

sql select from table then compare with other table in one statement

From Dev

How do I select rows in 1 table containing keywords from another table in SQL?

From Dev

MS SQL how to select n rows from table even if table has n - x rows

From Dev

How to select a specific row from the table with one column as a sum of values of other rows?

From Dev

Select rows from a table if one of them matches with another table

From Dev

How to select all rows from a table given conditions of rows?

From Dev

Select alternate rows from SQL Server table

From Dev

How to select data from several rows in a single table in one result row?

From Dev

How to select a quantity of rows from one table based on quantities from another

From Dev

How do I select rows in 1 table containing keywords from another table in SQL?

From Dev

How to select rows from one data.table to apply in another data.table?

From Dev

Select random rows from table

From Dev

SQL: How select rows and minus from another table

From Dev

Select clustered rows from a table

From Dev

SQL Server - How to SELECT values from different rows but in the same table

From Dev

how to select one row from one table and multiple rows from other table using joins in mysql,

From Dev

SQL: How to select multiple values from one table as seperate columns

From Dev

Postgres select from one to many table to single table rows

From Dev

How to SELECT none matching rows from one table to another?

From Dev

PHP SQL - Multiple select from one table

From Dev

How to select from one dbms_sql.number_table into another

Related Related

  1. 1

    select from one table, insert into another table oracle sql query

  2. 2

    Select unique pairs of rows from one table that are associated to the same value in another table in SQL

  3. 3

    SQL Select to return 2 rows one for each column in table

  4. 4

    SQL select one row from table 1 joining on multiple rows in table 2

  5. 5

    select multiple column from one table and insert into another as rows

  6. 6

    Select percentage of rows from SQL table?

  7. 7

    How can i use SQL Select Insert to copy rows from one table to another

  8. 8

    select a random rows from table

  9. 9

    sql select from table then compare with other table in one statement

  10. 10

    How do I select rows in 1 table containing keywords from another table in SQL?

  11. 11

    MS SQL how to select n rows from table even if table has n - x rows

  12. 12

    How to select a specific row from the table with one column as a sum of values of other rows?

  13. 13

    Select rows from a table if one of them matches with another table

  14. 14

    How to select all rows from a table given conditions of rows?

  15. 15

    Select alternate rows from SQL Server table

  16. 16

    How to select data from several rows in a single table in one result row?

  17. 17

    How to select a quantity of rows from one table based on quantities from another

  18. 18

    How do I select rows in 1 table containing keywords from another table in SQL?

  19. 19

    How to select rows from one data.table to apply in another data.table?

  20. 20

    Select random rows from table

  21. 21

    SQL: How select rows and minus from another table

  22. 22

    Select clustered rows from a table

  23. 23

    SQL Server - How to SELECT values from different rows but in the same table

  24. 24

    how to select one row from one table and multiple rows from other table using joins in mysql,

  25. 25

    SQL: How to select multiple values from one table as seperate columns

  26. 26

    Postgres select from one to many table to single table rows

  27. 27

    How to SELECT none matching rows from one table to another?

  28. 28

    PHP SQL - Multiple select from one table

  29. 29

    How to select from one dbms_sql.number_table into another

HotTag

Archive