select couple of random rows by different criteria in one query [closed] - mysql

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to select 4 random rows from a table where level = 1 and 4 random rows from the same table where level=2. How can I do this in 1 query ?

select * from (select * from your_table
where level = 1 order by rand() limit 4) x
union all
select * from (select * from your_table
where level = 2 order by rand() limit 4) y

try like this...
SELECT product_id, title, description FROM products WHERE active = 1 AND stock > 0 ORDER BY RAND() LIMIT 4;

Related

Simple SQL Union 2 queries on the same table [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Trying to get all rows where userid = 'me'
Then 2 newest rows where userid <> 'me'
Items:
Userid time
other2 11
other3 10
me 10
me 8
other1 8
other3 7
me 6
would return
Userid time
me 10
me 8
me 6
other2 11
other3 10
The results don't have to be in any order
(SELECT * FROM Items WHERE userid='me' )
UNION ALL
(SELECT * FROM Items WHERE userid<>'me'
ORDER BY time DESC LIMIT 2)
This only outputs 2 rows
The sql was correct as shown above
(SELECT * FROM Items WHERE userid='me' )
UNION ALL
(SELECT * FROM Items WHERE userid<>'me'
ORDER BY time DESC LIMIT 2)
Union All concatenates the two queries, and the parenthesis allow different WHERE/ORDER BY/LIMIT clauses.
Demonstrated here:
http://sqlfiddle.com/#!9/ca405/1/0

Select 10 rows where sold='false' from SQL [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm working on an IPN for PayPal, and I need to know how to select a certain amount of rows where sold='false'
The following should work:
SELECT * FROM your_table WHERE sold='false' LIMIT 10
select * from table where sold='false' limit 10
OR
select * from table where sold='false' limit M,N
this query fetches data starts from Mth row to N number of records
.
select top 10 * from table where sold='false'

MySQL 2 counts in query [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have this table, answers:
user
question
rightAnswer
ab
1
0
bc
1
1
de
1
1
bc
2
1
ab
2
1
de
2
0
fx
2
1
I want to create a view in phpMyAdmin that can display the question number, the total number of answers to that question and the number of right answers to that question.
I would like it to look like:
question
total answers
right answers
1
3
2
2
4
3
Could anyone help me to make this view? How do I create the query? I have tried something about count(*) where rightAnswer=1, but I cannot get it to count BOTH the total number of answers and the number of answers of each question.
try something like this:
select question,count(*),sum(rightAnswer)
from TABLE
group by question
order by question
select question, count(*) as total_answers, sum(rightAnswer) as right_answers
from your_table
group by question
order by question
Using ms sql syntax:
SELECT Question, totalAnswers.Total, rightAnswers.correct
FROM answers AS a
INNER JOIN (
SELECT Question, COUNT(rightanswer) AS correct
FROM answers
WHERE rightanswer = 1
GROUP BY Question
) rightAnswers
ON a.Question = rightAnswers.QUestion
INNER JOIN (
SELECT Question, COUNT(rightanswer) AS total
FROM answers
GROUP BY Question
) totalAnswers
ON a.Question = rightAnswers.QUestion

Mysql query - grouping results [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have 2 tables and basically what I like to do is group the results or counts together for display. Tried different version of mysql statement but not getting anywhere.
The 2 example tables are:
tbl_One
index O_priority
1 low
2 medium
3 high
tbl_Two
t_priority
2
1
3
3
2
3
1
1
1
expected results:
low = 4
medium = 2
high = 3
SELECT T1.O_priority,T2.c FROM tbl_One as T1 LEFT JOIN (SELECT count(*) as c,t_priority FROM tbl_Two GROUP BY t_priority) as T2 ON T1.index = T2.t_priority;
Join the tables, then group the results:
SELECT tbl_One.O_priority, COUNT(*)
FROM tbl_One JOIN tbl_Two ON tbl_Two.t_priority = tbl_One.index
GROUP BY tbl_One.index
See it on sqlfiddle.
Simple as much as you can, try this:
SELECT count(o.index) as `index`, o.O_priority
FROM tbl_One o join tbl_two t on t.t_priority = o.index
group by t.t_priority;
SQL Fiddle

SQL Query group shuffle [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I have a query like
select id, item, producer from table
The result is like this:
id item producer
1 apple A
2 pear A
3 peach A
4 orange B
5 strawberry B
6 melon B
I want to shuffle this result (order by id DESC) and get something like this
item producer
strawberry B
pear A
orange B
apple A
peach A
melon B
I DON'T want to display like this:
ALL A ITEM
ALL B ITEM
ALL C ITEM...
Use the rand function in ORDER BY like this:
select id, item, producer from table order by rand();
To Shuffle the selection you can use rand()
The Answer for the link below contains more information.
SELECT id, item, producer
FROM table
ORDER BY RAND()
MySQL: Shuffle a limited query result?
select id, item, producer from table order by rand()
Use Order BY rand() to randomly order the results