I want to use the "order by rand alternative" query (bottom) to get a random set of results but I want to get them from within the results a query such as:
SELECT t2.id FROM index_table t1 JOIN data_table t2 ON t1.id= t2.index_id
And I need to limit the number of random results I'd get back.
I can't quite get my head around the syntax I'd need to use, any help greatly appreciated.
thanks
"order by rand alternative" query:
How can i optimize MySQL's ORDER BY RAND() function?
SELECT *
FROM (
SELECT #cnt := COUNT(*) + 1,
#lim := 10
FROM t_random
) vars
STRAIGHT_JOIN
(
SELECT r.*,
#lim := #lim - 1
FROM t_random r
WHERE (#cnt := #cnt - 1)
AND RAND(20090301) < #lim / #cnt
) i
SELECT
t2.id
FROM
index_table t1
JOIN data_table t2
ON t1.id= t2.index_id
ORDER BY
RAND()
LIMIT 5
or whatever the maximum number of entries you want via the limit
Would something like this work?
SELECT *
FROM (
SELECT #cnt := COUNT(*) + 1,
#lim := 10
FROM FROM index_table t1 JOIN data_table t2 ON t1.id= t2.index_id
) vars
STRAIGHT_JOIN
(
SELECT t2.id,
#lim := #lim - 1
FROM index_table t1 JOIN data_table t2 ON t1.id= t2.index_id
WHERE (#cnt := #cnt - 1)
AND RAND() < #lim / #cnt
) i
I removed the parameter from RAND otherwise my output was always the same.
Related
How to use Sql query for generating S.no from 1. I can't get the answer correctly
SELECT (#cnt := #cnt + 1) AS 'S.No',`barcode`,
(SELECT #cnt := 0) AS dummy
FROM wp_weblib_outitems
Try this:
SELECT #s:=#s+1 AS 'S.No',`barcode`
FROM wp_weblib_outitems,
(SELECT #s:= 0) AS s;
This is my query. i just want S.No as asc . How can i write this query as ascending order
SELECT DISTINCT (subject),a.title, a.callnumber,
a.author, a.recv_date,a.pubdate ,b.book_access_number,(#cnt := #cnt + 1) AS 'S.No'
from wp_weblib_collection a
inner join wp_weblib_book_log_reports b on a.barcode = b.book_access_number
$student
CROSS JOIN (SELECT #cnt := 0) AS dummy
GROUP BY subject asc
You can try with ROW_NUMBER() as given below.......
SELECT ROW_NUMBER() OVER (ORDER BY [Column1]) AS 'NO',[Column1],[column2],.... FROM tblUser;
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;
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 two tables:
exam_outline_items:
jml_quiz_pool:
Of all the things I've tried, this got me the closest:
select t1.sequence, t1.title, t2.q_cat, t2.q_count
from student_pl.exam_outline_items t1
cross join pe_joomla.jml_quiz_pool t2
where t1.exam_outline_id = 5 and t1.chapter_num > 0
and t2.q_id = 1109 and t2.q_count > 0
group by title
Which produces this result:
I just need those q_cat values to be different, like they are in the 2nd query.
Thanks in advance for your help.
You have to have something to connect them with. If you don't have such a column, you can simulate one by creating a rownumber with variables.
select sequence, title, q_cat, q_count from (
select t1.sequence, t1.title, #r1 := #r1 + 1 as rownumber
from student_pl.exam_outline_items t1
, (select #r1 := 0) var_init
where t1.exam_outline_id = 5 and t1.chapter_num > 0
order by t1.sequence
) a
inner join
(
select t2.q_cat, t2.q_count, #r2 := #r2 + 1 as rownumber
from pe_joomla.jml_quiz_pool t2
, (select #r2 := 0) var_init
where t2.q_id = 1109 and t2.q_count > 0
order by t2.q_cat
) b on a.rownumber = b.rownumber;
Also note, that I used order by in those queries. In a database you have no sort order unless you explicitly set it with order by.
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;