Mysql Pagination - mysql

I am trying to understand how Pagination works in mySQL. Considering I have a lot of data that is to be retrieved based on select query how does adding different columns in the select statement change the pagination?
e.g.
Select name from employee; vs Select name, employeeId from employee;
Will using employeeId in the select field help in retrieving data in more efficient manner even though that field in not required. Adding it as employeeId is indexed.
Thanks

Pagination deals with rows, not columns, and is as simple as a LIMIT clause:
LIMIT 10 OFFSET 10
Would give you 10 rows starting from the 10th record.

There is no implicit pagination in MySQL. If you are looking to implement pagination based on your query results, the LIMIT clause may come handy.
For example:
select name from employee limit a,b;
will return b rows from the table employee. These would be row numbers a+1 through a+b
select name from employee 0, 10
would return rows 1 through 10.
Using any number of columns will not affect the way you limit the number of rows which is normally referred to as pagination.

Related

Limiting the count query in MySQL?

I am trying to do a simple test where I'm pulling from a table the information of a specific part number as such:
SELECT *
FROM table_name
WHERE part_no IN ('abc123')
This returns 25 rows. Now I want to count the number that meet the "accepted" condition in a specific column but the result is limited to only the 10 most recent. My approach is to write it as follows:
Select Count(*)
FROM table_name
WHERE part_no IN ('abc123') AND lot IN ('accepted')
ORDER BY date DESC
LIMIT 10
I'm having a hard time to get the ORDER BY and LIMIT operations to work. I could use help just getting it to limit appropriately, and I can figure out the rest from there.
Edit: I understand that the operations are happening on the COUNT which only returns one row with a value; but I put the second clip to show where I am stuck in my thought process.
Your query SELECT Count(*) FROM ... will always return exactly one row.
It's not 100% clear what exactly you want to do, but if you want to know how many of the last 10 have been accepted, you could use a subquery - something like:
SELECT COUNT(*) FROM (
SELECT lot
FROM table_name
WHERE part_no IN ('abc123')
ORDER BY date DESC
LIMIT 10
)
WHERE lot IN ('accepted')
The inner query will return the 10 most recent rows for part abc123, then the outer query will count the accepted ones.
There are also other solution (for example, you could have the inner query output a field that is 0 when the part is not accepted and 1 when the part is accepted, then take the sum). Depending on which exact dialect/database you are using, you may also have more elegant options.
Select count returns ONE ROW therefore the ORDER BY and the LIMIT will not work on the results

Retrieve a single distinct value each time the query is ran

i have a column full of resort id's say 44 rows, i am using the following query
Query-> SELECT DISTINCT RESORT ID FROM Schema.table Name WHERE Condition='Value' AND ROW NUMBER= 1
the above query returns one value say='15'
when i run it multiple it is returning the same value ='15'!!!
i require a different value each time the query is ran
could any one please help me out.
Thanks,
You will get Random Resort ID by using this
SELECT TOP 1 ResortID FROM TableName
-- WHERE <Condition>
ORDER BY NEWID() ASC

Trouble selecting a single random row from MySQL table

I am trying to select a user at random from a very simple table for the purposes of generating sample data.
The table has just two columns, the integer primary key users_id which has all the values from 1 to 46 inclusive, and uname which is a varchar(60).
The query
select relusers.uname from relusers where relusers.users_id=floor(rand()*46+1);
is returning multiple rows. Perhaps I've been staring at this for too long but I fail to see how the above query could ever return more than one row. floor() returns a single integer which is being compared to the primary key column. Including users_id in the selection shows multiple different IDs being selected. Zero rows as a result I can understand, but multiple? Any ideas?
Your code is returning multiple rows because rand() is evaluated on each row. So, you have the change of multiple matches. And a chance of no matches at all.
You can use your idea, but try it this way:
select relusers.uname
from relusers cross join
(selext #rand := rand()) const
where relusers.users_id = floor(#rand*46+1);
This generates just one random value and hence just one row. But, with only 46 rows, the order by method should perform well enough:
select relusers.uname
from relusers
order by rand()
limit 1;
select * from relusers order by rand() limit 1

How can I select the second last and last row from the database?

When we are retrieving data from database, we use something like this
SELECT * FROM Names
However, we will get all the data inside the specific table. Since I am going to update some data to the database and want to make a comparison bewteen the last row of data in the db and the most updated data, how can I select and retrieve the last two row of the database only?
If you were using SQL Server, you would do something like this:
SELECT TOP 2 * FROM Names ORDER BY Name DESC
SQL Server syntax:
SELECT TOP 2 column_name FROM table_name ORDER BY column_name DESC;
Example:
If you want to retrieve the last value of the customer_name column from the customers table, you need to write the following query:
SELECT LAST (CUSTOMER_NAME) AS LAST_CUSTOMER FROM CUSTOMERS;
You should include a column in the Names table to keep track of when a name was added to the table since you cannot guarantee that the rows are in the order that they were inserted. With that column you can use the order by clause..
In MySQL Syntax:
SELECT *
FROM Names
ORDER BY order_column DESC
LIMIT 2;
If you want to get the last rows as they are, you cannot order the table by the inserted names because that is just getting the 2 names that come last in an alphabetically sorted list of names. You could try something like this where you include an offset in the limit clause if you get the number of rows in the table:
SELECT *
FROM Names
LIMIT *num_rows*-2, 2;
You would have to know the number of rows to use this query or use an additional query to implement a row count that works with limit. This, however, still may not be accurate since the system may not order the rows as they were inserted. I still recommend the first option where you keep track of order/time a name was inserted.

Finding the sum of values in a column for the last n rows

I'm trying to find the sum of values in a particular column for the last ten rows selected by some criteria and ordered by some column. I tried the obvious:
SELECT SUM(column) AS abc FROM table WHERE criteria ORDER BY column DESC LIMIT 10
However this seems to sum the entire column!?
So after playing around this seems to work:
SELECT SUM(column) AS abc FROM (SELECT column FROM table WHERE criteria ORDER BY column DESC LIMIT 10) AS abc
My questions...
Why doesn't the more intuitive approach work?
I could access the result by using $data[0], but I prefer to have some meaningful variable. So why do I need to do AS abc twice?
Is there a tidier/better way to do the job?
I'm quite inexperienced with SQL queries so I would really appreciate any help.
Because mysql runs query in the following order:
FROM->WHERE->GROUP BY->HAVING->ORDER BY->LIMIT.
So limit will be applied after grouping and will filter groups but not ordinary rows.
Regarding abs twice: it's necessary to add alias for all derived queries. This is mysql rule.