Using MYSQL GROUP_CONCAT with sub query

user3602290

I am trying to get my head around using GROUP_CONCAT within MYSQL.

Basically I have the following table, table1:

id, field1, field2, active

I want to bring back 5 rows within the table but in random order. So I'm using this:

SELECT GROUP_CONCAT(id ORDER BY rand()) FROM table1 WHERE active=1

This behaves as I would expect. I then want to use the output to select the other columns (field1, field2) from the table and display the results. So I've tried using:

SELECT *
FROM table1
WHERE id IN
(
    SELECT GROUP_CONCAT(id ORDER BY rand()) as id FROM table1 WHERE active=1
);

I expected something like the above to work but I cant figure out why it doesn't. It DOES bring back results but not all of them, (i.e.) my table contains 10 rows. 6 rows are set to active=1. Therefore I would expect 6 rows to be returned ... this isn't happening I may get 1,2 or 0.

Additionally if it helps I'd like to limit the number of results returned by the sub-query to 3 but adding LIMIT doesn't seem to have any affect on the results returned.

Thank you in advance for your help

Tom

I think this is what you are looking for. This will bring back 5 random active rows.

SELECT  *

FROM    table1

WHERE active=1

ORDER BY RAND()

LIMIT 5;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using MYSQL GROUP_CONCAT with sub query

From Dev

Mysql group_concat for count in sub query using parent filed is not allowed

From Dev

Mysql query using IN with group_concat result

From Dev

Using Mysql GROUP_CONCAT in JOIN Query

From Dev

how to use select query in a Group_concat sub query in mysql

From Dev

Mysql GROUP_CONCAT and IN query

From Dev

mySQL GROUP_CONCAT - Query

From Dev

GROUP_CONCAT not working in Sub-Query

From Dev

Optimize MySQL query for group_concat function

From Dev

Mysql JOIN with GROUP_CONCAT in a complex query

From Dev

MySQL GROUP_CONCAT Query Excluding Records

From Dev

mysql query join group_concat

From Dev

Mysql group_concat query gives error

From Dev

Select query with GROUP_CONCAT in mysql

From Dev

How to fix this query? Using GROUP_CONCAT

From Dev

mysql request using join and group_concat

From Dev

mysql request using join and group_concat

From Dev

MySQL: How to sort when using concat in group_concat?

From Dev

MySQL: How to sort when using concat in group_concat?

From Dev

Error when I used GROUP_CONCAT on mysql query

From Dev

Issue with GROUP_CONCAT AND ORDER BY FIELD in MySQL query

From Dev

Query using group_concat is returning only one row

From Dev

Query using group_concat is returning only one row

From Dev

MySql duplicated values in a join using GROUP_CONCAT

From Dev

How to get two variables using MYSQL GROUP_CONCAT?

From Dev

Mysql - optimisation - multiple group_concat & joins using having

From Dev

MySql duplicated values in a join using GROUP_CONCAT

From Dev

How to get two variables using MYSQL GROUP_CONCAT?

From Dev

MySQL Group_Concat Not In

Related Related

HotTag

Archive