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;
Related
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
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.
**castID**
nm0000116
nm0000116
nm0000116
nm0000116
nm0000116
nm0634240
nm0634240
nm0798899
This is my table (created as a view). Now I want to list the castID which has the most count (in this case which is nm0000116, and how many occurences/count it has in this table ( should be 5 times) and I'm not quite sure which query to use
try
Select CastId, count(*) countOfCastId
From table
Group By CastId
Having count(*)
= (Select Max(cnt)
From (Select count(*) cnt
From table
Group By CastId) z)
SELECT
MAX(E),
castId
FROM
(SELECT COUNT(castId) AS E,castId FROM [directors winning movies list] GROUP BY castId) AS t
You could just return the topmost count using LIMIT:
SELECT castID,
COUNT(*) AS Cnt
FROM atable
GROUP BY castID
ORDER BY Cnt DESC
LIMIT 1
;
However, if there can be ties, the above query would return only one row. If you want all the "winners", you could take the count from the above query as a scalar result and compare it against all the counts to return only those that match:
SELECT castID,
COUNT(*) AS Cnt
FROM atable
GROUP BY castID
HAVING COUNT(*) = (
SELECT COUNT(*)
FROM atable
GROUP BY castID
ORDER BY Cnt DESC
LIMIT 1
)
;
(Basically, same as Charles Bretana's approach, it just derives the top count differently.)
Alternatively, you could use a variable to rank all the counts and then return only those that have the ranking of 1:
SELECT castID,
Cnt
FROM (
SELECT castID,
COUNT(*) AS Cnt,
#r := IFNULL(#r, 0) + 1 AS r
FROM atable
GROUP BY castID
ORDER BY Cnt DESC
) AS s
WHERE r = 1
;
Please note that with the above method the variable must either not exist or be pre-initialised with a 0 or NULL prior to running the query. To be on the safe side, you could initialise the variable directly in your query:
SELECT s.castID,
s.Cnt
FROM (SELECT #r := 0) AS x
CROSS JOIN
(
SELECT castID,
COUNT(*) AS Cnt,
#r := #r + 1 AS r
FROM atable
GROUP BY castID
ORDER BY Cnt DESC
) AS s
WHERE s.r = 1
;
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
So I'll show you what I'm trying to do and explain my problem, there may be an answer different to the approach I'm trying to take.
The query I'm trying to perform is as follows:
SELECT *
FROM report_keywords rk
WHERE rk.report_id = 231
AND (
SELECT SUM(t.conv) FROM (
SELECT conv FROM report_keywords t2 WHERE t2.campaign_id = rk.campaign_id ORDER BY conv DESC LIMIT 10
) t
) >= 30
GROUP BY rk.campaign_id
The error I get is
Unknown column 'rk.campaign_id' in 'where clause'
Obviously this is saying that the table alias rk is not making it to the subsubquery. What I'm trying to do is get all of the campaigns where the sum of the top 10 conversions is greater than or equal to 30.
The relevant table structure is:
id INT,
report_id INT,
campaign_id INT,
conv INT
Any help would be greatly appreciated.
Update
Thanks to Kickstart I was able to do what I wanted. Here's my final query:
SELECT campaign_id, SUM(conv) as sum_conv
FROM (
SELECT campaign_id, conv, #Sequence := if(campaign_id = #campaign_id, #Sequence + 1, 1) AS aSequence, #campaign_id := campaign_id
FROM report_keywords
CROSS JOIN (SELECT #Sequence := 0, #campaign_id := 0) Sub1
WHERE report_id = 231
ORDER BY campaign_id, conv DESC
) t
WHERE aSequence <= 10
GROUP BY campaign_id
HAVING sum_conv >= 30
Possibly use a user variable to add a sequence number to get the latest 10 records for each one, then use SUM to get the count of those.
Something like this:-
SELECT rk.*
FROM report_keywords rk
INNER JOIN
(
SELECT campaign_id, SUM(conv) AS SumConv
FROM
(
SELECT campaign_id, conv, #Sequence := if(campaign_id = #campaign_id, #Sequence + 1, 1) AS aSequence, #campaign_id := campaign_id
FROM report_keywords
CROSS JOIN (SELECT #Sequence := 0, #campaign_id := "") Sub1
ORDER BY campaign_id, conv
) Sub2
WHERE aSequence <= 10
GROUP BY campaign_id
) Sub3
ON rk.campaign_id = Sub3.campaign_id AND Sub3.SumConv >= 30
WHERE rk.report_id = 231