I am trying to figure out where I am going wrong here. This has worked for me in the past, but that may because I was using MariaDB and now this is mySQL 8. I am trying to pagination with rank. Is this SQL no longer valid on mySQL 8? Is there a better way to do this?
SELECT
*
FROM
(
SELECT
(#row_number:=#row_number + 1) as 'rank',
a.*
FROM
(
SELECT
deals.id
FROM
deals
) AS a
ORDER BY
utc_created DESC
) AS x,
( SELECT #row := 0 ) AS r
WHERE
1 = 1
AND rank >= 1
AND rank < 51
ORDER BY
rank ASC
rank is a reserved word, and i think your table r is in the wrong position
SELECT
*
FROM
(
SELECT
#rownum := #rownum + 1 'rank',
a.*
FROM
(
SELECT
foo.id
FROM
foo
) AS a ,
( SELECT #rownum := 0 ) AS r
ORDER BY
utc_created DESC
) AS x
WHERE
1 = 1
AND `rank` >= 1
AND `rank` < 51
ORDER BY
`rank` ASC
Related
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'
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
I have following my sql query, I want to take limit start index from the other table, How could I do it?
SELECT std.totalmarks
FROM student as std
WHERE std.status=1
ORDER BY (std.datetime) ASC
LIMIT (
SELECT us.startnum
FROM user AS us
WHERE us.username='abc'
),10
select q.totalmarks from
(
SELECT *,#curRow := #curRow + 1 AS row_number
FROM student as std JOIN (SELECT #curRow := 0) r
WHERE std.status=1
ORDER BY (std.datetime) ASC
) q
where row_number>(
SELECT us.startnum
FROM user AS us
WHERE us.username='abc'
)
limit 10
select * from
(SELECT std.totalmarks, numstart.startnum, #n:=#n+1 as number
FROM student as std,
(SELECT us.startnum
FROM user AS us
WHERE us.username='abc') as numstart,
(SELECT #n:=0) sess_var
WHERE std.status=1
ORDER BY (std.datetime) ASC) res
where number>=startnum
LIMIT 0,10
I am looking for help converting my MySQL query to work in PostgreSQL. This is a query on the model of RoR application.
Any help is appreciated.
SELECT * FROM(
SELECT #row := #row + 1 AS rownum, id, device_id, name, quarterly
FROM (SELECT #row :=0) r, recurrent_tests
WHERE device_id = "+self.id.to_s+" AND quarterly = 1
ORDER BY name ASC
) ranked
WHERE (rownum-1) % 4 = "+(i-1).to_s)
select *
from (
select row_number() over (order by name asc) as rownum,
id,
device_id,
name,
quarterly
from recurrent_tests
) t
where rownum - 1 % 4 = ...
For more details on window functions (the over (...) clause) please see the manual:
http://www.postgresql.org/docs/current/static/tutorial-window.html
I've been wrestling with this query for a while. Here it is:
select First10.mal, Last10.family_name,
(select * from gender2
JOIN
(select * from status) as Mix1
JOIN
(select * from age as Mix2 order by rand() limit 10) as Mix3
JOIN
(select incidentid from incidentid2 as Mix4)
as Mix5 where data='mal' and incidentid='6' and status IN ('inj','ali') and age IN ('NULL','0-17')
order by rand() limit 100)
from ( select fn.mal, #fns := #fns + 1 as Sequence
from ( select mal from fnames where mal IS NOT NULL order by rand() limit 100) fn,
(select #fns := 0 ) vars ) First10
JOIN
( select ln.family_name, #lns := #lns + 1 as Sequence
from ( select family_name from lastnames order by rand() limit 100 ) ln,
(select #lns := 0 ) vars ) Last10
ON First10.Sequence = Last10.Sequence;
My goal here is to insert into a table which would present a randomized first name, last name, gender, status, age, and incident id. I have tried many ways to rework this script, including seperating the select statements, but I always seem to end up with this error:
ERROR 1241 (21000): Operand should contain 1 column(s)
Please advise guys, this has been stressing me out for a while now... It's probably a very simple answer, but I am just confusing myself further. If you need any clarifications, just ask.
Well, it looks like after a good night's sleep and some help from a friend, I got this query working. For those that are looking for an answer to a similar question, here is how I got this to work:
select mal,family_name,data,age,status,incidentid
from ( select fn.mal, #fns := #fns + 1 as Sequence
from ( select mal from fnames where mal IS NOT NULL order by rand() limit 100) fn,
(select #fns := 0 ) vars ) as FN
INNER JOIN
(select ln.family_name, #lns := #lns + 1 as Sequence
from ( select family_name from lastnames order by rand() limit 100 ) ln,
(select #lns := 0 ) vars ) as LN
INNER JOIN
(select * from gender2) as Mix0
INNER JOIN
(select * from status) as Mix1
INNER JOIN
(select * from age as Mix2 order by rand() limit 3) as Mix3
INNER JOIN
(select incidentid from incidentid2 as Mix4)
as Mix5 where data='mal' and incidentid='6' and status IN ('inj','ali') and age IN ('NULL','0-17')
order by rand() limit 100;