MySQL Over Convert from SQL to MySQL - mysql

Can anyone help me covert this SQL statement into MySQL.
Its being used for jQuery datatables for server side pagination.
I have tried using
#curRank := #curRank + 1 AS rank
instead of
row_number() OVER (ORDER BY item_id asc) AS RowNumber
but i keep getting a syntax error
CREATE TEMPORARY TABLE IF NOT EXISTS temp1 AS (SELECT * INTO temp1 FROM item where BLOCKED='0');
SELECT *
FROM
(SELECT row_number() OVER (ORDER BY item_id asc) AS RowNumber , *
FROM
(SELECT (SELECT count(temp1.NO)
FROM
temp1) AS TotalRows
, ( SELECT count(NO) FROM temp1 ) AS TotalDisplayRows ,*
FROM
temp1 ) RawResults) Results
WHERE
RowNumber BETWEEN 1 AND 10
Thanks for your help, Here is the query. But how can i return TotalRows and TotalDisplayRows into the data ? i had to manually add the item_id and description. The colums i get outputted are RowNumber, Item_Id, Description. I also need TotalRows amd TotalDisplayRows
set #rn := 0;
SELECT *
FROM (SELECT (#rn := #rn + 1) AS RowNumber,item_id,description
FROM
(SELECT (SELECT count(item.item_id)
FROM
item) AS TotalRows
, ( SELECT count(item_id) FROM item ) AS TotalDisplayRows ,item_id,description
FROM
item ) RawResults
) Results
WHERE
RowNumber BETWEEN 1 AND 10

You can use variables:
CREATE TEMPORARY TABLE IF NOT EXISTS temp1 AS (SELECT * INTO temp1 FROM item where BLOCKED='0');
SELECT *
FROM (SELECT (#rn := #rn + 1) AS RowNumber , *
FROM (SELECT (SELECT count(temp1.NO)
FROM temp1
) AS TotalRows,
(SELECT count(NO) FROM temp1 ) AS TotalDisplayRows ,*
FROM temp1
) RawResults CROSS JOIN
(SELECT #rn := 0) vars
ORDER BY item_id
) Results
WHERE RowNumber BETWEEN 1 AND 10
If you don't actually need the RowNumber column in the table, you could just use limit and offset.

SELECT * FROM (SELECT (#rn := #rn + 1) AS RowNumber,item_id,description
FROM
(SELECT (SELECT count(item.item_id)
FROM
item) AS TotalRows
, ( SELECT count(item_id) FROM item ) AS TotalDisplayRows ,item_id,description
FROM
item ) RawResults
) Results
WHERE RowNumber BETWEEN 1 AND 10

Related

MySQL get 2% of the record

I am trying to get 2% of the random sample record.
SELECT * FROM Orders
ORDER BY RAND()
LIMIT (SELECT CEIL(0.02 * (SELECT COUNT(*) FROM Orders)));
This one gives a syntax error due to line 3. Is there anything I am doing wrong?
Or is there a better way to get n % of records?
If you are using MySQL 8+, then ROW_NUMBER() provides one option:
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (ORDER BY RAND()) rn,
COUNT(*) OVER () cnt
FROM Orders
)
SELECT *
FROM cte
WHERE 1.0*rn / cnt <= 0.02;
On MySQL 5.7 and earlier, we can simulate row number:
SELECT *
FROM
(
SELECT *, (#rn := #rn + 1) AS rn
FROM Orders, (SELECT #rn := 0) AS x
ORDER BY RAND()
) t
CROSS JOIN (SELECT COUNT(*) AS cnt FROM Orders) o
WHERE 1.0*rn / cnt <= 0.02;

What would the equivalent statement be in MySQL 5.6.39?

I recently moved to a shared host that has MySQL 5.6.39 instead of MariaDB 10.x, I was wondering what would the equivalent of the following MariaDB statement in MySQL?
SELECT rank,
total
FROM
(SELECT ROW_NUMBER() OVER (
ORDER BY `prestige` DESC, `xp` DESC) AS rank,
(SELECT COUNT(*)
FROM Modular_LS) AS total,
steamid
FROM Modular_LS) sub
WHERE sub.steamid = '%s'
I got as far as this, but now I'm stuck
SELECT rank, total FROM
(SELECT #rank := #rank +1 as rank FROM Modular_LS,
(SELECT COUNT(*) FROM Modular_LS) AS total, steamid FROM Modular_LS) sub,
(SELECT #rank := 0) r ORDER BY `prestige` DESC, `xp` DESC) t;
The table structure contains the column steamid, xp, prestige
My goal is to order by prestige descending first and then xp descending to put it in a ranking like-order, then using WHERE query to find a specific player's ranking. The output of which contains the rank (position) and the total (total amount of records)
Maybe this will get you started:
SELECT #rank := IF(player_id = #prev, #rank + 1, 1), #prev := player_id
FROM ( SELECT #rank := 1, #prev = 0 ) AS init
JOIN ( SELECT player_id
FROM Modular_LS
ORDER BY prestige DESC, SP DESC
) AS x ;
After a few hours, this is what I came up with that solved my problem.
SELECT
sub.rank
,sub.total
FROM
(
SELECT
t.id
,t.steamid
,#rownum : = #rownum + 1 AS rank
,(
SELECT
COUNT (*)
FROM
Modular_LS
) AS total
FROM
Modular_LS t JOIN (
SELECT
#rownum : = 0
) r
ORDER BY
t.prestige DESC
,t.xp DESC
) sub
WHERE
sub.steamid = '%s'

How to select certain numbers of groups in 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;

MySQL Simple row num incremental counter

I'm trying to produce an incremental counter (column 'rownum' in the query below) in an SQL select clause. the counter should start over each time a new user switches.
SELECT * FROM (
SELECT CONCAT(' ',g.node1,' ',g.node2),
#r:= CASE WHEN #g = g.`user` THEN #r +1 ELSE 1 END rownum,
#g:= g.`user` user_group
FROM sn.sn_graph_reduced g
CROSS JOIN (SELECT #g:=0,#r:=0) t2
ORDER BY `user` , RAND()
) t
WHERE rownum <= 100
However, the above code snippet returns the row number, and since the records are RANDOMLY sampled, the row numbers are not incremental.
What I need is a simple counter (1,2,3....) for each row returned.
thanks
Try putting the variable outside of the inline view:
SELECT t.*,
#r:= CASE WHEN #t = t.`user` THEN #r +1 ELSE 1 END rownum,
#t:= t.`user` user_group
FROM (
SELECT CONCAT(' ',g.node1,' ',g.node2), g.`user`
FROM sn.sn_graph_reduced g
ORDER BY `user` , RAND()
) t
CROSS JOIN (SELECT #t:=0,#r:=0) t2
where rownum <= 100
the method for incrementing row number doesn't cope with RAND() directly, so use values from rand() as a column. Also initiate #g as '' not zero and then you need a final ORDER BY.
SELECT
*
FROM (
SELECT
CONCAT(' ', g.node1, ' ', g.node2) AS node_concat
, #r:= IF(#g = g.`user`, #r + 1, 1) AS rownum
, #g:= g.`user` AS user_group
FROM (
SELECT *, rand() AS R FROM sn_graph_reduced
) g
CROSS JOIN ( SELECT #g:= '' ,#r:= 1 ) t2
ORDER BY
`user`
, R
) t
WHERE rownum <= 100
ORDER BY
user_group
, rownum
;
see: This SQLfiddle

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