Search code examples
mysqlphpmyadmingroup-concat

REGXP set of values though GROUP_CONCAT from subquery


I have this horrible query,

REMOVED

Now this worked perfectly fine when the user only liked one band however this wont work when a user likes 2 or more bands due to subquery returning more than one result however i tried to then GROUP_CONCAT i ended up with the WHERE clause like below

REMOVED

and this results in the error #1111 - Invalid use of group function

if some one would be able to help me do this as i really dont want to have to put the load onto the Apache server pulling the results into php to get values and fire loads of queries off also this could end up being hundreds or possibly thousands of query for one section of a page

UPDATE 2: WooHoo sort of...

SELECT 
    B.*,
    (SELECT COUNT(*) FROM `blog_likes` AS BL WHERE BL.`blog_id` = B.`blog_id` ) AS likes
FROM blog AS B
WHERE `tags` REGEXP (
    SELECT GOUP_CONCAT(ConcatBandNames, '|') 
    FROM (
        SELECT CONCAT(
            CONCAT(
                '%',
                (
                    SELECT 
                        `band_name` 
                    FROM `band` AS BA 
                    WHERE BA.`band_id` = 
                    ( 
                        SELECT `band_id` 
                        FROM `likes` AS L 
                        WHERE `user_id` = '7' 
                        AND `band_id` = BA.`band_id` 
                    )
                )
            )
            , '%'
        ) AS ConcatBandNames
    ) AS T
)

Work's in terms of syntax however now it it's erroring saying #2014 - Commands out of sync; you can't run this command now


Solution

  • This is how it was supposed to be GROUP_CONCAT(CONCAT('.*',CONCAT(band_name, '.*')) SEPARATOR '|') so my query now looks like

    SELECT 
        B.*,
        (SELECT COUNT(*) FROM `blog_likes` AS BL WHERE BL.`blog_id` = B.`blog_id` ) AS likes
    FROM blog AS B
    WHERE `tags` REGEXP (
        SELECT 
        GROUP_CONCAT(CONCAT('.*',CONCAT(`band_name`, '.*')) SEPARATOR '|') 
        FROM `band` AS BA 
        WHERE BA.`band_id` = 
        ( 
            SELECT `band_id` 
            FROM `likes` AS L 
            WHERE `user_id` = '7' 
            AND `band_id` = BA.`band_id` 
        )
    )