How to select certain numbers of groups in MySQL? - mysql

I have the table with data:
And for this table I need to create pegination by productId column. I know about LIMIT N,M, but it works with rows and not with groups. For examle for my table with pegination = 2 I expect to retrieve all 9 records with productId = 1 and 2 (the number of groups is 2).
So how to create pegination by numbers of groups ?
I will be very thankfull for answers with example.

One way to do pagination by groups is to assign a product sequence to the query. Using variables, this requires a subquery:
select t.*
from (select t.*,
(#rn := if(#p = productid, #rn + 1,
if(#rn := productid, 1, 1)
)
) as rn
from table t cross join
(select #rn := 0, #p := -1) vars
order by t.productid
) t
where rn between X and Y;
With an index on t(productid), you can also do this with a subquery. The condition can then go in a having clause:
select t.*,
(select count(distinct productid)
from t t2
where t2.productid <= t.productid)
) as pno
from t
having pno between X and Y;

Try this:
select * from
(select * from <your table> where <your condition> group by <with your group>)
LIMIT number;

Related

How to extract alternate rows from a table which has two rows(Employ_id, salary)?

I have a table with two rows(employeeId, salary), Now I want to extract alternate rows(even or odd) from that table. how to write code in sql?
Assuming you have a column that specifies the ordering of the table, then you can use variables to do what you want:
select t.*
from (select t.*, (#rn := #rn + 1) as seqnum
from table t cross join
(select #rn := 0) vars
order by col
) t
where mod(seqnum, 2) = 0;
ODD ROWS,
select * from
(select empno, ename, sal, rownum rn
from emp
order by empno)
where mod (rn, 2) <> 0;
EVEN ROWS
select * from
(select empno, ename, sal, rownum rn
from emp
order by empno)
where mod (rn, 2) = 0;
SQL tables represent unordered sets, so there is no such thing as "even" and "odd" rows. If I interpret the question as wanting rows where employeeid is even or odd, I would use:
where mod(employeeid, 2) = 0 -- or 1 or odd
If I interpret it as wanted rows -- ordered by employeeid -- were even or odd, then I would enumerate the rows. In the most recent versions of MySQL:
select t.*
from (select t.*,
row_number() over (order by employeeid) as seqnum
from t
) t
where mod(seqnum, 2) = 0; -- or 1 for odd

MySQL: Limiting result for WHERE IN list

Let's say there are millions of records in my_table.
Here is my query to extract rows with a specific name from list:
SELECT * FROM my_table WHERE Name IN ('name1','name2','name3','name4')
How do I limit the returned result per name1, name2, etc?
The following query would limit the whole result (to 100).
SELECT * FROM my_table WHERE Name IN ('name1','name2','name3','name4') LIMIT 100
I need to limit to 100 for each name.
This is a bit of a pain in MySQL, but the best method is probably variables:
select t.*
from (select t.*,
(#rn := if(#n = name, #rn + 1,
if(#n := name, 1, 1)
)
) as rn
from my_table t cross join
(select #n := '', #rn := 0) params
order by name
) t
where rn <= 100;
If you want to limit this to a subset of the names, then add the where clause to the subquery.
Note: If you want to pick certain rows -- such as the oldest or newest or biggest or tallest -- just add a second key to the order by in the subquery.
Try
SELECT * FROM my_table WHERE Name IN ('name1','name2','name3','name4') FETCH FIRST 100 ROWS ONLY

Sql - count the number of occurence for each duplicate value

Edit: What I want is to rank the frequency of all duplicates. I tried to use the code suggested earlier:
SELECT
t.*,
#rn:=IF(#name = name,
#rn + 1,
IF(#name:=name, 1, 1)) freq
FROM
(SELECT
*
FROM
student
ORDER BY name) t
CROSS JOIN
(SELECT #name:=NULL, #rn:=0) t2;
This, however, were able to count the frequency but only if they are consecutively ordered. See image below:
Instead of being counted as 3, number 1 under id 6 was counted back to 1.
It seems like you need every occurrence of the name, so a simple group won't do. Try something like this:
SELECT s.name AS "Name of Student",
(SELECT COUNT(name) FROM student ss WHERE s.name = ss.name) AS "Frequency of Late"
FROM student s
SELECT
name, COUNT(*)
FROM
TableName
GROUP BY
name
This query should work for you.
Use user variables for this:
SELECT
t.*,
#rn:=IF(#name = name,
#rn + 1,
IF(#name:=name, 1, 1)) freq
FROM
(SELECT
*
FROM
student
ORDER BY name) t
CROSS JOIN
(SELECT #name:=NULL, #rn:=0) t2;
EDIT:
If you want to order by date and freq later, you can do:
select * from (
/* the above query */
) t order by the_date_column desc, freq;
EDIT 2:
For the updated question, see below:
SELECT
*
FROM
(SELECT
t.*,
#rn:=IF(#number = number, #rn + 1, IF(#number:=number, 1, 1)) freq
FROM
(SELECT
*
FROM
your_table
ORDER BY number) t
CROSS JOIN (SELECT #number:=NULL, #rn:=0) t2) t
ORDER BY id;
DEMO # SQLFiddle
This is your solution
SELECT
Student_Name AS StudentName,count(Student_Name) Total
FROM
Student_Table
GROUP BY
Student_Name`

MySQL select first row then skip few

I'm trying to select first row then skip X next rows then select rest in one query. For example if I have (a,b,c,d,e) in table I need to select "a" (first row) then skip X=2 rows ("b", "c") and then select rest which is "d" and "e", all in one query. So the result would be a,d,e
Try
select *
from
(
select *, #rank := #rank + 1 as rank
from your_table
cross join (select #rank := 0) r
order by colA
) tmp
where rank = 1
or rank > 3
or
select * from your_table
order by colA
limit 1
union all
select * from your_table
order by colA
limit 4, 9999999
You can use a variable to generate a row number:
select
YourField,
YourOtherField
from
(
select id,
YourField,
YourOtherField,
#row := #row + 1 as rownum
from YourTable
cross join (select #row:=0) c
order by YourField -- The field you want to sort by when you say 'first' and 'fourth'
) d
where
rownum = 1 or rownum >= 4

Mysql query to get the row position in a table

I have table with id (store user id) and score in different match. I want what is the position of a user.
So for i try this sql fiddle;
in this I am getting all the row but I need only user having id 3 and it position in the table.
like this:
Score Postion
26 3
Even i try to do like this but no success
MySql: Find row number of specific record
With MySQL, how can I generate a column containing the record index in a table?
I got the answer: http://sqlfiddle.com/#!2/b787a/2
select * from (
select T.*,(#rownum := #rownum + 1) as rownum from (
select sum(score) as S,id from mytable group by id order by S desc ) as T
JOIN (SELECT #rownum := 0) r
) as w where id = 3
Updated sqlfiddle and above query. Now it is working perfectly.
I think this should do the trick:
SELECT totalScore, rownum FROM (
SELECT id,sum(score) AS totalScore,(#rownum := #rownum + 1) AS rownum
FROM mytable
JOIN (SELECT #rownum := 0) r
group by id) result
WHERE result.ID = 3;
just add a where clause
select x.id,x.sum,x.rownum
from(
select id,sum(score) as sum,(#rownum := #rownum + 1) as rownum
from mytable
JOIN (SELECT #rownum := 0) r
group by id
) x
where id =3